您好,欢迎访问一九零五行业门户网

C# 设计模式之 工厂模式

把创建对象的事情  封装起来
using system; using system.collections.generic; using system.linq; using system.text; namespace designpytterndemo { /// <summary> /// 简单工厂 /// </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> /// 抽象工厂 /// </summary> public interface iactiongame { } public class 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(); } } }
以上就是c# 设计模式之 工厂模式的内容。
其它类似信息

推荐信息