wrap up the things that create objects
Using system;using system.collections.generic;using system.linq;using system.text;namespace DesignPytternDemo{//& Lt;summary>///Simple factory///</summary> public interface Ifood {int. price {get;} } public class Orange:ifood {public orange () {Console.WriteLine ("Orange created"); } public int Price {get {return 1; }}} public class Rice:ifood {public rice () {Console.WriteLine ("Rice created "); } public int Price {get {return 3; }}} public static class Foodfactory {public static Ifood Createfood (string foodtype) { Ifood f = null; Switch (foodtype) {case "o": F = new Orange (); Break Case "R": f = new Rice (); Break Default:break; } return F; }}///<summary>//Abstract Factory///</summary> public interface Iactiongame {} public Clas s Kof:iactiongame {public Kof () {Console.WriteLine ("Kof created"); }} public class War3:iactiongame {public War3 () {Console.WriteLine ("War3 created"); }} public class Cs:iactiongame {public cs () {Console.WriteLine ("CS created") ); }} public interface IRPG {} public class Menghuan:irpg {public Menghuan () { Console.WriteLine ("Menghuan created"); }} public class Legend:irpg {public Legend () {Console.WriteLine ("Legend created"); }} public class Diablo:irpg {public Diablo () { Console.WriteLine ("Diablo created"); }} public abstract class Gamefactory {public abstract iactiongame createactiongame (); Public abstract IRPG createrpggame (); } public class Mygamefactory:gamefactory {public override Iactiongame Createactiongame () { return new Kof (); } public override Irpg Createrpggame () {return new Legend (); } }}
The above is the C # design mode of the factory model content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!