模式角色与模式:
建造者模式可以将一个产品的内部表象与产品的生产过程分割开来,从而可以是一个建造过程生成具有不同的内部表象的产品对象。
using System;
using System.Collections;
namespace CSharpDesignPattern.BuilderPattern
{
public class Product
{
ArrayList parts = new ArrayList();
public void Add(String part)
{
parts.Add(part);
}
}
public interface IBuilder
{
void BuildPartA();
void BuildPartB();
Product GetResult();
}
public class ConcreteBuilder1 : IBuilder
{
private Product product;
public void BuildPartA()
{
product = new Product();
product.Add("PartA");
}
public void BuildPartB()
{
product.Add("PartB");
}
public Product GetResult()
{
return product;
}
}
public class ConcreteBuilder2 : IBuilder
{
private Product product;
public void BuildPartA()
{
product = new Product();
product.Add("PartX");
}
public void BuildPartB()
{
product.Add("PartY");
}
public Product GetResult()
{
return product;
}
}
class Director
{
public void Construct(IBuilder builder)
{
builder.BuildPartA();
builder.BuildPartB();
}
}
class Program
{
static void Main(string[] args)
{
Director director = new Director();
IBuilder b1 = new ConcreteBuilder1();
IBuilder b2 = new ConcreteBuilder2();
director.Construct(b1);
Product p1 = b1.GetResult();
director.Construct(b2);
Product p2 = b2.GetResult();
Console.ReadLine();
}
}
}