快捷搜索:

Net设计模式实例之建造者模式(Builder Pattern)

一、建造者模式简介(Brief Introduction)

建造者模式(Builder Pattern),将一个繁杂工具的构建与它的表示分离,使的同样的构建历程可以创建不合的表示。

建造者模式的优点是:使得建造代码与表示代码分离,因为建造者暗藏了该产品是若何组装的,以是如要改变一个产品的内部表示,只必要再定义一个详细的建造者就可以了。

二、办理的问题(What To Solve)

主要用于创建一些繁杂的工具,这些工具内部构建间的建造顺序平日是稳定的,然则工具内部的构建平日面临着繁杂的变更。

三、建造者模式阐发(Analysis)

1、建造者模式布局

Product类:详细的产品。

Director类:批示者,构建一个应用Builder接口的工具。

Builder接口:是为创建一个Product工具的各个组件指定的抽象接口

ConcreteBuilder类:详细的建造者,实现Builder接口,构造和装置各个部

2、源代码

1、详细产品Product

public class Product

{

private IListstring> parts = new Liststring>();

public void Add(string part)

{

parts.Add(part);

}

public void Show()

{

Console.WriteLine("产品开初创建......");

foreach (string part in parts)

{

Console.WriteLine("部件" + part);

}

Console.WriteLine("产品完成创建");

}

}

2、抽象建造者Builder及其详细建造者ConcreteBuilder1,ConcreteBuilder2

public abstract class Builder

{

public abstract void BuildPartA();

public abstract void BuildPartB();

public abstract Product GetResult();

}

public class ConcreteBuilder1 : Builder

{

Product product = new Product();

public override void BuildPartA()

{

product.Add("A1");

}

public override void BuildPartB()

{

product.Add("B1");

}

public override Product GetResult()

{

return product;

}

}

public class ConcreteBuilder2 : Builder

{

Product product = new Product();

public override void BuildPartA()

{

product.Add("A2");

}

public override void BuildPartB()

{

product.Add("B2");

}

public override Product GetResult()

{

return product;

}

}

3、批示者类Director

///

/// 批示建造历程

///

public class Director

{

public void Construct(Builder builder)

{

builder.BuildPartA();

builder.BuildPartB();

}

StringBuilder

}

4、客户端代码

static void Main(string[] args)

{

Director director = new Director();

Builder builder1 = new ConcreteBuilder1();

Builder builder2 = new ConcreteBuilder2();

director.Construct(builder1);

Product product1 = builder1.GetResult();

product1.Show();

director.Construct(builder2);

Product product2 = builder2.GetResult();

product2.Show();

Console.Read();

}

3、法度榜样运行结果

四.案例阐发(Example)

1、场景

假设屋子只有房顶和墙(Roof And Walls)

Jane和 Joe两个客户必要从建造商Bob那里买屋子。Jane必要1个房顶(Roof)和4面墙(Walls)的屋子,Joe必要1个房顶(Roof)和7面墙(Walls)的屋子。建造商必要经由过程建造者模式实现客户的个性要求。

此时建造商Bob(Contractor)便是批示者(Director),TownHomeSubContractor和SingleFamilySubContractor便是建造者类(Builder Class),屋子便是产品(House is Product).如下图所示

House类:Houseis Pruduct

Contractor类,批示者

MakeHouse()措施,批示屋子建造历程。

ISubConstractor接口,建造者接口,

AddRoof().添加房顶措施

AddWalls(),添加墙的措施

GetHouse(),得到构建好的屋子措施。

TownHomeSubContractor类,详细的建造者,实现建造者接口措施。

建造一个房顶和四面墙,以满意Jane 的要求。

SingleFamilySubContractor类,详细的建造者,实现建造者接口措施。

建造一个房顶和七面墙,以满意Joe的要求。

2、代码

1、产品House

public class House

{

public string Roof { get; set; }

public string Walls { get; set; }

}

2、批示者类Contractor

public class Contractor

{

public void MakeHouse(ISubContractor contractor)

{

contractor.AddRoof();

contractor.AddWalls();

}

}

3、抽象建造者ISubContractor及其详细建造者TownHomeSubContractor,SingleFamilySubContractor

//Sub Contractor interface (IBuilder)

public interface ISubContractor

{

void AddRoof();

void AddWalls();

House GetHouse();

}

//Concrete Builder Classes

public class TownHomeSubContractor : ISubContractor

{

private House _house = new House();

public void AddRoof()

{

_house.Roof = "Adding a small roof";

}

public void AddWalls()

{

_house.Walls = "Adding 4 Walls";

}

public House GetHouse()

{

return _house;

}

}

public class SingleFamilySubContractor : ISubContractor

{

private House _house = new House();

public void AddRoof()

{

_house.Roof = "Adding a large roof";

}

public void AddWalls()

{

_house.Walls = "Adding 7 Walls";

}

public House GetHouse()

{

return _house;

}

}

4、客户端代码

static void Main(string[] args)

{

//Jane wants a Town Home

Console.WriteLine("Building a Town Home for Jane..");

Contractor contractor = new Contractor();

ISubContractor subContractorTownHome = new TownHomeSubContractor();

contractor.MakeHouse(subContractorTownHome);

House townHome = subContractorTownHome.GetHouse();

Console.WriteLine(townHome.Roof + "..");

Console.WriteLine(townHome.Walls + "..");

//Joe wants a Single Family Home

Console.WriteLine("\nBuilding a Town Home for Joe..");

ISubContractor subContractorSingleFamily = new SingleFamilySubContractor();

contractor.MakeHouse(subContractorSingleFamily);

House singleFamilyHouse = subContractorSingleFamily.GetHouse();

Console.WriteLine(singleFamilyHouse.Roof + "..");

Console.WriteLine(singleFamilyHouse.Walls + "..");

}

3、法度榜样运行结果

五、总结(Summary)

建造者模式(Builder Pattern),将一个繁杂工具的构建与它的表示分离,使的同样的构建历程可以创建不合的表示。建造者模式是在当创建繁杂工具的算法应该自力于该工具的组成部分以及他们的装置要领时适用的模式。

转自:http://www.cnblogs.com/ywqu/archive/2010/01/11/1643753.html

您可能还会对下面的文章感兴趣: