C # design patterns (2) - factory pattern

 

Source: https://www.cnblogs.com/wyy1234/

 

Read catalog

 


1. Introduction factory pattern

  Previous We know the shortcomings of the factory is simple: when we add a new product need to modify the factory class, so contrary to the principle of opening and closing. Factory pattern is to solve this defect occurs, the solution is to create a concrete example of the task in the subclass of the plant, the plant provides only create an instance of the interface , but also in the production of more than one mouse is example:

Mouse categories:

Copy the code
   // mouse abstract class 
    public abstract class Mouse 
    { 
        public abstract void the Print (); 
    } 

   // Dell mouse 
    public class DellMouse: Mouse 
    { 
        public void the override the Print () 
        { 
            Console.WriteLine ( "mouse produced a Dell!"); 
        } 
    } 

    // HP mouse 
    public class HpMouse: mouse 
    { 
        public void the override the Print () 
        { 
            Console.WriteLine ( "HP produced a mouse!"); 
        } 
    }
Copy the code

Factory class provides abstract methods produce only mouse (or interface), its subclasses produce specific products, such as Dell mouse factory plant inherits from Mouse, Mouse only Dell production; Hewlett-Packard HP mouse plants produce only mouse, as follows:

Copy the code
  /// <Summary> 
    /// mouse abstract class factory 
    /// </ Summary> 
    public abstract class MouseFactory 
    { 
        public abstract Mouse CreateMouse (); 
    } 

    // Dell mouse factory 
    public class DellMouseFactroy: MouseFactory 
    { 
        public CreateMouse the override Mouse () 
        { 
            return new new DellMouse (); // in particular examples of plant products 
        } 
    } 

    // HP mouse factory 
    public class HpMouseFactory: MouseFactory 
    { 
        public CreateMouse the override mouse () 
        { 
            return new new HpMouse (); // in a particular plant examples of products 
        } 
    }
Copy the code

Client code:

Copy the code
        the Main void static (String [] args) 
        { 
            // produce a Dell mouse 
            MouseFactory dellMouseFactory new new DellMouseFactroy = (); 
            Mouse dellMouse dellMouseFactory.CreateMouse = (); 
            dellMouse.Print (); 

            // HP produce a mouse 
            MouseFactory hpMouseFactory = new HpMouseFactory (); 
            Mouse hpMouse hpMouseFactory.CreateMouse = (); 
            hpMouse.Print (); 
            the Console.ReadKey (); 
        }
Copy the code

Run the program, results are as follows:

  In the example above we can see: the client to produce a particular product, the first product corresponding to the factory for specific examples, then instantiated by concrete factory product. If we want to produce ASUS mouse, then, to add a mouse ASUS factory class (AsusMouseFactory) and Asus Mouse class (AsusMouse), then the client to produce ASUS mouse with the following code:

MouseFactory asusMouseFactroy=new AsusMouseFactroy();
asusMouseFactory.CreateMouse();

By adding new products factory mode operation is only to add, and not to modify the previous code, in line with the principle of opening and closing.

2. Summary

FIG upper class code:

Factory pattern advantages:

  Factory mode effectively solves the problem of adding new products have to modify the factory class code, in line with the principle of opening and closing of the design principles.

Factory mode Cons:

  Nature plant model is to create specific examples of the work on the concrete subclasses were factory, which leads to a new problem: the type of task to select the instance of the client , such as Dell, we want to produce a mouse, you must in a new client Dell mouse factory. Imagine if we new the 100 mouse Dell factory, it is incumbent to HP mouse how to do? A new DellMouseFactory can only be replaced by a new HpMouseFactory. So simple factory and factory pattern is not perfect, we should be selected according to specific circumstances.

Guess you like

Origin www.cnblogs.com/frank0812/p/11241920.html