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 IList<string> parts = new List<string>();
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 |
/// <summary> /// 指挥建造过程 /// </summary> 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类:House is 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 = newTownHomeSubContractor(); 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 = newSingleFamilySubContractor(); 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