设计模式-抽象工厂
程序员文章站
2022-07-01 23:31:10
抽象工厂:创建对象,创建一组密不可分的对象 创建产品簇,多个对象是个整体,不可分割 工厂+约束 倾斜的可扩展性设计,增加产品很麻烦 /// /// War3种族之一 /// public class Human : IRace { public Human( ......
抽象工厂:创建对象,创建一组密不可分的对象
创建产品簇,多个对象是个整体,不可分割
工厂+约束
倾斜的可扩展性设计,增加产品很麻烦
/// <summary> /// 一个工厂负责一些产品的创建 /// 产品簇 /// 单一职责就是创建完整的产品簇 /// /// 继承抽象类后,必须显式的override父类的抽象方法 /// </summary> public abstract class factoryabstract { public abstract irace createrace(); public abstract iarmy createarmy(); public abstract ihero createhero(); public abstract iresource createresource(); //public abstract iluck createluck(); }
/// <summary> /// 一个工厂负责一些产品的创建 /// </summary> public class humanfactory : factoryabstract { public override irace createrace() { return new human(); } public override iarmy createarmy() { return new humanarmy(); } public override ihero createhero() { return new humanhero(); } public override iresource createresource() { return new humanresource(); } }
/// <summary> /// 一个工厂负责一些产品的创建 /// </summary> public class orcfactory : factoryabstract { public override irace createrace() { return new orc(); } public override iarmy createarmy() { return new orcarmy(); } public override ihero createhero() { return new orchero(); } public override iresource createresource() { return new orcresource(); } }
/// <summary> /// 一个工厂负责一些产品的创建 /// </summary> public class undeadfactory : factoryabstract { public override irace createrace() { return new undead(); } public override iarmy createarmy() { return new undeadarmy(); } public override ihero createhero() { return new undeadhero(); } public override iresource createresource() { return new undeadresource(); } }
{ irace race = new undead(); iarmy army = new undeadarmy(); ihero hero = new undeadhero(); iresource resource = new undeadresource(); //1 对象转移,屏蔽细节,让使用者更轻松 //2 对象簇的工厂 } { //system.data.sqlclient.sqlclientfactory factoryabstract undeadfactory = new undeadfactory(); irace race = undeadfactory.createrace();// new undead(); iarmy army = undeadfactory.createarmy();//new undeadarmy(); ihero hero = undeadfactory.createhero();//new undeadhero(); iresource resource = undeadfactory.createresource();//new undeadresource(); } { factoryabstract humanfactory = new humanfactory(); irace race = humanfactory.createrace(); iarmy army = humanfactory.createarmy(); ihero hero = humanfactory.createhero(); iresource resource = humanfactory.createresource(); } { factoryabstract humanfactory = new orcfactory(); irace race = humanfactory.createrace(); iarmy army = humanfactory.createarmy(); ihero hero = humanfactory.createhero(); iresource resource = humanfactory.createresource(); }
/// <summary> /// war3种族之一 /// </summary> public class human : irace { public human(int id, datetime datetime, string reamrk) { } public human() { } public void showking() { console.writeline("the king of {0} is {1}", this.gettype().name, "sky"); } } public class humanarmy : iarmy { public void showarmy() { console.writeline("the army of {0} is {1}", this.gettype().name, "footman,火枪,骑士,狮鹫"); } } public class humanhero : ihero { public void showhero() { console.writeline("the army of {0} is {1}", this.gettype().name, "大法师、山丘、圣骑士、血法师"); } } public class humanresource : iresource { public void showresource() { console.writeline("the army of {0} is {1}", this.gettype().name, "1000g1000w"); } }
上一篇: 2.预科-走进计算机