把建造逻辑和建造顺序 分离
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPytternDemo
{
public interface IBuilder
{
void ShowTop();
void ShowMiddle();
void ShowBottom();
}
/// <summary>
/// 打印出*
/// </summary>
public class StarBuilder : IBuilder
{
public void ShowTop()
{
Console.WriteLine("*");
}
public void ShowMiddle()
{
Console.WriteLine("**");
}
public void ShowBottom()
{
Console.WriteLine("***");
}
}
/// <summary>
/// 打印出+
/// </summary>
public class AddBuilder : IBuilder
{
public void ShowTop()
{
Console.WriteLine("+");
}
public void ShowMiddle()
{
Console.WriteLine("++");
}
public void ShowBottom()
{
Console.WriteLine("+++");
}
}
/// <summary>
/// 打印出-
/// </summary>
public class SubBuilder : IBuilder
{
public void ShowTop()
{
Console.WriteLine("-");
}
public void ShowMiddle()
{
Console.WriteLine("--");
}
public void ShowBottom()
{
Console.WriteLine("---");
}
}
public class TopDirector
{
private IBuilder builder;
public TopDirector(IBuilder builder)
{
this.builder = builder;
}
public void Show()
{
this.builder.ShowTop();
this.builder.ShowMiddle();
this.builder.ShowBottom();
}
}
public class MiddleDirector
{
private IBuilder builder;
public MiddleDirector(IBuilder builder)
{
this.builder = builder;
}
public void Show()
{
this.builder.ShowMiddle();
this.builder.ShowTop();
this.builder.ShowBottom();
}
}
public class BottomDirector
{
private IBuilder builder;
public BottomDirector(IBuilder builder)
{
this.builder = builder;
}
public void Show()
{
this.builder.ShowBottom();
this.builder.ShowTop();
this.builder.ShowMiddle();
}
}
}
IBuilder builder = new StarBuilder();
TopDirector td = new TopDirector(builder);
td.Show();
builder = new AddBuilder();
MiddleDirector md = new MiddleDirector(builder);
md.Show();
builder = new SubBuilder();
BottomDirector bd = new BottomDirector(builder);
bd.Show();