标签:task pre 耦合 表达 pac dash exception 接口 ctr
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; } } }
抽象工厂模式将具体产品的创建延迟到具体工厂的子类中,这样将对象的创建封装起来,可以减少客户端与具体产品类之间的依赖,从而使系统耦合度低,这样更有利于后期的维护和扩展,这真是抽象工厂模式的优点所在,然后抽象模式同时也存在不足的地方;
缺点:
抽象工厂模式很难支持新种类产品的变化。这是因为抽象工厂接口中已经确定了可以被创建的产品集合,如果需要添加新产品,此时就必须去修改抽象工厂的接口,这样就涉及到抽象工厂类的以及所有子类的改变,这样也就违背了“开发——封闭”原则。
知道了抽象工厂的优缺点之后,也就能很好地把握什么情况下考虑使用抽象工厂模式了,下面就具体看看使用抽象工厂模式的系统应该符合那几个前提:
标签:task pre 耦合 表达 pac dash exception 接口 ctr
原文地址:https://www.cnblogs.com/SoftZoro20181229/p/12830127.html