Gang Of Four Abstract Factory Pattern In Python Using UML

| | 0 Comment| 7:58 pm|

The “GOF (Gang Of Four) Design Patterns” is a framework that has been used in various OOP (Object Oriented Programming) environments for a long time. This article describes the Abstract Factory Pattern (which adds an abstraction layer over multiple creational pattern implementations). The Abstract Factory Pattern is part of what is commonly called “Creational Patterns“.

In simple terms, this pattern is a Factory that can return Factories. It is also found in examples of being used to return Builder, Prototypes, Singletons or other design pattern implementations.

Below, I have presented one Class Model diagram and two Sequence Model diagrams to illustrate the power of the Abstract Factory Pattern. To make this clear, the terminologies used in the diagrams are the following:

  • Client: The application that calls the Abstract Factory.
  • Abstract Factory: A common interface over all of the sub-factories.
  • Concrete Factory: The sub-factory of the Abstract Factory and contains method(s) to allow creating the Concrete Product.
  • Abstract Product: The interface for the product that the sub-factory returns.
  • Concrete Product: The object that is finally returned.

Class Diagram

The diagram below includes everything: IAbstractFactory, AbstractFactory, IProduct, along with the concrete products, FactoryA, and FactoryB.

All the relationships are clear:

  • AbstractFactory uses both FactoryA and FactoryB.
  • Both factories create the concrete products (Concrete Products – A, B, and C – that are displayed at the bottom of the class diagram).

Class Diagram Abstract Factory

Sequence Diagrams

  • For AbstractFactory.create_object(‘ab’): This uses FactoryA and creates ConcreteProductB.
  • For AbstractFactory.create_object(‘bc’): This uses FactoryB and creates ConcreteProductC.

SD Abstract Factory A

SD Abstract Factory B

All of the above diagrams have their respective Python Program code.
If anybody is interested, I can share the actual files.