Two soldier - factory model (Factory Method)

Foreword

  Previous singleton pattern we introduced today to tell you a relatively simple pattern - factory model (Factory's Method, ) , the factory model, what is it? As the name suggests, factory - manufacturing things local. Then the application how to use them in the program, and they play what effect? So why does the factory model?

  Before we say that the principle of OCP (open closed principle), be open for extension, closed for modification of this principle. Excellent reflected in the factory mode, the object classes and excellent package. So as to reduce the coupling between the tags. More scalable.

Factory Pattern Introduction

First, the reason

  Among our programming, as well as creating an object created will always face some classes, but due to the changing needs (add or modify), resulting in a change of the object. Then how to deal with this problem?

A package mechanism provided factory pattern, easy to isolate the objects that change. This time to change the subject before the change will not affect demand.

Second, the intent

  Define an interface for creating an object, but let subclasses decide which class to instantiate technology. FactoryMethod make a class instance to delay its subclasses.

Third, the case of FIG.

 

 

Factory mode consists of the following components:

(1) , Abstract Factory (Creator): Copy the basic rules needed to define the plant, any other specific plants had inherited this abstract factory 

(2) , concrete factory (by Concrete _ Creator): extend the abstract factory implementation of its definition, in order to create specific products

(3) the abstract product (Product): Copy the basic methods and rules defined products, all products had specific product based on this abstract class

(4) , specific products (by Concrete _ Product): inherited abstract products, and its definition of abstract method, created by the concrete plant, the two are one to one relationship.

Fourth, the factory model code implementation

Here we introduce the rules and usage through the factory model of a case. In Factory mode, define the abstract factory and its plant-specific implementation calls the inherited abstract method inherited a specific product to create abstract product class products.

 

 

 

Here we manufacture mobile phones to look at the following examples of it:

namespace ConsoleApp4 
{ 
   public   class Factory_Method_Pattern 
    { 
    } 
    #region   product =========================
     ///  <Summary> 
    /// abstract class phones
     / //  </ Summary> 
    public  abstract  class phone 
    { 
        public  abstract  String the Create (); 
    } 

    ///  <Summary> 
    /// DETAILED Huawei phones class
     ///  </ Summary> 
    public  class the Huawei: phone 
    { 
        ///  < the Summary> 
        /// implement abstract methods
        ///  </ Summary> 
        ///  <Returns> </ Returns> 
        public  the override  String the Create () 
        { 
            return  " Huawei One secular " ; 
        } 
    } 

    ///  <Summary> 
    /// specific product categories millet phone
     @ /  </ Summary> 
    public  class Xiaomi: Phone 
    { 
        ///  <Summary> 
        /// implement the abstract methods
         ///  </ Summary> 
        ///  <Returns> </ Returns> 
        public  the override  String the Create () 
        { 
            return  "One secular millet "; 
        } 
    } 

    #Endregion 

    #region   plant =========================
     ///  <Summary> 
    /// abstract factory class
     ///  </ Summary> 
    public  abstract  class factory's 
    { 
        ///  <Summary> 
        /// method abstract factory class, product creation call
         ///  </ Summary> 
        ///  <Returns> </ Returns> 
        public  abstract Phone CreatePhone (); 
    } 

    ///  <Summary> 
    /// DETAILED Huawei factory class inherits the abstract factory class
     ///  </ Summary> 
    public  class HuaweiFactory: factory's 
    {
        ///  <Summary> 
        /// inheritance method
         ///  </ Summary> 
        ///  <Returns> </ Returns> 
        public  the override Phone CreatePhone () 
        { 
            return  new new the Huawei (); 
        } 
    } 

    ///  <Summary> 
    /// DETAILED millet factory class inherits the abstract factory class
     ///  </ Summary> 
    public  class XiaomiFactory: factory's 
    { 
        ///  <Summary> 
        /// inheritance method
         ///  </ Summary> 
        ///  <Returns> </ returns>
        public override Phone CreatePhone()
        {
            return  new new Xiaomi (); 
        } 
    } 
    #endregion  
}

 

 class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            // Initialization plant 
            Factory's huaweiFactory = new new HuaweiFactory (); 
            
            // production of specific phone Huawei 
            var Result = huaweiFactory.CreatePhone ();
             // Huawei phone secular 
            var answer = Result .create (); 
            Console.WriteLine (answer); 

            Factory's xiaomiFactory = new new XiaomiFactory (); 
            Result = xiaomiFactory.CreatePhone (); 
            answer = result.Create();
            Console.WriteLine(answer); 

            Console.ReadLine();
        }
    }

 

  In the above case, the same is the first mobile phone abstract an abstract class, the phone certainly not appear out of thin air. Therefore, a need to abstract factory class. Factory class derived different subclasses of class mobile phone manufacturers. The same mobile phone also derive a different brand. Then the corresponding class mobile phone manufacturers to produce mobile phone brand in the phone. Then we need to add a mobile phone handset brand, we do not need to modify the factory class and product category. We can add another. It does not affect its original run. For example, we need to add Meizu phone.

    #region new Meizu phone ================
     ///  <the Summary> 
    /// New Meizu factory class
     ///  </ the Summary> 
    public  class MeizuFactory: Factory's 
    { 
        public  the override phone CreatePhone () 
        { 
            return  new new Meizu (); 
        } 
    } 

    ///  <Summary> 
    /// Add Meizu phone specific product categories
     ///  </ Summary> 
    public  class Meizu: phone 
    { 
        public  the override  String the Create () 
        { 
            return  " Meizu One secular " ;
        } 
    } 
    #Endregion

 

Add the following invoked when the client calls on it

            Factory meizuFactory = new MeizuFactory();
            result = meizuFactory.CreatePhone();
            answer = result.Create();
            Console.WriteLine(answer);

Usage scenarios and the advantages and disadvantages

In this mode, all the work to create specific objects are delayed in the subclass to achieve, to achieve an expansion strategy. OCP correspond with the principle.

Factory mode is aimed at solving the "single object" problem. Coupling between the user and the particular type of object class isolation. To achieve a certain degree of decoupling.

(A) usage scenario

1, when the user does not need to know the class name of the product or do not care when the time how to create. We just need to know that it was created when the concrete factory.

2, the task entrusted to create objects in a multiple plants, when the client does not need to be concerned about the use of which is to create a product. When you need to specify dynamic.

(Ii) the advantages

1, the programming method, the client calls do not need to know the specific plant products, we do not need to be concerned about how to create.

2, the increase in demand when the product increases when only need to add a class factory and product category, do not need to modify the original code. It can be extended easily.

(C) shortcomings

Increased product, adds additional factory class and product category. Add a product to add two classes. To some extent, increased the complexity of the system. But also enhance the dependencies between classes. This is where the problem of poor

to sum up

  Each design pattern has a corresponding occasions, not to abuse. As we learn design patterns, we also need it as a reference for its original main study. It must not violate the principle. In design mode - the factory model, you create an object of the task entrusted to its factory subclass, delayed execution. Difficult to expand the system to solve the problem, but also reduces coupling between specific classes and objects. Also implements the principle of open extended closed modified.

  This society is unfair, do not complain, because there is no use! People are always reflect the progress!

  C # Design Patterns series directory

 

Welcome to scan the next Fanger Wei code, and I set foot on the road to pass through the design patterns it!

  

 

Guess you like

Origin www.cnblogs.com/hulizhong/p/11404514.html