Concept: define an interface for creating objects, so that subclasses decide which class to instantiate, and the factory method defers the instantiation of a class to its subclasses.
Second, the Code implementation
namespaceFactory method Pattern of design pattern {/// <summary> ///Vegetable Abstract class/// </summary> Public Abstract classFood {//What's the point of the output? Public Abstract voidPrint (); } /// <summary> ///tomato scrambled egg this dish/// </summary> Public classTomatoscrambledeggs:food { Public Override voidPrint () {Console.WriteLine ("scrambled eggs with tomatoes, please! "); } } /// <summary> ///potato, shredded pork, this dish ./// </summary> Public classShreddedporkwithpotatoes:food { Public Override voidPrint () {Console.WriteLine ("potatoes, shredded pork."); } } /// <summary> ///Abstract Factory class/// </summary> Public Abstract classCreator {//Factory Method Public AbstractFood createfoddfactory (); } /// <summary> ///Tomato Scrambled Egg factory class/// </summary> Public classTomatoscrambledeggsfactory:creator {/// <summary> ///is responsible for creating tomato scrambled egg this course/// </summary> /// <returns></returns> Public OverrideFood createfoddfactory () {return NewTomatoscrambledeggs (); } } /// <summary> ///potato and shredded pork factory class/// </summary> Public classShreddedporkwithpotatoesfactory:creator {/// <summary> ///responsible for the creation of potato shredded pork this dish/// </summary> /// <returns></returns> Public OverrideFood createfoddfactory () {return Newshreddedporkwithpotatoes (); } } /// <summary> ///Client Calls/// </summary> classClient {Static voidMain (string[] args) { //initialize two plants for cooking ()Creator shreddedporkwithpotatoesfactory =Newshreddedporkwithpotatoesfactory (); Creator tomatoscrambledeggsfactory=Newtomatoscrambledeggsfactory (); //start making tomato scrambled eggsFood Tomatoscrambleeggs =tomatoscrambledeggsfactory.createfoddfactory (); Tomatoscrambleeggs.print (); //start making shredded potatoes.Food Shreddedporkwithpotatoes =shreddedporkwithpotatoesfactory.createfoddfactory (); Shreddedporkwithpotatoes.print (); Console.read (); } } }
Three, the plant model four elements
- Factory interface. The factory interface is the core of the factory method pattern, which interacts directly with the caller to provide the product. In practical programming, an abstract class is sometimes used as an interface to interact with the caller, essentially the same.
- Factory implementation. In programming, the factory implementation decides how to instantiate the product, is the way to realize the expansion, how many kinds of products need to have, how many concrete factories need to implement.
- Product interface. The main purpose of the product interface is to define the product specification, and all product implementations must follow the specification of the product interface definition. Product interface is the most concern of the caller, the product interface definition directly determines the stability of the caller code. Similarly, product interfaces can be replaced with abstract classes, but it is best not to violate the Richter scale substitution principle.
- Product realization. The specific class that implements the product interface determines the specific behavior of the product in the client.
C # Design pattern (3)--factory method mode