创建模式
一种封装创建对象的模式它包括:
- 抽象工厂模式(Abstract Factory)
- 构造者模式(Builder)
- 工厂方法(Factory Method)
- 原型对象(Prototype)
- 单例模式(Singleton)
Abstract Factory
提供一个接口,用于创建一组相关或依赖的对象而无需指定它们的具体类型。
从关键字"一组相关或依赖的对象"可知:
- 工厂要创建多个对象
- 创建的对象们是相互关联或相互依赖的
实例--动物世界
模拟食物链
class AnimalWorld
{
private Herbivore _herbivore;
private Carnivore _carnivore;
// Constructor
public AnimalWorld(AnimalFactory factory)
{
_carnivore = factory.CreateCarnivore();
_herbivore = factory.CreateHerbivore();
}
public void RunFoodChain()
{
_carnivore.Eat(_herbivore);
}
}
- AnimalWorld就是我们的客户端,客户端的代码不能被轻易改变。
- AnimalWorld中Carnivore(食肉动物)吃Herbivore(食草动物)
- 构造AnimalWorld需要提供AnimalFactory对象
- AnimalFactory负责创建_herbivore和_carnivore
AnimalFactory就是抽象工厂类,它负责创建一组对象(_herbivore和_carnivore),并且_herbivore和_carnivore具有一定的依赖性(_carnivore.Eat(_herbivore)).
食肉动物吃食草动物 (抽象)
abstract class Herbivore
{
}
abstract class Carnivore
{
public abstract void Eat(Herbivore h);
}
abstract class AnimalFactory
{
public abstract Herbivore CreateHerbivore();
public abstract Carnivore CreateCarnivore();
}
狮子吃角马(实现)
class Wildebeest: Herbivore
{
}
class Lion: Carnivore
{
public void Eat(Herbivore h)
{
Console.WriteLine(this.GetType().Name +
" eats " + h.GetType().Name);
}
}
class WildebeestLionFactory: AnimalFactory
{
public Herbivore CreateHerbivore()
{
return new Wildebeest();
}
public Carnivore CreateCarnivore()
{
return new Lion();
}
}
狼吃野牛(实现)
class Bison: Herbivore
{
}
class Wolf : Carnivore
{
public void Eat(Herbivore h)
{
Console.WriteLine(this.GetType().Name +
" eats " + h.GetType().Name);
}
}
class BisonWolfFactory: AnimalFactory
{
public Herbivore CreateHerbivore()
{
return new Bison();
}
public Carnivore CreateCarnivore()
{
return new Wolf();
}
}
测试程序
class MainApp
{
public static void Main()
{
// Create and run the WildebeestLionFactory animal world
ContinentFactory africa = new WildebeestLionFactory();
AnimalWorld world = new AnimalWorld(africa);
world.RunFoodChain();
// Create and run the BisonWolfFactory animal world
ContinentFactory america = new BisonWolfFactory();
world = new AnimalWorld(america);
world.RunFoodChain();
Console.ReadKey();
}
}
用WildebeestLionFactory和BisonWolfFactory创建了两个AnimalWorld,在上面的代码中我们甚至都不需要知道Wildebeest,Lion , Bison 和 Wolf的实现。
Factory Method
此模式定义一个接口用于创建一个对象,让子类决定要实例化哪个类。这种模式将类的实例化延迟到了子类。
抽象类
abstract class Product
{
}
abstract class Creator
{
public abstract Product FactoryMethod();
}
实现类A
class ProductA: Product{
}
class CreatorA: Creator
{
public override Product FactoryMethod(){
return new ProductA();
}
}
实现类B
class ProductB: Product{
}
class CreatorB: Creator
{
public override Product FactoryMethod(){
return new ProductB();
}
}
Client
class Client {
Creator creator;
List<Product> products = new List<Product>();
public Client(Creator ctor)
{
creator = ctor;
}
void Run(){
products.Add(creator.FactoryMethod());
}
}
void Main(){
Creator ctorA = new CreatorA();
Client c = new Client(ctorA);
Creator ctorB = new CreatorB();
Client c1 = new Client(ctorA);
}
每一个具体的Product类都需要一个与之对应的Creator,如示例代码一般简单,个人认为直接new出对应象实现反而更好。
Prototype
此模式让Prototype有克隆自己的能力
abstract class Prototype
{
abstract public Prototype Clone();
}
class ProductA : Prototype
{
public int Age { get; set; }
public string Name { get; set; }
public override Prototype Clone()
{
return this.MemberwiseClone() as Prototype;
}
}
Singleton
public class Singleton
{
static Singleton instance;
// Constructor is 'protected'
protected Singleton()
{
}
public static Singleton Instance()
{
// Uses lazy initialization.
// Note: this is not thread safe.
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
Builder
Builder设计模式将复杂对象的构造与其表示分离开来,以便相同的构造过程可以创建不同的表示。
Director
class Director
{
// Builder uses a complex series of steps
public void Construct(Builder builder)
{
builder.BuildPartA();
builder.BuildPartB();
}
}
生产一个产品,有多道工序,在Director中定义这些工序。
Builder和Product
abstract class Builder
{
public abstract void BuildPartA();
public abstract void BuildPartB();
public abstract Product GetResult();
}
class Product {
private List<string> _parts = new List<string>();
public void Add(string part)
{
_parts.Add(part);
}
}
一个产品有多个部分组成(PartA, PartB), 组合的顺序在Builder并不知道,只要生产出对应的部分即可。
BuilderX
class ConcreteBuilder1 : Builder
{
private Product _product = new Product();
public override void BuildPartA()
{
_product.Add("PartA");
}
public override void BuildPartB()
{
_product.Add("PartB");
}
public override Product GetResult()
{
return _product;
}
}
class ConcreteBuilder2 : Builder
{
private Product _product = new Product();
public override void BuildPartA()
{
_product.Add("PartX");
}
public override void BuildPartB()
{
_product.Add("PartY");
}
public override Product GetResult()
{
return _product;
}
}
测试代码
Director director = new Director();
Builder b1 = new ConcreteBuilder1();
director.director(b1);
b1.GetResult();
Builder b2 = new ConcreteBuilder2();
director.director(b2);
b2.GetResult();
因为生产工序样(使用同一个Director), 不同的Builder使用不了不同的原材料。