In this video, discover the Factory Method pattern. Learn how using a Factory Method can prevent code from becoming long, complicated, and difficult to maintain.
- [Instructor] The factory method deals with the problem…of needing to crate objects but not knowing…exactly what type of objects need to be created.…There might be a class that creates new objects,…but when the class is written, you want to be flexible…about the type of objects it creates.…In fact, the class might have no idea…what kind of objects it will want to create.…For example, say that there is a candy store…that sells different types of candy.…The owners of the candy store will want to buy in stock,…but when they open the store, they won't know…which candy they will want to buy in the future.…
The job of the store is just to sell candy.…It is not the job of the store to get involved…with exactly how the candy is created.…The solution to this problem is to use…the factory method pattern.…This involves adding an extra class…between the candy store and the candy.…This class has something called a factory method…which handles deciding which class to return.…This means that all the code that decides…which candy to make is moved out of the candy…
- What are creational design patterns?
- Avoid complex constructors
- Implementing the Builder pattern
- Multithreading with the Singleton pattern
- Using the Prototype pattern
- Implementing a simple Factory Method pattern
- Making code more flexible with the Abstract Factory pattern