首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

创造型——Builder模式

2012-10-06 
创建型——Builder模式UML:?如何使用:假设一个复杂对象由多个部件组成的。Builder模式是把复杂对象的创建和部

创建型——Builder模式

UML:

创造型——Builder模式

?

如何使用:

假设一个复杂对象由多个部件组成的。Builder模式是把复杂对象的创建和部件的创建分别开来,分别用Builder类和Director类来表示。

package com.lwf.create.builder;public interface Builder {//用于复杂对象的创建,而其部件的组装在Director类中。void builderPartA();void builderPartB();Product getResult();}

?

package com.lwf.create.builder;public class ConcreateBuilder implements Builder {Part partA,partB;public void builderPartA() {partA = new Part();}public void builderPartB() {partB = new Part();}public Product getResult() {return new Product(partA,partB);}}

?

?

package com.lwf.create.builder;public class Director {//用于部件的组装Builder builder;public Director(Builder builder){this.builder = builder;}public void construct(){builder.builderPartA();builder.builderPartB();}}

?

?

package com.lwf.create.builder;public class Product {Part partA;Part partB;public Product(Part partA,Part partB){this.partA = partA;this.partB = partB;}}

?

?

package com.lwf.create.builder;public class Part {}

?

?

package com.lwf.create.builder;public class TestBuilder {public static void main(String[] args) {Builder builder = new ConcreateBuilder();Director director = new Director(builder);director.construct();//组装部件builder.getResult();//得到最终产品}}

?

?代码见附件

热点排行