Design patterns implementation examples

Factory method

Definition:

Creates objects of one family without using class names. It is done by specially created method called factoryMethod which allows to hide implementation details in subclasses - subclasses decide which real object (implementation) should be created.


Example:

A parent asks a child for help when buying a TV. He uses general term "TV" because children are the ones that are experts in that field, they know all the specifics but hide them from their parents - they do not explain the specifics which parents do not want to know, instead they choose a product adequate for them. A parent trusts his child, and believes that the child will choose the TV which will fulfil his needs.


C++ code:

/src/factorymethod.cpp
Download source code

Factory method UML class diagram:
Factory method UML class diagram

Factory method UML sequence diagram:
Factory method UML sequence diagram