意甲

C#设计模式之Builder_a

2020-01-16 15:44:56来源:励志吧0次阅读

名称: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 description for Client.

///

public class Client

{

public static int Main(string[] args)

{

ConcreteBuilder concreteBuilder = new ConcreteBuilder();

Director director = new Director();

director.Construct(concreteBuilder);

return 0;

}

}

}

孩子不消化的症状
丁桂薏芽健脾凝胶用量
如何给儿童止咳安全用药
怎样调理小孩脾胃虚弱
分享到: