Reference
"Java and Mode"
The example on the above book is the example of gardener and orchard. Learning design model is best looking for an example in life.
Practice, the following is an example of my own, is the example of the fast food restaurant, the fast food store offers a lot of food, such as
Noodles, rice, bread. First define a FOOD interface, then these foods are inherited from it, defining a chef
He packaged all the production of food, this is the concept of the simple factory model I understand, the following is the source code:
Using system;
Namespace SimpleActoryPattern {///
}
// Noodle
Public class noodle: food {public noodle () {console.writeline ("/ nthe noodle isbook ..");} private int price; // noodle NOODLE Cook method interface implementation public void cook () {console.writeline "/ nnoodle is cooking ...");
// Noodle Noodle's SELL method interface implements public void sell () {console.writeline ("/ nnoodle has been sild ...");} public int price {get {return this.price;} set {price = value }}}
// Rice public class rice: food {public rice () {console.writeline ("/ nthe rice isbook ..");} private void cook () {console.writeline ("/ nRice is cooking. .. ");} public void sell () {Console.writeline (" / nrice has been silver ... ");} public int price {get {return this.price;}}}}
// BREAD PUBLIC CLASS BREAD: Food {Public Bread () {Console.Writeline ("/ NTHE BREAD IS MADE ....");} private void cook () {console.writeline ("/ NBREAD IS Cooking ... ");} public void sell () {console.writeline (" / nbread has been sild ... ");} public int price {get {return this.price;} set {price = value;} }} // Define the chef, all of the Food in this fast food restaurant, including noodles, bread and rice class chef {public static food, {try, {switch (foodname) {CASE "noodle": return new Noodle (); Case "Rice": return new rice (); case "break": return new break (); default: throw new badfo ODEXCEPTION ("Bad Food Request!");}} Catch (BadfoodException E) {throw E;}}}
// Abnormal class, the restaurant does not have food class badfoodexception: system.exception {public badfoodException (string strmsg) {console.writeline (strmsg);}}
///