具体参考抽象工厂(AbstractFactory)模式-创建型模式,本文在FactoryMethod工厂方法模式(创建型模式)的基础上进行业务进一步抽象,不做详细原理介绍.
1、在FactoryMethod工厂方法模式(创建型模式)的基础上,提出了一个代码上的需求,需要对前面的风格进行抽象,将相同风格的道路和房屋工厂类型进行抽象,这就是抽象工厂,代码如下:
#region 抽象工厂 ///// <summary> ///// 抽象的游戏建造系统 ///// </summary> public class BuildSystem { /// <summary> /// 具体的构建方法,Build方法的逻辑变化较慢(只需要创建2种风格的房屋和道路,总共8个对象),但是风格变化较快,由于需求变化,可能需要创建诸如卡通风格、另类风格等的房屋和道路 /// </summary> public void Build(AbstractFactory cartoonFactory, AbstractFactory alternativeFactory) { House HFirstStyleOne = cartoonFactory.CreateHouse(); Console.WriteLine(HFirstStyleOne.ShowHouseStyle()); House HFirstStyleTwo = cartoonFactory.CreateHouse(); Console.WriteLine(HFirstStyleTwo.ShowHouseStyle()); Road RFirstStyleOne = cartoonFactory.CreateRoad(); Console.WriteLine(RFirstStyleOne.ShowRoadStyle()); Road RFirstStyleTwo = cartoonFactory.CreateRoad(); Console.WriteLine(RFirstStyleTwo.ShowRoadStyle()); House HSecondStyleOne = alternativeFactory.CreateHouse(); Console.WriteLine(HSecondStyleOne.ShowHouseStyle()); House HSecondStyleTwo = alternativeFactory.CreateHouse(); Console.WriteLine(HSecondStyleTwo.ShowHouseStyle()); Road RSecondStyleOne = alternativeFactory.CreateRoad(); Console.WriteLine(RSecondStyleOne.ShowRoadStyle()); Road RSecondTwo = alternativeFactory.CreateRoad(); Console.WriteLine(RSecondTwo.ShowRoadStyle()); } } #region 抽象 public abstract class House { public abstract string ShowHouseStyle(); } public abstract class Road { public abstract string ShowRoadStyle(); } #endregion #region 抽象工厂 public abstract class AbstractFactory { public abstract House CreateHouse(); public abstract Road CreateRoad(); } #endregion #region 具体抽象工厂-负责系列对象的创建 public class ModernFactory : AbstractFactory { public override House CreateHouse() { return new ModernHouse(); } public override Road CreateRoad() { return new ModernRoad(); } } public class ClassicalFactory : AbstractFactory { public override House CreateHouse() { return new ClassicalHouse(); } public override Road CreateRoad() { return new ClassicalRoad(); } } public class CartoonFactory : AbstractFactory { public override House CreateHouse() { return new CartoonHouse(); } public override Road CreateRoad() { return new CartoonRoad(); } } public class AlternativeFactory : AbstractFactory { public override House CreateHouse() { return new AlternativeHouse(); } public override Road CreateRoad() { return new AlternativeRoad(); } } #endregion #region 具体的实现 public class ModernHouse : House { public override string ShowHouseStyle() { return "Modern现代化风格房屋"; } } public class ModernRoad : Road { public override string ShowRoadStyle() { return "Modern现代化风格道路"; } } public class ClassicalHouse : House { public override string ShowHouseStyle() { return "Classical古典化风格房屋"; } } public class ClassicalRoad : Road { public override string ShowRoadStyle() { return "Classical古典化风格道路"; } } public class CartoonHouse : House { public override string ShowHouseStyle() { return "Cartoon卡通化风格房屋"; } } public class CartoonRoad : Road { public override string ShowRoadStyle() { return "Cartoon卡通化风格道路"; } } public class AlternativeHouse : House { public override string ShowHouseStyle() { return "Alternative另类化风格房屋"; } } public class AlternativeRoad : Road { public override string ShowRoadStyle() { return "Alternative另类化风格道路"; } } #endregion #endregion
客户端调用代码如下:
static void Main(string[] args) { #region 抽象工厂 BuildSystem buildSystem = new BuildSystem(); buildSystem.Build(new CartoonFactory(), new AlternativeFactory()); #endregion Console.ReadKey(); }
ok,根据输出可以发现,输出可以发现,抽象工厂也可以很好的解决提出的需求,而且,对工厂模式中的工厂类进行抽象,减少了工厂类.提供了代码的扩展性和可读性的同时,精简了代码,如果想继续减少工厂类,让代码保持前面的优势的同时,精简代码请参考:原型模式(创建型模式)