Factory method (Factory Method) mode

Introduction (Introduction)

Previous blog with a simple example to introduce a mobile phone factory factory pattern, logic will create OPPO OPPO mobile phone and objects are placed inside PhoneFactory inside, and were created two specific products like mobile phones OPPOPhone, OPPOPhone, which specifically Create like mobile phones, depends on the parameters passed. In this case, if the mobile phone factory wants to expand the scale of production, adding millet mobile phone production, we need to change two places: Add a XiaoMiPhone specific product category; modify the internal logic of the plant, increasing the branch judgment of a XiaoMiPhone; not only for extended opening up, but also open for modification (to modify the factory class), which is contrary to the principle of opening and closing. To solve this problem, we need to use the factory method (Factory Method) mode a.

Intention (Intention)

Define an interface for creating an object, but let subclasses decide which class to instantiate technology. Examples of such a Factory Method delay class to subclasses.

Structure (struct)

Roles (Roles)

Abstract Factory (Creator): Core factory method, any class factory object created in the schema must implement this interface.

Specific plant (Concrete Creator): implement particular abstract factory class factory interface.

Abstract product (Producr): parent object created by the factory method (or that these products have a common interface).

Specific products (ConcreteProduct): created by the corresponding concrete factory, the product is an abstract subclass (or implement abstract interface products)

Examples (Example)

Create a factory interface

    interface IFactory
    {
        Phone CreatePhone();
    }

Three brand mobile phone each to build a concrete plant to implement this interface

    class HuaWeiFactory : IFactory
    {
        public Phone CreatePhone()
        {
            return new HuaweiPhone();
        }
    }
    class OPPOFactory : IFactory
    {
        public Phone CreatePhone()
        {
            return new OPPOPhone();
        }
    }
    class XiaoMiFactory : IFactory
    {
        public Phone CreatePhone()
        {
            return new XiaoMiPhone();
        }
    }

Abstract product categories: mobile phones classes

    public  class Phone
    {
        public virtual void Start(){}
    }

Specific product categories: HuaweiPhone, OPPOPhone, XiaoMiPhone, is a subclass of class Phone

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

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

    class XiaoMiPhone : Phone
    {
        public override void Start()
        {
            Console.WriteLine("小米手机开机了");
        }
    }

Implement client-side code

            IFactory phoneFactory = new HuaWeiFactory();
            Phone phone = phoneFactory.CreatePhone();
            phone.Start();

operation result

Compared with the simple factory, abstract factory pattern, concrete factory class that we want to add a phone millet, millet phone only need to add a class and a millet phone can, without modifying the original factory class. So that only the expansion of change, and change is not modified, it is entirely in line with the open - closed principle. A method implemented at the factory mode, the client that determines which code examples of a plant is achieved which creates a brand of mobile phone, there is determined, within the factory factory method simple logic determines moved to client code, you do not need to change factory class, only need to modify the client on it.

If the wrong place, welcome to correct me ~ ~ ~ ~

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

Guess you like

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