标签:
创建者模式:核心思想---将对象创建的变化部分与不变部分分离,将不变的部分抽象到基类中,将变化部分的创建延迟到具体类的实现过程中去。
(因为工具使用不是很给力,画得很粗糙):
下面是自己在整理思路时所写的一个例子,具体的实现用伪代码代替。
public class Computer { public object Cpu { get; set; } public object MainBoard { get; set; } public object Memory { get; set; } } public abstract class ComputerBuilder { protected Computer computer; public abstract void BuildCpu(); public abstract void BuildMainBoard(); public abstract void BuildMemory(); public Computer GetComputer() { BuildMainBoard(); BuildCpu(); BuildMemory(); return this.computer; } } public class CmdComputerBuilder : ComputerBuilder { public override void BuildCpu() { //创建cmdCpu的实际业务 computer.Cpu=new object(); } public override void BuildMainBoard() { //创建cmd主板的实际业务 computer.MainBoard = new object(); } public override void BuildMemory() { //创建cmd内存的实际业务 computer.Memory = new object(); } } public class IntelComputerBuilder : ComputerBuilder { public override void BuildCpu() { //创建IntelCpu的实际业务 computer.Cpu = new object(); } public override void BuildMainBoard() { //创建Intel主板的实际业务 computer.MainBoard = new object(); } public override void BuildMemory() { //创建Intel内存的实际业务 computer.Memory = new object(); } } public class ComputerFactory { public static Computer BuildComputer(ComputerBuilder builder) { return builder.GetComputer(); } } public class Client { public Computer BuyComputer() { ComputerBuilder builder = new IntelComputerBuilder(); return ComputerFactory.BuildComputer(builder); } }
创建者模式有区别与工厂模式,工厂模式更多的是针对单个明确类型的创建过程,而创建者模式则是对于复杂对象的创建过程,拆分变化。
具体的实现与运用还是要去实践的。睡觉去!
标签:
原文地址:http://www.cnblogs.com/Joy-et/p/4830974.html