Lecture 19
Factory Method Pattern
UML
class Level {
public:
virtual Enemy *createEnemy()=0;
};
class Easy: public Level {
public:
Enemy *createEnemy() override {
// easy levels would have higher probability of creating turtles randomly rather than bullets
}
};
class Hard: public Level {
public:
Enemy *createEnemy() override {
// harder level would have a higher probability of producing a bullet than turtles
// might also check game progress and generate boss at certain point
}
};
Level *l = new Easy/Hard
Enemy *e = l->createEnemy();
// the factory method calls the level function to give the correct probability of getting a turtle or a bullet
// we did not call the constructor for an enemy, but the factory method calls it for us
// we also did not have to decide whether to create a turtle or a bullet, since `createEnemy` decides for us
}Template Method Pattern
Non-Virtual Interface (NVI)
Public Virtual Method
STL Maps - For creating dictionaries
Iterating over a map
Visitor Pattern
UML

Last updated

