How to implement the factory method with Python
This article mainly introduces the relevant knowledge of "how to realize the factory method in Python". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to realize the factory method in Python" can help you solve the problem.
Code example
Or an example of a maze game. The difference is that this time MazeGame built the maze himself.
Because it is a factory method pattern, CreateMaze does not hard-code objects such as Wall,Room,Door, but calls virtual function MakeRoom,MakeRoor,MakeWall objects separately.
After this preparation is completed, we can define different games separately:
These two derived classes redefine the MakeWall/MakeRoom and MakeRoom/MakeDoor methods according to their respective needs, with the following code:
This is the end of the content about "how to implement the factory method in Python". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.