📖 preface

In the previous article, we had some understanding of the general factory pattern in the factory pattern. In fact, the abstract work is to represent the further abstraction of the factory method, for the factory method itself also needs to be abstracted centralized management.

  • From the factory pattern, we know that calling the same factory will produce different results depending on user input.

  • The abstract factory pattern: on the basis of the factory method extends the factory’s support for multiple products to create, more suitable for some large systems, such as system, which has more than one product family, and species of these products need to implement the same interface, like many software system interface under different themes in different button, text box, type and so on.


🚀 instance

Such as:

In writing a game for all ages, the game itself needs to be developed using a factory approach. However, the game also needs to take into account the different needs and tastes of different age groups, so it needs to be modified for different age groups. Then, after the user enters his age, the game will run in accordance with his age requirements.

Code (from Mastering Python Design Patterns)

Class Frog(object): "" def __init__(self,name): name def __str__(self): return self.name def interact_with(self,obstacle): print('{} the Frog encounters {} and {} ! '. The format (self, obstacle, the obstacle. The action ())) class Bug (object) : ' 'Bug class "' def the __str__ (self) : Return 'a bug' def action(self): return 'eats it' class FrogWorld(object): "" def __init__(self,name): print(self) self.player_name = name def __str__(self): return '----------welcome to FrogWorld-------------' def make_character(self): return Frog(self.player_name) def make_obstacle(self): return Bug() class Wizerd(object): "' the wizard class 'def __init__ (self, name) : the self. The name = name def the __str__ (self) : return self.name def interact_with(self,obstacle): print('{} the Wizerd battles against {} and {} ! '. The format (self, obstacle, the obstacle. The action ())) class Ork (object) : ' 'monster class "' def the __str__ (self) : return 'an evil ork' def action(self): return 'kills it' class WizerdWorld(object): Def __init__(self,name): print(self) self.player_name = name def __str__(self): return '--------welcome to WizerdWorld-------------------' def make_character(self): return Wizerd(self.player_name) def make_obstacle(self): return Ork() class GameEnvironment(object): Def __init__(self,factory): self.hero = factory.make_character() self.obstacle = factory.make_obstacle() def play(self): Self.hero. Interact_with (self.obstacle) def validata_age(name): self.hero. Interact_with (self.obstacle) def validata_age(name): age = input('welcome {}, How old are you?'.format(name)) age = int(age) except Exception as e: print('Age {} is invalid,please try again... '.format(age)) return(False,age) return(True,age) def main(): name = input("Hello,What's you name?" ) valid_input = False while not valid_input: valid_input,age = validata_age(name) game = FrogWorld if age<18 else WizerdWorld environment =GameEnvironment(game(name)) environment.play() if __name__ == '__main__': main() print('hello world')Copy the code

Resolution:

1. Manage two games through a GameEnvironment: FrogWorld and WizerdWorld, and decide which game to play according to the user’s age input, FrogWorld or wizard world?

2. Each game class (FrogWorld, WizerdWorld) manages two characters — Frog, Bug and Wizerd, Ork.

The idea is the same in both the factory and abstract factory patterns: depending on different inputs, the same interface is called and different results are obtained. Its internal encapsulation of the operation process, the user does not need to know its internal how to achieve how to choose, just input can get the results.


🎉 summary

  • For more references, see here:The Blog of Chan Wing Kai
  • Like the small partner of the blogger can add a concern, a thumbs-up oh, continue to update hey hey!