无废话C#设计模式之六:Builder
2009-04-02 08:21:24 来源:WEB开发网意图
将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
场景
在电脑城装机总有这样的经历。我们到了店里,先会有一个销售人员来询问你希望装的机器是怎么样的配置,他会给你一些建议,最终会形成一张装机单。和客户确定了装机配置以后,他会把这张单字交给提货的人,由他来准备这些配件,准备完成后交给装机技术人员。技术人员会把这些配件装成一个整机交给客户。
不管是什么电脑,它总是由CPU、内存、主板、硬盘以及显卡等部件构成的,并且装机的过程总是固定的:
l 把主板固定在机箱中
l 把CPU安装到主板上
l 把内存安装到主板上
l 把硬盘连接到主板上
l 把显卡安装到主板上
但是,每台兼容机的部件都各不相同的,有些配置高一点,有些配置低一点,这是变化点。对于装机技术人员来说,他不需要考虑这些配件从哪里来的,他只需要把他们组装在一起了,这是稳定的装机流程。要把这种变化的配件和稳定的流程进行分离就需要引入Builder模式。
示例代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace BuilderExemple
{
class Program
{
static void Main(string[] args)
{
ComputerFactory factory = new ComputerFactory();
ComputerBuilder office = new OfficeComputerBuilder();
factory.BuildComputer(office);
office.Computer.ShowSystemInfo();
ComputerBuilder game = new GameComputerBuilder();
factory.BuildComputer(game);
game.Computer.ShowSystemInfo();
}
}
class ComputerFactory
{
public void BuildComputer(ComputerBuilder cb)
{
Console.WriteLine();
Console.WriteLine(">>>>>>>>>>>>>>>>>>Start Building " + cb.Name);
cb.SetupMainboard();
cb.SetupCpu();
cb.SetupMemory();
cb.SetupHarddisk();
cb.SetupVideocard();
Console.WriteLine(">>>>>>>>>>>>>>>>>>Build " + cb.Name + " Completed");
Console.WriteLine();
}
}
abstract class ComputerBuilder
{
protected string name;
public string Name
{
get { return name; }
set { name = value; }
}
protected Computer computer;
public Computer Computer
{
get { return computer; }
set { computer = value; }
}
public ComputerBuilder()
{
computer = new Computer();
}
public abstract void SetupMainboard();
public abstract void SetupCpu();
public abstract void SetupMemory();
public abstract void SetupHarddisk();
public abstract void SetupVideocard();
}
class OfficeComputerBuilder : ComputerBuilder
{
public OfficeComputerBuilder()
{
name = "OfficeComputer";
}
public override void SetupMainboard()
{
computer.Mainboard = "Abit升技LG-95C 主板(Intel 945GC芯片组/LGA 775/1066MHz) ";
}
public override void SetupCpu()
{
computer.Cpu = "Intel 英特尔赛扬D 336 (2.8GHz/LGA 775/256K/533MHz) ";
}
public override void SetupMemory()
{
computer.Memory = "Patriot博帝DDR2 667 512MB 台式机内存";
}
public override void SetupHarddisk()
{
computer.Harddisk = "Hitachi日立SATAII接口台式机硬盘(80G/7200转/8M)盒装";
}
public override void SetupVideocard()
{
computer.Videocard = "主板集成";
}
}
class GameComputerBuilder : ComputerBuilder
{
public GameComputerBuilder()
{
name = "GameComputer";
}
public override void SetupMainboard()
{
computer.Mainboard = "GIGABYTE技嘉GA-965P-DS3 3.3 主板(INTEL P965 东莞产)" ;
}
public override void SetupCpu()
{
computer.Cpu = "Intel 英特尔酷睿E4400 (2.0GHz/LGA 775/2M/800MHz)盒装";
}
public override void SetupMemory()
{
computer.Memory = "G.SKILL 芝奇F2-6400CL5D-2GBNQ DDR2 800 1G*2台式机内存";
}
public override void SetupHarddisk()
{
computer.Harddisk = "Hitachi日立SATAII接口台式机硬盘(250G/7200转/8M)盒装";
}
public override void SetupVideocard()
{
computer.Videocard = "七彩虹逸彩GT-GD3 UP烈焰战神H10 显卡(GeForce 8600GT/256M/DDR3)支持HDMI!";
}
}
class Computer
{
private string videocard;
public string Videocard
{
get { return videocard; }
set { videocard = value; }
}
private string cpu;
public string Cpu
{
get { return cpu; }
set { cpu = value; }
}
private string mainboard;
public string Mainboard
{
get { return mainboard; }
set { mainboard = value; }
}
private string memory;
public string Memory
{
get { return memory; }
set { memory = value; }
}
private string harddisk;
public string Harddisk
{
get { return harddisk; }
set { harddisk = value; }
}
public void ShowSystemInfo()
{
Console.WriteLine("==================SystemInfo==================");
Console.WriteLine("CPU:" + cpu);
Console.WriteLine("MainBoard:" + mainboard);
Console.WriteLine("Memory:" + memory);
Console.WriteLine("VideoCard:" + videocard);
Console.WriteLine("HardDisk:" + harddisk);
}
}
}
更多精彩
赞助商链接