C#设计模式之我见(二)Ⅱ
下面介绍一下建造者模式(Builder Pattern),代码实例如下:
Builder类:
public abstract class Builder
{
public abstract void BuildDoor();
public abstract void BuildWall();
public abstract void BuildWindows();
public abstract void BuildFloor();
public abstract void BuildHouseCeiling();
public abstract House GetHouse();
}
Director类:
public class Director
{
public void Construct(Builder builder)
{
builder.BuildWall();
builder.BuildHouseCeiling();
builder.BuildDoor();
builder.BuildWindows();
builder.BuildFloor();
}
}
ChineseBuilder类:
public class ChineseBuilder:Builder
{
private House ChineseHouse = new House();
public override void BuildDoor()
{
Console.WriteLine("this Door 's style of Chinese");
}
public override void BuildWall()
{
Console.WriteLine("this Wall 's style of Chinese");
}
public override void BuildWindows()
{
Console.WriteLine("this Windows 's style of Chinese");
}
public override void BuildFloor()
{
Console.WriteLine("this Floor 's style of Chinese");
}
public override void BuildHouseCeiling()
{
Console.WriteLine("this Ceiling 's style of Chinese");
}
public override House GetHouse()
{
return ChineseHouse;
}
}