Design Patterns Demo
Design Patterns Demo
DESIGN PATTERNS
29 pages
from the full book are omitted in the demo version
72 Creational Design Patterns
Factory
Method
Provides an interface for creating objects in a superclass, but
allows subclasses to alter the type of objects that will be created.
Abstract
Factory
Lets you produce families of related objects without specifying
their concrete classes.
73 Creational Design Patterns
Builder
Lets you construct complex objects step by step. The pattern
allows you to produce different types and representations of an
object using the same construction code.
Prototype
Lets you copy existing objects without making your code depen-
dent on their classes.
Singleton
Lets you ensure that a class has only one instance, while provid-
ing a global access point to this instance.
74 Creational Design Patterns / Factory Method
FACTORY METHOD
Also known as: Virtual Constructor
Problem
Imagine that you’re creating a logistics management applica-
tion. The first version of your app can only handle transporta-
tion by trucks, so the bulk of your code lives inside the Truck
class.
After a while, your app becomes pretty popular. Each day you
receive dozens of requests from sea transportation companies
to incorporate sea logistics into the app.
Adding a new class to the program isn’t that simple if the rest of the code
is already coupled to existing classes.
Great news, right? But how about the code? At present, most of
your code is coupled to the Truck class. Adding Ships into
the app would require making changes to the entire codebase.
Moreover, if later you decide to add another type of transporta-
tion to the app, you will probably need to make all of these
changes again.
76 Creational Design Patterns / Factory Method
As a result, you will end up with pretty nasty code, riddled with
conditionals that switch the app’s behavior depending on the
class of transportation objects.
Solution
The Factory Method pattern suggests that you replace direct
object construction calls (using the new operator) with calls
to a special factory method. Don’t worry: the objects are still
created via the new operator, but it’s being called from within
the factory method. Objects returned by a factory method are
often referred to as products.
The code that uses the factory method (often called the client
code) doesn’t see a difference between the actual products
returned by various subclasses. The client treats all the prod-
ucts as abstract Transport .
78 Creational Design Patterns / Factory Method
Structure
79 Creational Design Patterns / Factory Method
You can declare the factory method as abstract to force all sub-
classes to implement their own versions of the method. As an
alternative, the base factory method can return some default
product type.
Pseudocode
This example illustrates how the Factory Method can be used
for creating cross-platform UI elements without coupling the
client code to concrete UI classes.
When the factory method comes into play, you don’t need to
rewrite the logic of the dialog for each operating system. If
we declare a factory method that produces buttons inside the
base dialog class, we can later create a dialog subclass that
returns Windows-styled buttons from the factory method. The
subclass then inherits most of the dialog’s code from the base
class, but, thanks to the factory method, can render Windows-
looking buttons on the screen.
For this pattern to work, the base dialog class must work with
abstract buttons: a base class or an interface that all concrete
buttons follow. This way the dialog’s code remains functional,
whichever type of buttons it works with.
75 method main() is
76 this
this.initialize()
77 dialog.render()
Applicability
Use the Factory Method when you don’t know beforehand the
exact types and dependencies of the objects your code should
work with.
For example, to add a new product type to the app, you’ll only
need to create a new creator subclass and override the factory
method in it.
Let’s see how that would work. Imagine that you write an
app using an open source UI framework. Your app should
have round buttons, but the framework only provides square
ones. You extend the standard Button class with a glorious
RoundButton subclass. But now you need to tell the main
a default one.
That’s a lot of code! And it must all be put into a single place
so that you don’t pollute the program with duplicate code.
How to Implement
1. Make all products follow the same interface. This interface
should declare methods that make sense in every product.
87 Creational Design Patterns / Factory Method
At this point, the code of the factory method may look pretty
ugly. It may have a large switch operator that picks which
product class to instantiate. But don’t worry, we’ll fix it soon
enough.
5. If there are too many product types and it doesn’t make sense
to create subclasses for all of them, you can reuse the control
parameter from the base class in subclasses.
6. If, after all of the extractions, the base factory method has
become empty, you can make it abstract. If there’s something
left, you can make it a default behavior of the method.
• You can use Factory Method along with Iterator to let collec-
tion subclasses return different types of iterators that are com-
patible with the collections.