SWE : Creational Design Pattern

Part 2 of Design Pattern series

Pisit J.
Sum up As A Service
4 min readMar 4, 2021

--

image

1. Abstract Factory

  • Produce families of related objects without specifying their concrete classes.
  • Client works with factories and products using abstract interfaces. It makes the same client code working with many product variants, depending on the type of factory object.

Pros

  • You can extract the product creation code into one place, making the code easier to support.
  • You can introduce new variants of products without breaking existing client code.

Cons

  • The code may become more complicated because a lot of new interfaces and classes are introduced.

Example

  • Abstract Factory defines an interface for creating buttons and checkboxes. There are two concrete factories, which return both products in a single variant.
  • In this example, buttons and checkboxes will act as products. They have two variants: macOS and Windows.
Factory
GUI Application (Client Code)
Main
Output

Reference

2. Builder

  • Construct various representations of the product involves similar steps that differ only in the details.
  • The base Builder interface defines all possible construction steps, and concrete builders implement these steps to construct particular representations of the product.
  • Meanwhile, the Director class defines the order of construction.
  • You can swap or defer execution of some steps without breaking the final product.
  • Builder doesn’t expose the final product until construction is complete.

Pros

  • You gain full control of construction — how to implement & order of steps to build products.

Cons

  • The overall complexity of the code increases since the pattern requires creating multiple new classes.

Example

Builder — defines all possible construction steps
Director — defines the order of construction
Main

Reference

3. Prototype

  • Keyword — clone, copy.
  • Delegates the cloning process to the actual objects that are being cloned. The pattern declares a common interface for all objects that support cloning.

Pros

  • Instead of instantiating a subclass that matches some configuration, The Prototype pattern lets you simply look for an appropriate prototype and clone it.

Cons

  • Cloning objects that have circular references might be very challenging.

Example

Prototype
Class extends Prototype
Main
Output

Reference

4. Singleton

  • Keyword — object pool, cache.
  • Ensure a class has only one instance, and provide a global point of access.
  • Unlike global variables, Singleton make sure that there’s just one instance of a class. Only the Singleton itself can replace the cached instance.

Pros

  • Singleton limit resource to instantiate object and limit how to access.

Cons

  • Singleton requires more concern in a multi-threaded environment so that Singleton objects won’t be created several times in each thread.

Example

Implementation of Singleton have these two steps in common:

  1. Make the default constructor private, to prevent other objects from using the new operator with the Singleton class.
  2. Create a static creation method that acts as a constructor. This method calls the private constructor to create an object and saves it in a static field. All following calls to this method return the cached object.
Singleton
Main
Output

Reference

--

--