ay解释:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
两个角色, 材料构建角色Builder, 材料搭建角色Director
创建个Builder
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace StrategyMo.Builder { public class AyWindowRenderBuilder { private string windowName; public string WindowName { get { return windowName; } set { windowName = value; } } public AyWindowRenderBuilder(string name) { this.WindowName = name; } public AyWindowRenderBuilder() { } public abstract void BuildTitleBar(); public abstract void BuildContentNav(); public abstract void BuildLeftMenu(); public abstract void BuildStatusBar(); } }
搭建人员,定义如何组装 生产好的零件
public class AyWindowRenderDirector { AyWindowRenderBuilder builder; public AyWindowRenderDirector(AyWindowRenderBuilder _builder) { this.builder = _builder; } public void Build() { //搭建人员,定义的顺序,不管客户端先实现的哪个方法,这个顺序必须固定。 www.ayjs.net builder.BuildTitleBar(); builder.BuildContentNav(); builder.BuildLeftMenu(); builder.BuildStatusBar(); } }
定义个窗体
public class Window1 : AyWindowRenderBuilder { public override void BuildContentNav() { Console.WriteLine("内容区域中的内容已经渲染"); } public override void BuildTitleBar() { Console.WriteLine("标题栏的内容已经渲染"); } public override void BuildLeftMenu() { Console.WriteLine("菜单栏的内容已经渲染"); } public override void BuildStatusBar() { Console.WriteLine("状态栏的内容已经渲染"); } }
使用方法,结果显示
建造者模式跟 外观模式有点类似,但是建造者模式,的组装的权利是在director中的。外观模式,是定义多个方法组,暴露给别人用