using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMode { public abstract class NewAbstractFactory { public abstract HightPower CreateHightProduct(); public abstract LowPower CreateLowProduct(); public abstract HightFrequenty CreateFrequenty(); } public class ElectricProduction { public int Intitale() { return 0; } } /// <summary> /// 高压产品 /// </summary> public abstract class HightPower { public abstract int Open(); public abstract void ProductSerialNum(string strSerial); } /// <summary> /// 低压产品 /// </summary> public abstract class LowPower { public abstract int Open(); public abstract void ProductSerialNum(string strSerial); } /// <summary> /// 高频产品 /// </summary> public abstract class HightFrequenty { public abstract int Open(); public abstract void ProductSerialNum(string strSerial); } //+++++++++++++++++++++++++++++++ //实际需求产品工厂 public class ABBFactory : NewAbstractFactory { public override HightFrequenty CreateFrequenty() { throw new NotImplementedException(); } public override HightPower CreateHightProduct() { return new ABBHightPower(); } public override LowPower CreateLowProduct() { return new ABBLowPower(); } } public class SiemensFactory : NewAbstractFactory { public override HightFrequenty CreateFrequenty() { throw new NotImplementedException(); } public override HightPower CreateHightProduct() { return new SiemensHightPower(); } public override LowPower CreateLowProduct() { return new SiemensLowPower(); } } //+++++++++++++++++++++++++++++++ /// <summary> /// ABB高压产品 /// </summary> public class ABBHightPower : HightPower { string _strProductSerial = ""; public override int Open() { Console.WriteLine(_strProductSerial); return 0; } public override void ProductSerialNum(string strSerial) { _strProductSerial = strSerial; } } /// <summary> /// ABB低压产品 /// </summary> public class ABBLowPower :LowPower { string _strProductSerial = ""; public override int Open() { Console.WriteLine(_strProductSerial); return 0; } public override void ProductSerialNum(string strSerial) { _strProductSerial = strSerial; } } /// <summary> /// 西门子高压产品 /// </summary> public class SiemensHightPower : HightPower { string _strProductSerial = ""; public override int Open() { Console.WriteLine(_strProductSerial); return 0; } public override void ProductSerialNum(string strSerial) { _strProductSerial = strSerial; } } /// <summary> /// 西门子低压产品 /// </summary> public class SiemensLowPower : LowPower { string _strProductSerial = ""; public override int Open() { Console.WriteLine(_strProductSerial); return 0; } public override void ProductSerialNum(string strSerial) { _strProductSerial = strSerial; } } }
客户端使用:
List<HightPower> hightPowerProductions = new List<HightPower>();
List<LowPower> lowPowerProductions = new List<LowPower>();
//+++++++++++++++++++++++
ABBFactory aBBFactory = new ABBFactory();
SiemensFactory siemensFactory = new SiemensFactory();
List<LowPower> lowPowerProductions = new List<LowPower>();
//+++++++++++++++++++++++
ABBFactory aBBFactory = new ABBFactory();
SiemensFactory siemensFactory = new SiemensFactory();
hightPowerProductions.Add(aBBFactory.CreateHightProduct());
hightPowerProductions.Add(siemensFactory.CreateHightProduct());
lowPowerProductions.Add(aBBFactory.CreateLowProduct());
lowPowerProductions.Add(siemensFactory.CreateLowProduct());
lowPowerProductions[0].ProductSerialNum("");
lowPowerProductions.Add(siemensFactory.CreateLowProduct());
lowPowerProductions[0].ProductSerialNum("");
Console.ReadKey();
抽象工厂的优点:
抽象工厂模式将具体产品的创建延迟到具体工厂的子类中,这样将对象的创建封装起来,可以减少客户端与具体产品类之间的依赖,从而使系统耦合度低,这样更有利于后期的维护和扩展,这真是抽象工厂模式的优点所在,然后抽象模式同时也存在不足的地方;
缺点:
抽象工厂模式很难支持新种类产品的变化。这是因为抽象工厂接口中已经确定了可以被创建的产品集合,如果需要添加新产品,此时就必须去修改抽象工厂的接口,这样就涉及到抽象工厂类的以及所有子类的改变,这样也就违背了“开发——封闭”原则。
知道了抽象工厂的优缺点之后,也就能很好地把握什么情况下考虑使用抽象工厂模式了,下面就具体看看使用抽象工厂模式的系统应该符合那几个前提:
- 一个系统不要求依赖产品类实例如何被创建、组合和表达的表达,这点也是所有工厂模式应用的前提。
- 这个系统有多个系列产品,而系统中只消费其中某一系列产品
- 系统要求提供一个产品类的库,所有产品以同样的接口出现,客户端不需要依赖具体实现。