Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Applicability
Use the Abstract Factory pattern when
a system should be independent of how its products are created, composed, and represented.
a system should be configured with one of multiple families of products.
a family of related product objects is designed to be used together, and you need to enforce this constraint.
you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.
Structure
Members
Product Interface
All products will implement this inteface so that any classes that need to use products will refer to this interface instead of referring the concrete product class.
ConcreteProducts
Concrete products that implement the Product interface.
ConcreteFactories
Overrides the factory method to return an instance of a ConcreteProduct.
Creator
Declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.
May call the factory method to create a Product object.
Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct.
Outcome
Factory methods eliminate the need to bind application-specific classes into your code. The code only deals with the Product interface; therefore it can work with any user-defined ConcreteProduct classes.
A potential disadvantage of factory methods is that clients might have to subclass the Creator class just to create a particular ConcreteProduct object. Subclassing is fine when the client has to subclass the Creator class anyway, but otherwise the client now must deal with another point of evolution.
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Applicability
Use the Factory Method pattern when
a class can’t anticipate the class of objects it must create.
a class wants its subclasses to specify the objects it creates.
classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
Structure
Members
Product Interface
All products will implement this inteface so that any classes that need to use products will refer to this interface instead of referring the concrete product class.
ConcreteProducts
Concrete products that implement the Product interface.
ConcreteFactories
Overrides the factory method to return an instance of a ConcreteProduct.
Creator
Declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.
May call the factory method to create a Product object.
Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct.
Outcome
Factory methods eliminate the need to bind application-specific classes into your code. The code only deals with the Product interface; therefore it can work with any user-defined ConcreteProduct classes.
A potential disadvantage of factory methods is that clients might have to subclass the Creator class just to create a particular ConcreteProduct object. Subclassing is fine when the client has to subclass the Creator class anyway, but otherwise the client now must deal with another point of evolution.
Singleton is a creational design pattern that is used when you need to ensure that a class has only one instance, the singleton class should also be providing a global access point to this instance.
Applicability
Use the Singleton pattern when
there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.
when the sole instance should be extensible by sub-classing, and clients should be able to use an extended instance without modifying their code.
Structure
Members
static getInstance() : We hide the constructor and we only provide this static function to get an instance of Singleton object.
instance attribute : This private attribute will hold the only instance of Singleton object.
Outcome
Controlled access to sole instance. Because the Singleton class encapsulates its sole instance, it can have strict control over how and when clients access it.
Reduced name space. It avoids polluting the name space with global variables that store sole instances.
Permits a variable number of instances. The pattern makes it easy to change your mind and allow more than one instance of the Singleton class. Moreover, you can use the same approach to control the number of instances that the application uses. Only the operation that grants access to the Singleton instance needs to change.