在软件系统中,可能会出现以下情况,一个产品其构建过程是稳定的,但其组成部件是不稳定的,容易发生变化。建造者者模式可以用来解决这个问题。
创建者模式
例如:组装一台娱乐用的高配置电脑和组装一台办公用的普通配置电脑,其组装过程是一样的,发生变化的是组成电脑的部件,它们来自不同的厂商、可能具有不同的性能,当然,还有不同的价格(这个也算作组成电脑部件的一个属性吧)。使用创建者模式,可以在其算法相对稳定的基础上,封装构成产品的组件的变化。该例中,相对稳定的算法是指组装电脑的方法(装主板,装CUP,装内存等),相对不稳定的组成部件是指主板、CPU、内存等可以是来自不同的厂家,可以具有不同水平的性能。
新建一个抽象的建筑者类(BuilderBase),用来封装组成产品的各个部件的变化,代码如下:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BuliderPattern
{
public abstract class BuilderBase
{
/// <summary>
/// 显示器
/// </summary>
public abstract void AddCLD();
/// <summary>
/// 主板
/// </summary>
public abstract void AddMainBoard();
/// <summary>
/// CPU
/// </summary>
public abstract void AddCPU();
/// <summary>
/// 内存
/// </summary>
public abstract void AddMemory();
/// <summary>
/// 机箱
/// </summary>
public abstract void AddBox();
/// <summary>
/// 键盘
/// </summary>
public abstract void AddKeyBoard();
/// <summary>
/// 鼠标
/// </summary>
public abstract void AddMouse();
/// <summary>
/// 硬盘
/// </summary>
public abstract void AddDisk();
/// <summary>
/// 组装好的电脑
/// </summary>
/// <returns></returns>
public abstract Computer GetComputer();
}
}
新建两个具体的继承于BuilderBase的建造者类,它们分别用来建造高配置电脑和普通配置电脑,代码如下:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BuliderPattern
{
public class FamilyComputerBuilder:BuilderBase
{
private Computer _computer = new Computer();
public override void AddCLD()
{
_computer.AddPart("三星 2343BW",1215);
}
public override void AddMainBoard()
{
_computer.AddPart("微星 P45 Neo3-FR V2", 980);
}
public override void AddCPU()
{
_computer.AddPart("Intel 酷睿2双核 E8400(盒)", 1140);
}
public override void AddMemory()
{
_computer.AddPart("金士顿 2GB DDR2 800(窄板)*2", 600);
}
public override void AddBox()
{
_computer.AddPart("酷冷至尊 毁灭者RC-K100", 299);
}
public override void AddKeyBoard()
{
_computer.AddPart("微软光电键鼠套装", 75);
}
public override void AddMouse()
{
_computer.AddPart("微软光电键鼠套装", 75);
}
public override void AddDisk()
{
_computer.AddPart("希捷 1TB 7200.12 32M(串口/散)", 580);
}
public override Computer GetComputer()
{
return _computer;
}
}
}
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BuliderPattern
{
public class OffaceComputerBuilder : BuilderBase
{
private Computer _computer = new Computer();
public override void AddCLD()
{
_computer.AddPart("爱国者F725 17'8ms", 1799);
}
public override void AddMainBoard()
{
_computer.AddPart("华擎K8VM890", 475);
}
public override void AddCPU()
{
_computer.AddPart("Sempron64 2600+(754Pin盒)", 545);
}
public override void AddMemory()
{
_computer.AddPart("海盗船Corsair512MB/DDR 400", 310);
}
public override void AddBox()
{
_computer.AddPart("FOXCONN飞狐804机箱", 265);
}
public override void AddKeyBoard()
{
_computer.AddPart("现代套件(键盘)", 25);
}
public override void AddMouse()
{
_computer.AddPart("现代套件(鼠标)", 25);
}
public override void AddDisk()
{
_computer.AddPart("酷鱼 7200 SATA 8M 80G", 435);
}
public override Computer GetComputer()
{
return _computer;
}
}
}
新建一个BuilderManager类,其不变的算法单独放在这个类:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BuliderPattern
{
public class BuilderManager
{
public void Construct(BuilderBase builder)
{
builder.AddBox();
builder.AddCLD();
builder.AddCPU();
builder.AddDisk();
builder.AddKeyBoard();
builder.AddMainBoard();
builder.AddMemory();
builder.AddMouse();
}
}
}
最后是客户端代码调用:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BuliderPattern;
using System.Configuration;
using System.Reflection;
namespace Client
{
class Program
{
static void Main(string[] args)
{
Computer computer = GetBuilder().GetComputer();
Dictionary<string, int> part = computer.GetPartList();
foreach (var item in part.Keys)
{
Console.WriteLine(string.Format("{0}:{1}",item,part[item]));
}
Console.WriteLine("总价钱:" + computer.GetCast().ToString());
Console.Read();
}
public static BuilderBase GetBuilder()
{
string builderClassName = ConfigurationSettings.AppSettings["ComupterType"];
System.Reflection.Assembly o = Assembly.Load("BuliderPattern");
BuilderBase builder = (BuilderBase)o.CreateInstance("BuliderPattern." + builderClassName);
BuilderManager manager = new BuilderManager();
manager.Construct(builder);
return builder;
}
}
}
App.Config文件:
Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!--<add key="ComupterType" value="FamilyComputerBuilder"/>-->
<add key="ComupterType" value="OffaceComputerBuilder"/>
</appSettings>
</configuration>
看一下运行结果: