Abstract Factory (Abstract Factory Pattern)

Define (Definition)

Abstract factory pattern provides a uniform interface to create a family of products. When you need the family of a series, you can select from a series of relatively abstract factory to create a concrete factory class.

Motivation (Motivation)

In software systems, often faced with "a series of interdependent objects " to create work; the same time, due to the changes in demand,

There is often create more series of objects work.

Structure (Structure)

Roles (Roles)

Abstract Factory (Abstract Factory): declare a product to create abstract object interface operation.

Concrete Factory (Concret Factory): creation of specific products to achieve the object.

Abstract product (Abstract Product): a class of products subject to declare an interface.

Specific products (Concrete Product): The definition of a concrete factory is to create the appropriate product target to achieve AbstractProduct interface.

Client applies only interface consists AbstractFatory and AbstractProduct class declaration.

Applicable

1. a system to be independent of the product when it is created, combined and expressed.

2. A system for multiple product lines in a configured time.

3. The need to emphasize the design of a series of objects related products for joint use.

4. To provide a class library of products, and they want to implement the interface instead of the display.

For example

Huawei and OPPO is taken as an example, they can be seen as two series, series of Huawei, OPPO series, the series also includes Huawei Huawei mobile phone products, flat products of Huawei, OPPO OPPO series also includes mobile phones, flat OPPO products.

FIG ConcreteFactory1 Huawei factory, ConcreteFactory2 plant is OPPO

AbstractProductA abstract mobile phone products, AbstractProductB abstract Tablet PC products

ProductA1 Huawei phone, ProductA2 is OPPO phone

ProductB2 OPPO is flat, ProductB1 Huawei Tablet

IPhone interfaces for client access, coupled with the release of the specific type of access the mobile phone, which corresponds to the structure of FIG. Abstract ProductA

    interface IPhone
    {
        void Start();  //启动
        void Off();    //关机
    }

HuaWeiPhone particular product class, the corresponding structure in FIG ProductA1

    class HuaweiPhone:IPhone
    {
        public void Start()
        {
            Console.WriteLine("华为手机开机了");
        }

        public void Off()
        {
            Console.WriteLine("华为手机关机了");
        }
    }

OPPOPhone particular product class, the corresponding structure in FIG ProductA2

    class OPPOPhone :IPhone
    {
        public void Start()
        {
            Console.WriteLine("OPPO手机开机了");
        }

        public void Off()
        {
            Console.WriteLine("OPPO手机关机了");
        }
    }

ITabletComputer interfaces for client access, coupled with the release of the particular type of access plate, corresponding structures in FIG AbstractProductB

    interface ITabletComputer
    {
        void Start();
    }

DETAILED flat products Huawei class that implements the interface plate, in the corresponding structures in FIG ProductB1

    class HuaweiTablet : ITabletComputer
    {
        public void Start()
        {
            Console.WriteLine("华为平板电脑启动了");
        }
    }

DETAILED flat products OPPO class that implements the interface plate, in the corresponding structures in FIG ProductB2

    class OPPOTablet : ITabletComputer
    {
        public void Start()
        {
            Console.WriteLine("OPPO平板电脑启动了");
        }
    }

Defines an abstract factory interface corresponding structures in FIG Abstract Factory

    interface IFactory
    {
        IPhone CreatePhone();
        ITabletComputer CreateTablet();
    }

HuaweiFactory specific plant defined class that implements the interface ConcreteFactory1 IFactory, corresponding structures in FIG.

    class HuaweiFactory : IFactory
    {
        public IPhone CreatePhone()
        {
            return new HuaweiPhone();
        }

        public ITabletComputer CreateTablet()
        {
            return new HuaweiTablet();
        }
    }

OPPOFactory specific plant defined class that implements the interface ConcreteFactory2 IFactory, corresponding structures in FIG.

    class OPPOFactory : IFactory
    {
        public IPhone CreatePhone()
        {
            return new OPPOPhone();
        }
        public ITabletComputer CreateTablet()
        {
            return new OPPOTablet();
        }
    }

Client code

            IFactory factory = new HuaweiFactory();
            IPhone phone = factory.CreatePhone();
            phone.Start();
            phone.Off();
            ITabletComputer tablet = factory.CreateTablet();
            tablet.Start();
            Console.Read();

operation result

If you want to create OPPO series of mobile phones and flat, only need to change the code below this line client will be able to:

IFactory factory = new HuaweiFactory();  →   IFactory factory = new OPPOFactory();

Results are as follows

Summary : It applies to products, but its drawback is difficult to support new types 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 types of products. In other words, if the OPPO and now Huawei has added a new business, the production of notebook computers, then we need to increase the abstract factory interface, and Huawei and OPPO two concrete plants will be changed, which violates the principle of opening and closing .

If you have questions, welcome to correct me ~ ~ ~

 

Published 75 original articles · won praise 24 · views 20000 +

Guess you like

Origin blog.csdn.net/wtt15100/article/details/105167002