Or doing an example in a fast food restaurant, the simple factory model is equivalent to the family workshop. With the progress of society, the division of labor is thinner, all fast food is no longer a Master, but there is a face teacher, rice teacher (huh, I don't know Without this, anyway, everyone understands it), the bakers are responsible for special food production. This is the factory model, please see the example below:
Using system;
Namespace factoryPattern {///
// Call example nchef = new noodlchef (); nChef.makefood (); rchef = new ricechef (); rchef.makefood (); bchef = new bowchef (); bchef.makefood (); bchef.makefood (); bchef.makefood (); bchef.makefood (); bchef.makefood ();
Console.readline ();}} class badfoodexception: system.exception {public badfoodexception (string strmsg) {console.writeline (strmsg);}}
/ / Define Various Fast Food Public Interface Food {// Cooking Void Cook (); // Sell Void Sell ();
PUBLIC CLASS NOODLE: FOOD {public noodle () {console.writeline ("/ nthe noodle isbook ..");} private void cook () {console.writeline ("/ nnoodle is cooking ... ");} Public void sell () {console.writeline (" / nnotle HAS BEEN SOLD ... ");} public int price {get {return this.price;} set {price = value;}}} public class Rice: food {public {public {public {"/ nthe rice isbook ..");} private void cook () {console.writeLine ("/ nrice is cooking ..."); } Public void self () {console.writeLine ("/ nrice has been sild ...");} public int price {get {return this.price;}}} public class break: food {Console.WriteLine ("/ NTHE BREAD IS MADE ...."); Public void cook () {console.writeline ("/ nbread is cooking ...");} public void sell () {console.writeLine ("/ nbread has been silver ...");} public int price {Get {Return this.price;} set {price = value;}}} // Define the chef interface and define the stroke staff, rice workers, and bakers.
Public interface chef {
/ / Cooking Food Makefood ();
// Noodlers are responsible for making noodles Class Noodlchef: chef {public food makefood () {return new noodle ();}}
// 米 饭 负 is responsible for rice class ricechef: chef {public food makefood () {return new rice ();}}
// Bakery is responsible for making a bread Class Breadchef: Chef {public food makefood () {return new break ();}}}