Creational pattern - abstract factory pattern (ABSTRACT FACTORY)

Provides an interface for creating a series of related or interdependent objects without specifying their concrete classes.

Specifically, in multiple abstract product classes, each abstract product class can derive multiple concrete product classes. An abstract factory class can derive multiple concrete factory classes. Each concrete factory class can create multiple instances of concrete product classes.

example:

Suppose we have two product interfaces Button and Border, and each product supports multiple series, such as Mac series and Windows series. In this way, the products of each series are MacButton, WinButton, MacBorder, WinBorder respectively. In order to create a series of product families at runtime, we can create a factory MacFactory and WinFactory for each series of product families. Each factory has two methods CreateButton and CreateBorder and returns the corresponding products. These two methods can be abstracted into an interface AbstractFactory. This way we can choose to create the desired product family at runtime.

Our product structure is
class Button; // Abstract 
Classclass MacButton: public Button 
{ }; class WinButton: public Button  { };



class Border; // Abstract 
class MacBorder: public Border 
{
};
class WinBorder: public Border 
{
};

class AbstractFactory 
{
public:
    virtual Button* CreateButton() =0;
    virtual Border* CreateBorder() =0;
};
class MacFactory: public AbstractFactory 
{
public:
    MacButton* CreateButton() { return new MacButton; }
    MacBorder* CreateBorder() { return new MacBorder; }
};
class WinFactory: public AbstractFactory 
{
public:
    WinButton* CreateButton() { return new WinButton; }
    WinBorder* CreateBorder() { return new WinBorder; }
};

Then customers can choose Mac style or Win style to create Button or Border
AbstractFactory* fac;
switch(style)
{     case MAC:         fac = new MacFactory;         break;     case WIN:         fac = new WinFactory;         break; } Button* button = fac->CreateButton(); Border* border = fac->CreateBorder();








Applicable situation:

The abstract factory pattern can be considered in the following situations:

  • When a system is to be independent of the creation, composition and presentation of its products.

  • When a system is to be configured from one of several product families. 

  • When it is necessary to emphasize the design of a series of related product objects for joint use.

  • When providing a library of product classes, you only want to show their interfaces and not their implementations.

Advantages and disadvantages:

advantage

(1) The concrete class is separated. The client manipulates the instance through the abstract interface, and the class name of the product is also separated in the implementation of the concrete factory, and they do not appear in the client code. 

(2) Easy to exchange product series. A concrete factory class appears only once during initialization, which makes it easy to change an application's concrete factory to use different product configurations by simply changing the concrete factory. 

(3) Conducive to product consistency. When a family of product objects are designed to work together, it is important that an application only use objects from the same family at a time, and Abstract Factory makes this easy.

shortcoming

Difficulty supporting new kinds of products. Because the abstract factory interface determines the set of products that can be created, it is difficult to extend the abstract factory to produce new kinds of products. 

Guess you like

Origin blog.csdn.net/rukawashan/article/details/124464445