Factory Pattern || How to implement a factory in C++:
What is the factory pattern?
Ans:
- When a method returns one of several possible classes that share a common super class
- Create a new Enemy in a game
- Random number generator picks a number assigned to a specific enemy
- The Factory returns the enemy associated with the number
- The Class is chosen at run time
When to use a Factory || Factory Pattern?
Ans:
- When you don't know ahead of time what class object you need
- When all of the potential classes are in the same subclass hierarchy
- To Centralize class selection code
- When you don't want the user to have to know every subclass
- To Encapsulate Object Creation
Factory Code Example :
#include
#include
No comments:
Post a Comment