using
System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace ConsoleBuilder
... {
class Program
...{
static void Main(string[] args)
...{
Shape shape = new Shape();
VerticalBuilder rectangle = new Rectangle();
VerticalBuilder square = new Square();
shape.Construct(rectangle);
rectangle.Vertical.Show();
shape.Construct(square);
square.Vertical.Show();
Console.Write("please press any key to continue");
Console.Read();
}
}
class Shape
...{
public void Construct(VerticalBuilder verticalBuilder)
...{
verticalBuilder.ColorBuilder();
verticalBuilder.HeightBuilder();
verticalBuilder.WidthBuilder();
}
}
abstract class VerticalBuilder
...{
protected Vertical vertical;
public Vertical Vertical
...{
get ...{ return vertical;}
}
public abstract void ColorBuilder();
public abstract void HeightBuilder();
public abstract void WidthBuilder();
}
class Rectangle : VerticalBuilder
...{
public override void ColorBuilder()
...{
vertical = new Vertical("Rectangle");
vertical["Color"] = "Green";
}
public override void HeightBuilder()
...{
vertical["Height"] = "100";
}
public override void WidthBuilder()
...{
vertical["Width"] = "50";
}
}
class Square : VerticalBuilder
...{
public override void ColorBuilder()
...{
vertical = new Vertical("Square");
vertical["Color"] = "Yellow";
}
public override void HeightBuilder()
...{
vertical["Height"] = "200";
}
public override void WidthBuilder()
...{
vertical["Width"] = "100";
}
}
class Vertical
...{
private string type;
private Hashtable parts = new Hashtable();
public Vertical(string s)
...{
this.type = s;
}
public object this[string key]
...{
get
...{
return parts[key];
}
set
...{
parts[key] = value;
}
}
public void Show()
...{
Console.WriteLine(" -------------------");
Console.WriteLine("Vertical Type:{0}", type);
Console.WriteLine(" Color : {0}", parts["Color"]);
Console.WriteLine(" Height : {0}", parts["Height"]);
Console.WriteLine(" Width : {0}", parts["Width"]);
}
}
}
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace ConsoleBuilder
... {
class Program
...{
static void Main(string[] args)
...{
Shape shape = new Shape();
VerticalBuilder rectangle = new Rectangle();
VerticalBuilder square = new Square();
shape.Construct(rectangle);
rectangle.Vertical.Show();
shape.Construct(square);
square.Vertical.Show();
Console.Write("please press any key to continue");
Console.Read();
}
}
class Shape
...{
public void Construct(VerticalBuilder verticalBuilder)
...{
verticalBuilder.ColorBuilder();
verticalBuilder.HeightBuilder();
verticalBuilder.WidthBuilder();
}
}
abstract class VerticalBuilder
...{
protected Vertical vertical;
public Vertical Vertical
...{
get ...{ return vertical;}
}
public abstract void ColorBuilder();
public abstract void HeightBuilder();
public abstract void WidthBuilder();
}
class Rectangle : VerticalBuilder
...{
public override void ColorBuilder()
...{
vertical = new Vertical("Rectangle");
vertical["Color"] = "Green";
}
public override void HeightBuilder()
...{
vertical["Height"] = "100";
}
public override void WidthBuilder()
...{
vertical["Width"] = "50";
}
}
class Square : VerticalBuilder
...{
public override void ColorBuilder()
...{
vertical = new Vertical("Square");
vertical["Color"] = "Yellow";
}
public override void HeightBuilder()
...{
vertical["Height"] = "200";
}
public override void WidthBuilder()
...{
vertical["Width"] = "100";
}
}
class Vertical
...{
private string type;
private Hashtable parts = new Hashtable();
public Vertical(string s)
...{
this.type = s;
}
public object this[string key]
...{
get
...{
return parts[key];
}
set
...{
parts[key] = value;
}
}
public void Show()
...{
Console.WriteLine(" -------------------");
Console.WriteLine("Vertical Type:{0}", type);
Console.WriteLine(" Color : {0}", parts["Color"]);
Console.WriteLine(" Height : {0}", parts["Height"]);
Console.WriteLine(" Width : {0}", parts["Width"]);
}
}
}
意图:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
适用性:
1.当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。
2.当构造过程必须允许被构造的对象有不同的表示时。