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.

The complete Factory Method pattern

The complete Factory Method pattern - Java Tutorial

From the course: Java Design Patterns: Creational

Start my 1-month free trial

The complete Factory Method pattern

- [Instructor] Introducing hierarchies into the candy store example, made the CandyFactory class overly long and complex. In particular, the getCandy method was getting very long, and different types of candy needed different implementations of the getCandyPackage method. In this example, the full_factory_method_pattern has been implemented to solve this problem. Firstly, the CandyFactory class is now an abstract class, and there were two new classes ChocolateFactory and HardCandyFactory. Both of these classes extends the CandyFactory class. The CandyFactory class still has two methods, getCandy and getCandy package. The getCandy method is now an abstract method with no implementation. If I open the ChocolateFactory class, you can see that it provides a concrete implementation of this method. This version of the getCandy method is shorter because it only deals with candy in the chocolate section The HardCandyFactory class does the same. It implements the getCandy method so that he…

Contents