From the course: Java Design Patterns: Creational

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Understand the Factory Method pattern

Understand the Factory Method pattern - Java Tutorial

From the course: Java Design Patterns: Creational

Start my 1-month free trial

Understand the Factory Method pattern

- [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…

Contents