创建型模式之抽象工厂模式Abstract Factory

 

一、认识

抽象工厂模式:提供一个创建一系列或相关依赖对象的接口,而无需要指定他们的具体类

二、基础代码模板

AbstractFactory:为创建抽象产品的操作声明接口。

ConcreteFactory:实现创建具体产品对象的操作。

AbstractProduct:声明一类产品对象的接口。

Product:1.定义要由相应的具体工厂创建的产品对象。2.实现AbstractProduct接口。

Client:使用AbstractFactory和AbstractProduct类声明的接口。

(一)走具体工厂1流程:

1.Program入口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象工厂模式
{
    class Program
    {
        //入口
        static void Main(string[] args)
        {
            // Abstract factory #1走ConcreteFactory1路径
            AbstractFactory factory1 = new ConcreteFactory1();
            Client client1 = new Client(factory1);
            client1.Run();

            // Abstract factory #2走ConcreteFactory2路径
            AbstractFactory factory2 = new ConcreteFactory2();
            Client client2 = new Client(factory2);
            client2.Run();

            // Wait for user input
            Console.ReadKey();
        }
    }
}

 2.Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象工厂模式
{
    //抽象工厂模式提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
    class Client
    {
        private AbstractProductA _abstractProductA;
        private AbstractProductB _abstractProductB;

        // Constructor
        public Client(AbstractFactory factory)
        {
            _abstractProductB = factory.CreateProductB();
            _abstractProductA = factory.CreateProductA();
        }

        public void Run()
        {
            _abstractProductB.Interact(_abstractProductA);//interact相互影响
        }
    }
}

 3.ConcreteFactory1类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象工厂模式
{
    class ConcreteFactory1 : AbstractFactory //具体的工厂,创建具有特定实现的产品对象
    {
        public override AbstractProductA CreateProductA()
        {
            return new ProductA1();
        }
        public override AbstractProductB CreateProductB()
        {
            return new ProductB1();
        }
    }
}

 4.ProductB1类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象工厂模式
{
    class ProductB1:AbstractProductB 
    {
        public override void Interact(AbstractProductA a)
        {
            Console.WriteLine(this.GetType().Name + " interacts with " + a.GetType().Name);
        }
    }
}

(二)走具体工厂2流程

5.ConcreteFactory2类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象工厂模式
{
    class ConcreteFactory2 :  AbstractFactory 
    {
        public override AbstractProductA CreateProductA()
        {
            return new ProductA2();
        }
        public override AbstractProductB CreateProductB()
        {
            return new ProductB2();
        }
    }
}

(三)抽象类

6.AbstractFactory类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象工厂模式
{
   abstract  class AbstractFactory//抽象工厂接口,它里面应该包含所有的产品创建的抽象方法
    {
        public abstract AbstractProductA CreateProductA();
        public abstract AbstractProductB CreateProductB();
    }
}

产品抽象接口:

7.抽象产品A

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象工厂模式
{
    abstract class AbstractProductA//对两个抽象产品的具体分类的实现ProductA1、A2
    {
    }
}

8.抽象产品B 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象工厂模式
{
    abstract class AbstractProductB
    {
        public abstract void Interact(AbstractProductA a);
    }
}

(四)产品类

9.产品A1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象工厂模式
{
    class ProductA1 : AbstractProductA //对两个抽象产品的具体分类的实现,ProductA1、A2实现抽象产品A
    {
    }
}

10.产品A2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象工厂模式
{
    class ProductA2 : AbstractProductA
    {
    }
}

11.产品B1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象工厂模式
{
    class ProductB1:AbstractProductB 
    {
        public override void Interact(AbstractProductA a)
        {
            Console.WriteLine(this.GetType().Name + " interacts with " + a.GetType().Name);
        }
    }
}

12.产品B2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象工厂模式
{
    class ProductB2 : AbstractProductB
    {
        public override void Interact(AbstractProductA a)
        {
            Console.WriteLine(this.GetType().Name + " interacts with " + a.GetType().Name);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值