First, Introduction
One of the drawbacks of simple factory models in a simple factory is that simple Factory mode systems are hard to scale, and once you add new products you have to modify simple factory methods, which can complicate the implementation logic of a simple factory, but the factory method model presented in this topic solves this problem in the simple factory model. Here is a detailed look at how the factory model solves the problem.
Second, the implementation of the factory method model
The factory method pattern solves the simple factory pattern because its implementation defers the creation of the specific product to the subclass, where the factory class is no longer responsible for all product creation, but only the interfaces that the specific factory must implement, so that the factory method pattern allows the system to add new products without modifying the factory class logic. , which overcomes the drawbacks of the simple factory model. Here's a look at the specific implementation code for the factory model (this is also the example of a la carte in Simple Factory mode):
<summary>///Menu Abstract object///</summary> public abstract class Food {//output point what dish public abstract void Print (); }//<summary>//fried potato wire///</summary> public class Friedpotatoshreds:food {public O verride void Print () {Console.WriteLine ("Fried shredded potatoes"); }}///<summary>//Pepper scrambled eggs///</summary> public class Pepperfriedegg:food {Publi c override void Print () {Console.WriteLine ("Pepper Scrambled eggs!"); }}///<summary>//tomato scrambled eggs////</summary> public class Scrambledeggtomato:food { public override void Print () {Console.WriteLine ("A tomato scrambled egg!") "); }}///<summary>///Abstract factory class///</summary> public abstract class Creator {//factory method Public abstract food createfoddfactory (); }///<summary>//Fried potato mills///</summary> public class Friedpotatoshredsfactory:creator {public override food createfoddfactory () { return new Friedpotatoshreds (); }}///<summary>//Pepper Scrambled egg factory///</summary> public class Pepperfriedeggfactory:creator { public override food Createfoddfactory () {return new Pepperfriedegg (); }}///<summary>//Tomato Scrambled egg factory///</summary> public class Scrambledeggtomatofactory:creator {public override Food createfoddfactory () {return new Scrambledeggtomato (); } }
<summary> ///Client call//</summary> class Client { static void Main (string[] args) { //Initialize two plants for cooking () Creator scrambledeggtomatofactory = new Scrambledeggtomatofactory (); Creator pepperfriedeggfactory = new Pepperfriedeggfactory (); Start making tomato scrambled egg food Tomatoscrambleeggs = Pepperfriedeggfactory.createfoddfactory (); Tomatoscrambleeggs.print (); Start making potato meat food shreddedporkwithpotatoes = Scrambledeggtomatofactory.createfoddfactory (); Shreddedporkwithpotatoes.print (); Console.read (); } }
Systems implemented using factory methods, if the system needs to add new products, we can use polymorphism to complete the expansion of the system, there is no need to make any changes to the abstract factory class and the code in the specific factory. For example, we also want to order a "minced meat eggplant", at this time we only need to define a meat eggplant specific factory class and minced meat eggplant class can be. Instead of modifying the implementation in the factory class as in simple Factory mode (specifically, adding case statements). The specific code is:
<summary>///minced eggplant this dish///</summary> public class Mincedmeateggplant:food {//<s ummary>///rewrite method in abstract class///</summary> public override void Print () {Consol E.writeline ("Minced meat eggplant good"); }}///<summary>//minced Eggplant factory class, responsible for creating minced meat eggplant this dish////</summary> public class Mincedmeateggplantfactory: Creator {//<summary>////For creating minced eggplant This course///</summary>//<returns></ returns> public override Food Createfoddfactory () {return new mincedmeateggplant (); }}///<summary>//Client call//</summary> class Client {static void Main (string[] A RGS) {//If the customer wants to order minced meat and eggplant again//and then initialize another minced meat eggplant factory Creator Mincemeateggplantfacto R = new Mincedmeateggplantfactory (); Use minced meat eggplant factory to create minced meat eggplant this dish food mincemeateggplant = MincemeateggPlantfactor.createfoddfactory (); Mincemeateggplant.print (); Console.read (); } }
Definition of Plant Method model
Defines an interface for creating objects, letting subclasses decide which class to instantiate. The factory method pattern defers the instantiation of a class to its subclasses.
Structure of the factory method pattern
Summary: Factory method mode
Defines an interface for creating objects, letting subclasses decide which class to instantiate. The factory method pattern defers the instantiation of a class to its subclasses.
Factory method-Participants
C # Design Pattern-factory method mode