名称:Builder
结构:
意图:
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
适用性 :
- 当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。
- 当构造过程必须允许被构造的对象有不同的表示时。
示例代码:
// Builder
namespace Builder_DesignPattern
{
using System;
// These two classes could be part of a framework,
// which we will call DP
// ===============================================
class Director
{
public void Construct(AbstractBuilder abstractBuilder)
{
abstractBuilder.BuildPartA();
if (1==1 ) //represents some local decision inside director
{
abstractBuilder.BuildPartB();
}
abstractBuilder.BuildPartC();
}
}
abstract class AbstractBuilder
{
abstract public void BuildPartA();
abstract public void BuildPartB();
abstract public void BuildPartC();
}
// These two classes could be part of an application
// =================================================
class ConcreteBuilder : AbstractBuilder
{
override public void BuildPartA()
{
// Create some object here known to ConcreteBuilder
Console.WriteLine("ConcreteBuilder.BuildPartA called");
}
override public void BuildPartB()
{
// Create some object here known to ConcreteBuilder
Console.WriteLine("ConcreteBuilder.BuildPartB called");
}
override public void BuildPartC()
{
// Create some object here known to ConcreteBuilder
Console.WriteLine("ConcreteBuilder.BuildPartC called");
}
}
/// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
public static int Main(string[] args)
{
ConcreteBuilder concreteBuilder = new ConcreteBuilder();
Director director = new Director();
director.Construct(concreteBuilder);
return 0;
}
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1671820