抽象工厂
//抽象工厂模式
//提供一个接口,用于创建相关和依赖对象的家族,而不需要明确制定具体类
//原则:依赖抽象,不要依赖具体类
//抽象工厂使用组合,对象的创建被实现在工厂接口所暴露出的方法中
//这是一些原料
public abstract class 肉 { }
public class 肥肉 : 肉 { }
public class 瘦肉 : 肉 { }
public abstract class 蔬菜 { }
public class 白菜 : 蔬菜 { }
public class 土豆 : 蔬菜 { }
public abstract class 油 { }
public class 菜籽油 : 油 { }
public class 金龙鱼油 : 油 { }
//Pizza抽象类
public abstract class pizza
{
public string name { get; set; }
public 肉 rou;
public 蔬菜 shucai;
public 油 you;
public abstract void prepare();
}
//一种Pizza
public class pizzaA : pizza
{
public IPizzaIngredientFactory factory;
public pizzaA(IPizzaIngredientFactory factory)
{
this.factory = factory;
prepare();
}
//每种Pizza都有不同的做法
public override void prepare()
{
//原料从工厂得来!
rou = factory.Createyrou();
shucai = factory.Createyshucai();
you = factory.Createyyou();
}
}
//另外一种不要蔬菜的Pizza
public class pizzaB : pizza
{
public IPizzaIngredientFactory factory;
public pizzaB(IPizzaIngredientFactory factory)
{
this.factory = factory;
prepare();
}
public override void prepare()
{
rou = factory.Createyrou();
you = factory.Createyyou();
}
}
//原料工厂接口
public interface IPizzaIngredientFactory
{
肉 Createyrou();
蔬菜 Createyshucai();
油 Createyyou();
}
//一个成都风味的原料工厂
public class AIngredientFactory:IPizzaIngredientFactory
{
public 肉 Createyrou()
{
return new 瘦肉();
}
public 蔬菜 Createyshucai()
{
return new 白菜();
}
public 油 Createyyou()
{
return new 菜籽油();
}
}
//Pizza店抽象类
public abstract class pizzaStore
{
protected abstract pizza CreatePizza(string type);
public pizza OrderPizza(string type)
{
pizza p = CreatePizza(type);
return p;
}
}
//一个成都的Pizza店
public class StoreA : pizzaStore
{
protected override pizza CreatePizza(string type)
{
pizza p = null;
IPizzaIngredientFactory factory = new AIngredientFactory();
if (type == "A")
{
p = new pizzaA(factory);
p.name = "瘦肉Pizza";
}
else if (type == "B")
{
p = new pizzaB(factory);
p.name = "不要瘦肉的Pizza";
}
return p;
}
}
class Program
{
static void Main(string[] args)
{
pizzaStore storeA = new StoreA();
pizza p = storeA.OrderPizza("A");
Console.WriteLine(p.name);
Console.Read();
}
}
//提供一个接口,用于创建相关和依赖对象的家族,而不需要明确制定具体类
//原则:依赖抽象,不要依赖具体类
//抽象工厂使用组合,对象的创建被实现在工厂接口所暴露出的方法中
//这是一些原料
public abstract class 肉 { }
public class 肥肉 : 肉 { }
public class 瘦肉 : 肉 { }
public abstract class 蔬菜 { }
public class 白菜 : 蔬菜 { }
public class 土豆 : 蔬菜 { }
public abstract class 油 { }
public class 菜籽油 : 油 { }
public class 金龙鱼油 : 油 { }
//Pizza抽象类
public abstract class pizza
{
public string name { get; set; }
public 肉 rou;
public 蔬菜 shucai;
public 油 you;
public abstract void prepare();
}
//一种Pizza
public class pizzaA : pizza
{
public IPizzaIngredientFactory factory;
public pizzaA(IPizzaIngredientFactory factory)
{
this.factory = factory;
prepare();
}
//每种Pizza都有不同的做法
public override void prepare()
{
//原料从工厂得来!
rou = factory.Createyrou();
shucai = factory.Createyshucai();
you = factory.Createyyou();
}
}
//另外一种不要蔬菜的Pizza
public class pizzaB : pizza
{
public IPizzaIngredientFactory factory;
public pizzaB(IPizzaIngredientFactory factory)
{
this.factory = factory;
prepare();
}
public override void prepare()
{
rou = factory.Createyrou();
you = factory.Createyyou();
}
}
//原料工厂接口
public interface IPizzaIngredientFactory
{
肉 Createyrou();
蔬菜 Createyshucai();
油 Createyyou();
}
//一个成都风味的原料工厂
public class AIngredientFactory:IPizzaIngredientFactory
{
public 肉 Createyrou()
{
return new 瘦肉();
}
public 蔬菜 Createyshucai()
{
return new 白菜();
}
public 油 Createyyou()
{
return new 菜籽油();
}
}
//Pizza店抽象类
public abstract class pizzaStore
{
protected abstract pizza CreatePizza(string type);
public pizza OrderPizza(string type)
{
pizza p = CreatePizza(type);
return p;
}
}
//一个成都的Pizza店
public class StoreA : pizzaStore
{
protected override pizza CreatePizza(string type)
{
pizza p = null;
IPizzaIngredientFactory factory = new AIngredientFactory();
if (type == "A")
{
p = new pizzaA(factory);
p.name = "瘦肉Pizza";
}
else if (type == "B")
{
p = new pizzaB(factory);
p.name = "不要瘦肉的Pizza";
}
return p;
}
}
class Program
{
static void Main(string[] args)
{
pizzaStore storeA = new StoreA();
pizza p = storeA.OrderPizza("A");
Console.WriteLine(p.name);
Console.Read();
}
}