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.
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“:
title Factory Method Pattern
Creator : factoryMethod()
Creator : anOperation()
ConcreteCreator : factoryMethod()
Creator <|– ConcreteCreator
Product <|– ConcreteProduct
ConcreteProduct <- ConcreteCreator
note top of Creator
The Creator is a class that contains
the implementation for all of the
methods to manipulate products,
except for the factory method.
note right of Creator
The abstract factoryMethod()
is what all Creator subclasses
note right of ConcreteCreator
factoryMethod(), which is
the method that actually
note “The ConcreteCreator is responsible for\ncreating one or more concrete products. It\nis the only class that has the knowledge of\nhow to create these products.” as n1
ConcreteProduct .. n1
ConcreteCreator .. n1
note “All products must implement\nthe same interface so that the\nclasses which use the products\ncan refer to the interface,\nnot the concrete class.” as n2
n2 . ConcreteProduct
n2 . Product
@startuml title Factory Method Pattern class Product class Creator class ConcreteProduct class ConcreteCreator Creator : factoryMethod() Creator : anOperation() ConcreteCreator : factoryMethod() Creator <|-- ConcreteCreator Product <|-- ConcreteProduct ConcreteProduct <- ConcreteCreator @enduml
Other Design Patterns
In another article you find information about how to put together a single-side web application using PlantUML.
[display-posts category=”Software Design Patterns in PlantUML” include_excerpt=”true” excerpt_length=”20″ excerpt_more=”… Read More” excerpt_more_link=”true” image_size=”thumbnail” wrapper=”div”]