设计模式之笔记--抽象工厂模式(Abstract Factory)

抽象工厂模式(Abstract Factory)

类图

描述

抽象工厂:

多个抽象产品类,每个抽象产品类可以派生出多个具体产品类;
一个抽象工厂类,可以派生出多个具体工厂类;
每个具体工厂可以创建多个具体产品,即每个工厂可以生产一个产品集合。

应用场景

      就拿生产轿车来说,轿车是由发动机、车轮、车体结构、方向盘等部件组成的,现在要生产一辆轿车,于是可以把各部件抽象出来单独封装,再由汽车工厂来组装轿车。

发动机接口

    public interface IEngine
    {
        void Install();
    }

发动机类

    public class AudiEngine : IEngine
    {
        public void Install()
        {
            Console.WriteLine("安装奥迪的发动机.");
        }
    }

    public class BenzEngine : IEngine
    {
        public void Install()
        {
            Console.WriteLine("安装奔驰的发动机.");
        }
    }

车体结构接口

    public interface IBody
    {
        void Install();
    }

车体结构类

    public class AudiBody : IBody
    {
        public void Install()
        {
            Console.WriteLine("安装奥迪的车身结构.");
        }
    }

    public class BenzBody : IBody
    {
        public void Install()
        {
            Console.WriteLine("安装奔驰的车身结构.");
        }
    }

车轮接口

    public interface IWheel
    {
        void InstallLeftFrontWheel();
        void InstallRightFrontWheel();
        void InstallLeftBackWheel();
        void InstallRightBackWheel();
    }

车轮类

    public class AudiWheel : IWheel
    {
        public void InstallLeftFrontWheel()
        {
            Console.WriteLine("安装奥迪的左前车轮.");
        }

        public void InstallRightFrontWheel()
        {
            Console.WriteLine("安装奥迪的右前车轮.");
        }

        public void InstallLeftBackWheel()
        {
            Console.WriteLine("安装奥迪的左后车轮.");
        }

        public void InstallRightBackWheel()
        {
            Console.WriteLine("安装奥迪的右后车轮.");
        }
    }

    public class BenzWheel : IWheel
    {
        public void InstallLeftFrontWheel()
        {
            Console.WriteLine("安装奔驰的左前车轮.");
        }

        public void InstallRightFrontWheel()
        {
            Console.WriteLine("安装奔驰的右前车轮.");
        }

        public void InstallLeftBackWheel()
        {
            Console.WriteLine("安装奔驰的左后车轮.");
        }

        public void InstallRightBackWheel()
        {
            Console.WriteLine("安装奔驰的右后车轮.");
        }
    }

汽车工厂接口

    public interface ICarFactory
    {
        IBody CreateBody();
        IEngine CreateEngine();
        IWheel CreateWheel();
    }

汽车工厂类

    public class AudiFactory : ICarFactory
    {
        public IEngine CreateEngine()
        {
            return new AudiEngine();
        }

        public IWheel CreateWheel()
        {
            return new AudiWheel();
        }

        public IBody CreateBody()
        {
            return new AudiBody();
        }
    }

    public class BenzFactory : ICarFactory
    {
        public IEngine CreateEngine()
        {
            return new BenzEngine();
        }

        public IWheel CreateWheel()
        {
            return new BenzWheel();
        }

        public IBody CreateBody()
        {
            return new BenzBody();
        }
    }

调用

            string value = ConfigurationManager.AppSettings["abstractFactoryPattern"];
            ICarFactory factory = (ICarFactory)Assembly.Load(value.Substring(0, value.IndexOf('.'))).CreateInstance(value);
            IBody body = factory.CreateBody();
            body.Install();
            IEngine engine = factory.CreateEngine();
            engine.Install();
            IWheel wheel = factory.CreateWheel();
            wheel.InstallLeftFrontWheel();
            wheel.InstallRightFrontWheel();
            wheel.InstallLeftBackWheel();
            wheel.InstallRightBackWheel();

 

你可能感兴趣的:(设计模式之笔记--抽象工厂模式(Abstract Factory))