一、认识
抽象工厂模式:提供一个创建一系列或相关依赖对象的接口,而无需要指定他们的具体类
二、基础代码模板
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);
}
}
}