First, preface
Also on the way to work, a programmer thinking is to buy plum dried vegetables meat bag or buy hot and sour fan bag, to the bun point but found only steamed buns stuffed bun, but bought a packet of soy milk;
A programmer may go to work at home after eating dinner, this is rich second generation;
A programmer might think about how to optimize his code, how to improve performance, how to ...
The programmer on the way to work makes it easy to understand three design patterns
Second, simple Factory mode
Simple Factory pattern: Also known as the Static Factory method (Factory mode), it belongs to the class-creation pattern. In the simple Factory mode, you can return instances of different classes depending on the parameters. The simple factory model specifically defines a class to be responsible for creating instances of other classes, and the instances that are created typically have a common parent class.
Simple Factory mode is like a rich second generation programmer, what you want, simple factory will give you what.
The simple factory model consists of three roles: abstract product roles, specific product roles, factory roles
The code is implemented as follows:
Abstract Product role: Vehicle public abstract class car {public virtual void gotowork () { } } // Specific product roles: Bicycle public class Bike:car {public override void Gotowork () { Console.WriteLine (" Ride a bike to Work ");} } Specific product role: Public bus class Bus:car {public override void Gotowork () { Console.WriteLine (" Take a bus to work ");} } Factory role: Simple factory class public class Simplefactory {public static Car createsimple (String str) { car simple = null; Switch (str) {case ' Bike ': simple = new Bike (); break; Case "Bus": Simple = new Bus (); break; ... } return simple; } }
Client calling Code:
Based on the parameters passed in by the client simple factory decided how to go to work car car = simplefactory.createsimple ("Bus"); Car. Gotowork ();
Three, factory method mode
The factory method pattern (Factory mode), also known as the factory pattern, is called the virtual Constructor mode or the Polymorphic factory (polymorphic Factory) pattern, which belongs to the class-creation pattern. In factory method mode, the factory parent is responsible for defining the public interface that creates the product object, while the factory subclass is responsible for generating the specific product object, which is intended to defer the instantiation of the product class to the factory subclass, i.e., the factory subclass to determine which specific product class should be instantiated.
The factory method pattern consists of four roles: abstract product roles, specific product roles, abstract factory roles, specific factory roles
The specific sample code is implemented as follows:
Abstract Product role: Vehicle public abstract class car {public virtual void gotowork () { } } // Specific product roles: Bicycle public class Bike:car {public override void Gotowork () { Console.WriteLine (" Ride a bike to Work ");} } Specific product role: Public bus class Bus:car {public override void Gotowork () { Console.WriteLine (" Take a bus to work ");} } Abstract Factory role: Factory interface Public interface ifactory { Car bywhatway (); } Specific factory class: Bicycle class public class bikefactory:ifactory {public Car Bywhatway () { return new Bike () ; } } Specific factory class: Public bus class busfactory:ifactory {public Car Bywhatway () { return new bus (); c42/>} }
Client calling Code:
Based on the client-instantiated factory decide how to go to work ifactory factory = new Busfactory (); General notation //Reflection Optimization (busfactory) Assembly.Load ("Current assembly name"). CreateInstance ("Current namespace name. Class name to instantiate") //using System.Reflection; reference namespace //var factoryname = "Busfactory"; Ifactory factory = (busfactory) assembly.load ("Current assembly name"). CreateInstance ("Current namespace name." + factoryname); Car car = factory. Bywhatway (); Car. Gotowork ();
Iv. Abstract Factory Model
Abstract Factory Pattern: Provides an interface to create a series of related or interdependent objects without specifying their specific classes. Abstract Factory mode, also known as the kit mode, belongs to the object creation mode.
The abstract factory model consists of four roles: abstract product roles, specific product roles, abstract factory roles, specific factory roles
The sample code is as follows:
Abstract vehicle public abstract class Car {//name public string carname{get; set;} Public virtual string Gotowork () {return carname; }}//Abstract breakfast class public abstract class Breakfast {//breakfast name public string Foodname {get; set;} Public virtual string Eat () {return foodname; }}//Bike public class Bike:car {public override string Gotowork () {carname = "riding Bicycles "; return carname; }}//Public bus class Suv:car {public override string Gotowork () {carname = "open su V Car "; return carname; }}//Gao GE Breakfast: Sandwich milk public class Sandwichandmilk:breakfast {public override string Eat () { Foodname = "sandwiches and milk"; return foodname; }}//Bitter breakfast: Bun soy milk public class Baozianddoujiang:breakfast {public override string Eat () {foodname = "steamed bun milk"; return foodname; }}//Abstract factory class public abstract class Abstractfactory {//Create Vehicle method public abstract Car Creatcar (); Create breakfast method public abstract breakfast createbreakfast (); }//Specific factory class: Bitter force programmer public class Lowfactory:abstractfactory {public override Car Creatcar () { return new Bike (); } public override breakfast Createbreakfast () {return new Baozianddoujiang (); }}//Specific factory class: High-forcing programmer public class Highfactory:abstractfactory {public override Car Creatcar () { return new Suv (); } public override breakfast Createbreakfast () {return new Sandwichandmilk (); }} public class Coderlove {private car car; private breakfast breakfast; Public Coderlove (abstractfactory FAC) {Car=fac. Creatcar (); Breakfast=fac. Createbreakfast (); } public void Getcoderlove () {Console.WriteLine ("Finish Breakfast" +breakfast.eat () + "," +car. Gotowork () + "go to Work"); } }
Client calls:
Abstractfactory factory=new highfactory (); Coderlove coder=new Coderlove (factory); Coder. Getcoderlove ();
Abstract Factory mode isolates the generation of specific classes so that customers do not need to know what is being created.
Because of this isolation, it becomes relatively easy to replace a specific factory. All of the specific factories implement the common interfaces defined in the abstract factory, so you can change the behavior of the entire software system to some extent by simply changing the instance of the specific factory.
In addition to the above-mentioned bitter programmer, high-force lattice programmer, can also be specific to add a car SUV to eat steamed bun soy milk specific factory, can also be added to ride a bicycle to eat sandwich milk specific factories.
V. Summary
1. This three mode belongs to the object-creating mode.
2, simple Factory mode simple understanding for the client tells the factory what he wants, the factory will give the production of what instance
3, the factory method mode can understand that the client has created an instance of the machine, he wants to what instance of his own production of what instance
4, the most common case of abstract Factory mode is a set of procedures that require multiple sets of data to implement the case
Advanced: Dependency Injection and control inversion (refer to spring's core idea)
Three Scenarios for Factory mode (RPM)