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.

Introduction to hierarchies

Introduction to hierarchies - Java Tutorial

From the course: Java Design Patterns: Creational

Start my 1-month free trial

Introduction to hierarchies

The Factory method pattern gets more complicated when further hierarchies are introduced. A more complete version of the Factory method helps with this by allowing subclasses to decide which concrete type to return. This example is a more complete version of the Candy Store application. In this example there is still a class called Candy Store, which gets packages of candy to sell. In reality there would probably be a lot more different types of candy than just chocolates and hard candy. In fact, chocolates and hard candy are actually whole sections of the store rather than products. Now there are several different types of chocolate: dark chocolates, milk chocolates, and white chocolates. There are also three different types of hard candy: candy canes, lollipops, and peppermints. All of these different types extends the Candy class and overrides the makeCandyPackage method. The candy that is in the hard candy sections prints out a slightly different message. Hard candy has to be…

Contents