CHAPTER 12

image

Factory Method Patterns

GoF Definition: Define an interface for creating an object, but let subclasses decide which class to instantiate. The factory method lets a class defer instantiation to subclasses.

Concept

The concept can be best described with the examples below.

Real-Life Example

Suppose you have two different types of televisions—one with an LED screen and another with an LCD screen. If any of these starts malfunctioning, you will call a TV repairman to request a visit to your residence. The repairman must ask first what kind of TV is nonoperational. As per your input, he’ll carry the required instruments with him.

Computer ...

Get Java Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.