samedi 9 janvier 2021

Can I have multiple template methods?

As the title says, is it a good practice to have multiple template methods?

Here is an example to demonstrate the situation:

class BaseTemplate:
    def template1(self):
        self.primitive1()
        self.primitive3()

    def template2(self):
        self.primitive3()
        self.primitive2()


class Concrete1(BaseTemplate):
    def primitive1(self):
        print("primitive1 called from C1")
    def primitive2(self):
        print("primitive2 called from C1")
    def primitive3(self):
        print("primitive3 called from C1")


class Concrete2(BaseTemplate):
    def primitive1(self):
        print("primitive1 called from C2")
    def primitive2(self):
        print("primitive2 called from C2")
    def primitive3(self):
        print("primitive3 called from C2")

Questions:

  1. If this is not a good pattern then, what are the drawbacks?
  2. Are there any other solutions for this situation?

Aucun commentaire:

Enregistrer un commentaire