Introduction
This post gives a brief overview about the Factory Method Pattern. The post is part of a series about software design patterns and their UML representations with the help of PlantUML.
The article aims at providing a very short description of the general idea of the pattern in the first part. This also involves a descriptive UML diagram. Then, the second part provides the PlantUML code for the diagram so that these posts can also be used as a source of design patterns in PlantUML syntax.
What is the Factory Method Pattern?
According to Wikipedia, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.
UML Diagram
The following diagram shows the Factory Method Pattern in UML notation. It is based on the corresponding chapter in the book “Head First Design Patterns“:
You can click on the diagram to open it online in Pladitor.
Diagram Sources
Here are the diagram sources:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| @startuml
hide empty fields
hide empty methods
interface Product
abstract Creator
class ConcreteProduct
class ConcreteCreator
Creator : factoryMethod()
Creator : anOperation()
ConcreteCreator : factoryMethod()
ConcreteCreator --|> Creator
ConcreteProduct --|> Product
ConcreteProduct <- ConcreteCreator
note right of Creator
The Creator is a class that contains
the implementation for all of the
methods to manipulate products,
except for the factory method.
end note
note left of Creator
The abstract factoryMethod()
is what all Creator subclasses
must implement.
end note
note right of ConcreteCreator
The ConcreteCreator
implements the
factoryMethod(), which is
the method that actually
produces products.
end note
note as n1
The ConcreteCreator is responsible for
creating one or more concrete products. It
is the only class that has the knowledge of
how to create these products."
end note
n1 .. ConcreteProduct
n1 .. ConcreteCreator
note as n2
All products must implement
the same interface so that the
classes which use the products
can refer to the interface,
not the concrete class.
end note
n2 . ConcreteProduct
n2 . Product
@enduml
|
Open this diagram online with Pladitor.