lundi 4 mai 2020

How to implement Strategy Pattern with Base class dependencies?

I'm looking to implement the Strategy Pattern and was on my way to do as the answer in Stack Strat Pattern. But realised that I would be unable to copy it right of the bat. What I want to do is implement the pattern but also make sure that my executeReplacement functions inherit from the base class.

class DB():

    def __init__(self, league, season, func=None):
        self.db_user = os.environ.get('DB_user')
        self.db_pass = os.environ.get('DB_pass')
        self.MONGODB_URL = f'mongodb+srv://{db_user}:{db_pass}@database-mbqxj.mongodb.net/test?retryWrites=true&w=majority'
        self.client = MongoClient(MONGODB_URL)
        self.DATABASE = client["Database"]
        self.league = league
        self.season = season
        self.pool = multiprocessing.cpu_count()
        if func:
            self.execute = func

        def execute(self):
            print("No execution specified")

def executeReplacement1():
    """Should inherit the the properties of the base class
    since it's dependant on it's properties (Connect to Database etc.)"""
    print("Push some data to DATABASE")

How would one build the Strategy Pattern so that the replacement functions inherit the properties of the base class? Or is this perhaps not the optimal approach?

Aucun commentaire:

Enregistrer un commentaire