Simple factory mode: Creates a factory class, and the caller Passes parameters to the factory class to determine whether a specific instance is returned.
First, let's look at the class chart. PS: in the days when I was a code Porter, I didn't need to draw pictures or even draw pictures. So I learned to draw pictures from scratch to deepen my understanding.
1 namespace designmodel. simple factory Mode 2 {3 4 5 public class animalfactory 6 {7 internal static animal getanimal (string name) 8 {9 animal = NULL; 10 switch (name) 11 {12 case "": 13 animal = new A (); 14 break; 15 case "": 16 animal = new B (); 17 break; 18 default: 19 animal = new C (); 20 break; 21} 22 return animal; 23} 24} 25 26 abstract class animal27 {28 public int weigtht {Get; set ;} 29 public string name {Get; set;} 30 public abstract void laugh (); 31} 32 internal class A: animal33 {34 public override void laugh () {} 35} 36 internal class B: animal37 {38 public override void laugh () {} 39} 40 internal class C: animal41 {42 Public override void laugh () {} 43} 44} 45 46 // <summary> 47 // Use 48 /// </Summary> 49 static void simple factory mode () 50 {51 var animal = animalfactory. getanimal ("A"); 52 animal. laugh (); 53 54}
View code
Advantage: the factory class contains logic judgment, and relevant classes are instantiated Based on the caller's choice. For the caller, the dependencies with specific products are removed.
Disadvantage: to add a product class, you need to modify the factory class to add a case Branch. It violates the principle of opening/closing.
Factory method mode: defines the interface for object creation, so that the subclass of the interface decides to instantiate a specific class, and delays the instantiation of a class to its subclass.
1 namespace designmodel. factory method mode 2 {3 Internal interface ifactory 4 {5 animal createanimal (); 6} 7 8 9 class afactory: ifactory10 {11 Public animal createanimal () 12 {13 return new A (); 14} 15} 16 class bfactory: ifactory17 {18 public animal createanimal () 19 {20 return New B (); 21} 22} 23 24 abstract class animal25 {26 Public int weigtht {Get; set;} 27 public string name {Get; set;} 28 public abstract void laugh (); 29} 30 31 Class A: animal32 {33 public override void laugh () {}34} 35 class B: animal36 {37 public override void laugh () {} 38} 39 40} 41 static void factory method mode () 42 {43 ifacloud if = new afactory (); 44 var ani = if. createanimal (); 45 Ani. laugh (); 46}
View code
In the preceding two modes, the factory class returns the parent class of a specific product and is designed for abstract programming.
Attached design principles:
1. Open and Close principle (open to expansion and close to modification)
2. Single Responsibility Principle (there should be no more than one reason for class change, and one class is only responsible for one responsibility)
3. Dependency inversion principle (high-level modules should not depend on low-level modules, but must depend on abstraction and programming interfaces)
4. Hierarchy replacement principle (subclass can replace parent class, that is, parent and child are not dependent and only dependent on interfaces)
5. Interface isolation principle (to minimize the number of interfaces required for sub-classes)
6. dimit Law (objects are loosely coupled. An object should be least associated with other objects. If you want to call other object methods, you can forward the call through a third party)
Design Mode 1-simple factory mode and factory method mode