Creational Design Pattern

Factory Method lets a class defer instantiation to subclasses and we create objects without exposing the creation logic to client and the client use the same common interface to create new type of object.

The creator class declares the factory method that must return an object of a product class. The creator's subclasses usually provide the implementation of this method.The creator may also provide some default implementation of the factory method. You can use the factory method when set the Factory Method when you don’t know beforehand the exact types and dependencies of the objects your code should work with. Use the Factory Method when you want to provide users of your library or framework with a way to extend its internal components.

Make all products follow the same interface. This interface should declare methods that make sense in every product.

Add an empty factory method inside the creator class. The return type of the method should match the common product interface.

In the UML diagram below the Product interface defines the objects the factory method creates, which is implemented by the ConcreteProduct. Creator is composed of a HAS-A relationship with Product and it returns an object of type Product. Creator implements ConcreteCreator.

Once a Creator subclass is instantiated, it can then instantiate Creator-specific Products without knowing their class. It may also define a default implementation of the factory method that returns a default ConcreteProduct object. ConcreteCreator overrides the factory method to return an instance of a ConcreteProduct.

Abstract Factory

Provide an interface for creating families of related or dependent objects without specifying their
concrete classes.

In the UML diagram below AbstractFactory an interface for operations that create abstract product objects. The ConcreteFactories 1 and 2 implement operations to create concrete product objects. The AbstractProducts A and B are interfaces for a type Product object. Product/ConcreteProduct(S) define the products (A1,A2,B1,B2) objects to be created by corresponding ConcreteFactory and implement the AbstractProduct interface. The Client uses only interfaces declared by AbstractFactory and AbstractProduct classes.

Singleton

Singleton is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance. In a singleton pattern there must be exactly one instance of a class, and it must be accessible to clients from a well known access point. In the UML, Instance() returns one unique instance.

Builder

Builder is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.

In the UML diagram below, the Director constructs an object using the builder interface.

Prototype

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

Leave a comment

Design a site like this with WordPress.com
Get started