1、模式设计c#--创建型--Builder名称Builder建造者模式结构意图将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。适用性·当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。·当构造过程必须允许被构造的对象有不同的表示时。CodeExamplenamespace Builder_DesignPattern{ using System; // These two classes could be part of a framework, // which we will call DP
2、 // =============================================== class Director { public void Construct(AbstractBuilder abstractBuilder) { abstractBuilder.BuildPartA(); if (1==1 ) //represents some local decision inside director {
3、 abstractBuilder.BuildPartB(); } abstractBuilder.BuildPartC(); } } abstract class AbstractBuilder { abstract public void BuildPartA(); abstract public void BuildPartB(); abstract public void
4、 BuildPartC(); } // These two classes could be part of an application // ================================================= class ConcreteBuilder : AbstractBuilder { override public void BuildPartA() { // Create some object here known to Con
5、creteBuilder Console.WriteLine("ConcreteBuilder.BuildPartA called"); } override public void BuildPartB() { // Create some object here known to ConcreteBuilder Console.WriteLine("ConcreteBuilder.BuildPartB called"
6、); } override public void BuildPartC() { // Create some object here known to ConcreteBuilder Console.WriteLine("ConcreteBuilder.BuildPartC called"); } } /// /// Summary description for Client. //
7、/ public class Client { public static int Main(string[] args) { ConcreteBuilder concreteBuilder = new ConcreteBuilder(); Director director = new Director(); director.Construct(concreteBuilder); return 0;