抽象工厂(Abstract Factory)模式 不同于简单工厂和工厂方法,抽象工厂用于创建一个产品族,
即抽象工厂模式用于创建一系列类,这些类在业务上有关联。
我们来举个快餐店的例子。
场景:快餐店的套餐分为汉堡和饮料,汉堡可以选鸡肉汉堡,鱼肉汉堡,巨无霸等等,
饮料可以选咖啡,可乐,橙汁等等。
对于业务来说,汉堡和饮料分别可以被抽象为两类产品,因此,我们可以建立2个接口。
汉堡可以有2个方法,一个是打印汉堡名,一个是打印价钱,如下:
{
public interface IHamburger
{
void printName();
void printCost();
}
}
饮料接口除了汉堡该有的方法外,由于我们可以选热饮或冷饮,热饮像咖啡我们可以加糖,或者冷饮我们可以加冰块。
所以,饮料接口必须有另外一个方法叫putAddition,如下:
{
public interface IDrink
{
void printName();
void printCost();
void putAddition();
}
}
接下来,我们便可以设计汉堡和饮料的系列产品了。
汉堡类产品如下:
namespace designpattern.cs.AbstracFactory
{
public class FishHamburger:IHamburger
{
public void printName()
{
Console.Write("Fish Hamburger : ");
}
public void printCost()
{
Console.WriteLine("$1.5");
}
}
}
using System;
namespace designpattern.cs.AbstracFactory
{
public class ChickenHamburger : IHamburger
{
public void printName()
{
Console.Write("Chicken Hamburger : ");
}
public void printCost()
{
Console.WriteLine("$1.8");
}
}
}
using System;
namespace designpattern.cs.AbstracFactory
{
public class SuperHamburger : IHamburger
{
public void printName()
{
Console.Write("Super Hamburger : ");
}
public void printCost()
{
Console.WriteLine("$2");
}
}
}
饮料类产品如下:
我们可以创建有糖或者无糖的咖啡,也可以创建加冰块或者不加冰块的可乐。
namespace designpattern.cs.AbstracFactory
{
public class CoffeeWithoutSugar:IDrink
{
public void printName()
{
Console.Write("Coffee");
}
public void printCost()
{
Console.WriteLine(" $0.9");
}
public void putAddition()
{
Console.Write(" Without Sugar");
}
}
}
using System;
namespace designpattern.cs.AbstracFactory
{
public class CoffeeWithSugar:IDrink
{
public void printName()
{
Console.Write("Coffee");
}
public void printCost()
{
Console.WriteLine(" $1");
}
public void putAddition()
{
Console.Write(" With Sugar");
}
}
}
using System;
namespace designpattern.cs.AbstracFactory
{
public class CokeWithIce:IDrink
{
public void printName()
{
Console.Write("Coke");
}
public void printCost()
{
Console.WriteLine(" $0.8");
}
public void putAddition()
{
Console.Write(" With Ice");
}
}
}
using System;
namespace designpattern.cs.AbstracFactory
{
public class CokeWithoutIce:IDrink
{
public void printName()
{
Console.Write("Coke");
}
public void printCost()
{
Console.WriteLine(" $0.8");
}
public void putAddition()
{
Console.Write(" Without Ice");
}
}
}
有了产品系列以后,我们可以构造一个抽象工厂接口,用于创建我们的订单,如下:
{
public interface OrderFactory
{
IHamburger createHamburger();
IDrink createDrink();
}
}
接下来,我们可以建立实体工厂类了,订单可能是各种可能的排列组合:
namespace designpattern.cs.AbstracFactory
{
public class OrderA:IOrderFactory
{
public IHamburger createHamburger()
{
return new ChickenHamburger();
}
public IDrink createDrink()
{
return new CokeWithIce();
}
}
}
using System;
namespace designpattern.cs.AbstracFactory
{
public class OrderB:IOrderFactory
{
public IHamburger createHamburger()
{
return new FishHamburger();
}
public IDrink createDrink()
{
return new CoffeeWithoutSugar();
}
}
}
using System;
namespace designpattern.cs.AbstracFactory
{
public class OrderC:IOrderFactory
{
public IHamburger createHamburger()
{
return new SuperHamburger();
}
public IDrink createDrink()
{
return new CoffeeWithSugar();
}
}
}
最后,我们些个客户端,构建2份订单,如下:
namespace designpattern.cs.AbstracFactory
{
public class ClientDemo1
{
static void Main(string[] args)
{
//Order A
IHamburger food1;
IDrink drink1;
IOrderFactory orderA = new OrderA();
food1 = orderA.createHamburger();
drink1 = orderA.createDrink();
Console.WriteLine("Your Order List:");
Console.WriteLine("------------------------------------------------");
food1.printName();
food1.printCost();
drink1.printName();
drink1.putAddition();
drink1.printCost();
Console.WriteLine("------------------------------------------------");
//Order C
IHamburger food2;
IDrink drink2;
IOrderFactory orderC = new OrderC();
food2 = orderC.createHamburger();
drink2 = orderC.createDrink();
Console.WriteLine("Your Order List:");
Console.WriteLine("------------------------------------------------");
food2.printName();
food2.printCost();
drink2.printName();
drink2.putAddition();
drink2.printCost();
Console.WriteLine("------------------------------------------------");
Console.ReadLine();
}
}
}
运行结果如下:
Your Order List:
------------------------------------------------
Chicken Hamburger $1.8
Coke With Ice $0.8
------------------------------------------------
Your Order List:
------------------------------------------------
Super Hamburger $2
Coffee With Sugar $1
------------------------------------------------