mercredi 15 mai 2019

Design pattern for interface for different classes

I am seeking the right design pattern to implement interface in a class setting.

My file structure like follows:

  1. models: which contains different models written in classes, say Model, subModelA, subModelB, subsubModelC etc.
  2. calculators: which contains different calculator tools for each models. Note the calculator will need to import model attributes for computation.

My question is how should I structure my calculator file so as to follow the structure of models.

My original attempt is to write a ABC class for my calculator and then for each subModel class in model I write a respective subCalculator subclass to implement. However this does not seem to fully exploit the prescribed class structure in model.

Some baby example of my attempt:

# in model.py

class model(object):
    def __init__(self,attr1):
        self.attr1 = attr1

class submodel(model):
    def __init__(self, attr1, attr2):
        super().__init__(attr1)
        self.attr2

# in calculator.py
from model import model

class calculator(abc.ABC):
    @abc.abstractmethod
    def calculate(model):
        return model.attr1 ** 2

class subcalculator(calculator):
    def calculate(model):
        y = super().calculate(model)
        return y + model.attr2 ** 3

I have surveyed some design pattern catalogs as listed in here, and strategy seems to be the right pattern. But the baby example there does not address my concern, as I would hope to use class structure in model file.

I'd hope someone can give me a more full-fledged example in such case. My thanks in advance.

Aucun commentaire:

Enregistrer un commentaire