一、抽象工厂模式(Abstract Factory)简介
抽象工厂模式(Abstract Factory)是以一个超级工厂创建其他工厂。它属于创建型模式。抽象工厂模式是工厂方法模式的升级版本,他用来创建一组相关或者相互依赖的对象。他与工厂方法模式的区别就在于,工厂方法模式针对的是一个产品等级结构;而抽象工厂模式则是针对的多个产品等级结构。
二、抽象工厂模式(Abstract Factory)的优点
抽象工厂模式将具体产品的创建延迟到具体工厂的子类中,这样将对象的创建封装起来,可以减少客户端与具体产品类之间的依赖,从而使系统耦合度低,这样更有利于后期的维护和扩展
三、抽象工厂模式(Abstract Factory)的缺点
抽象工厂模式很难支持新种类产品的变化。这是因为抽象工厂接口中已经确定了可以被创建的产品集合,如果需要添加新产品,此时就必须去修改抽象工厂的接口,这样就涉及到抽象工厂类的以及所有子类的改变,这就违背了"开闭原则"。
四、抽象工厂模式(Abstract Factory)适用场景
使用抽象工厂模式的系统应该符合以下几个条件:
①一个系统不要求依赖产品实例如何被创建、组合和表达
②这个系统有多个系列产品,而系统中只消费其中某一系列产品
③系统要求提供一个产品类的库,所有产品以同样的接口出现,客户端不需要依赖具体实现
五、抽象工厂模式(Abstract Factory)举例
举一个生活例子来形象地描述抽象工厂模式。某一个食品企业,在全国均有分店,它的产品有"产品A"和"产品B",由于北方和南方的口味并不一样,所以产品的生产会有所不同。
六、抽象工厂模式(Abstract Factory)实现
抽象工厂类
using System.Collections;using System.Collections.Generic;using UnityEngine;//定义抽象工厂public abstract class AbstractFactory{public abstract ProductAAbstract CreateProductAManager();public abstract ProductBAbstract CreateProductBManager();}
抽象产品类
using System.Collections;using System.Collections.Generic;using UnityEngine;//定义产品A抽象类public abstract class ProductAAbstract{public abstract void ProductionLine(string where);}抽象产品类using System.Collections;using System.Collections.Generic;using UnityEngine;//定义产品B抽象类public abstract class ProductBAbstract{public abstract void ProductionLine(string where);}
具体工厂类
using System.Collections;using System.Collections.Generic;using UnityEngine;//北方的工厂public class NorthManagerFactory : AbstractFactory{public override ProductAAbstract CreateProductAManager(){return new ProductAManager();}public override ProductBAbstract CreateProductBManager(){return new ProductBManager();}}
具体工厂类
using System.Collections;using System.Collections.Generic;using UnityEngine;//南方的工厂public class SouthManagerFactory : AbstractFactory{public override ProductAAbstract CreateProductAManager(){return new ProductAManager();}public override ProductBAbstract CreateProductBManager(){return new ProductBManager();}}
具体产品类
using System.Collections;using System.Collections.Generic;using UnityEngine;//定义产品A抽象类的具体产品public class ProductAManager : ProductAAbstract{public override void ProductionLine(string where){Debug.Log("商品A的口味:" + where);}}
具体产品类
using System.Collections;using System.Collections.Generic;using UnityEngine;//定义产品B抽象类的具体产品public class ProductBManager : ProductBAbstract{public override void ProductionLine(string where){Debug.Log("商品B的口味:" + where);}}
测试
using System.Collections;using System.Collections.Generic;using UnityEngine;public class AbstractFactoryTest : MonoBehaviour{void Start(){AbstractFactory northManagerFactory = new NorthManagerFactory();ProductAAbstract aProductManager = northManagerFactory.CreateProductAManager();aProductManager.ProductionLine("北方");ProductBAbstract bProductManager = northManagerFactory.CreateProductBManager();bProductManager.ProductionLine("北方");//AbstractFactory southManagerFactory = new SouthManagerFactory();//ProductAAbstract aProductManager = southManagerFactory.CreateProductAManager();//aProductManager.ProductionLine("南方");//ProductBAbstract bProductManager = southManagerFactory.CreateProductBManager();//bProductManager.ProductionLine("南方");}}