首页 > 代码库 > java设计模式_建造者模式
java设计模式_建造者模式
/** * */ package com.wangbiao.design.builder; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * @Title: Product.java * @Package com.wangbiao.design.builder * @Description: TODO * @author wangbiao * @date 2014-9-26 上午10:06:53 * @version V1.0 */ public class Product { List<String> parts = new ArrayList<String>(); public void addPart(String part){ parts.add(part); } public void display(){ Iterator<String> iter = parts.iterator(); while(iter.hasNext()){ System.out.println(iter.next()); } } } /** * */ package com.wangbiao.design.builder; /** * @Title: Builder.java * @Package com.wangbiao.design.builder * @Description: TODO * @author wangbiao * @date 2014-9-26 上午10:16:28 * @version V1.0 */ public abstract class Builder { public abstract void builderPartA(); public abstract void bulderPartB(); public abstract Product getResult(); } /** * */ package com.wangbiao.design.builder; /** * @Title: ConcreteBuilderA.java * @Package com.wangbiao.design.builder * @Description: TODO * @author wangbiao * @date 2014-9-26 上午10:18:47 * @version V1.0 */ public class ConcreteBuilderA extends Builder { private Product product = new Product(); /* (non-Javadoc) * @see com.wangbiao.design.builder.Builder#builderPartA() */ @Override public void builderPartA() { // TODO Auto-generated method stub product.addPart("大屏"); } /* (non-Javadoc) * @see com.wangbiao.design.builder.Builder#bulderPartB() */ @Override public void bulderPartB() { // TODO Auto-generated method stub product.addPart("金属机身"); } /* (non-Javadoc) * @see com.wangbiao.design.builder.Builder#getResult() */ @Override public Product getResult() { // TODO Auto-generated method stub return product; } } /** * */ package com.wangbiao.design.builder; /** * @Title: ConcreteBuilderB.java * @Package com.wangbiao.design.builder * @Description: TODO * @author wangbiao * @date 2014-9-26 上午10:19:05 * @version V1.0 */ public class ConcreteBuilderB extends Builder { private Product product = new Product(); /* (non-Javadoc) * @see com.wangbiao.design.builder.Builder#builderPartA() */ @Override public void builderPartA() { // TODO Auto-generated method stub product.addPart("小屏"); } /* (non-Javadoc) * @see com.wangbiao.design.builder.Builder#bulderPartB() */ @Override public void bulderPartB() { // TODO Auto-generated method stub product.addPart("塑料机身"); } /* (non-Javadoc) * @see com.wangbiao.design.builder.Builder#getResult() */ @Override public Product getResult() { // TODO Auto-generated method stub return product; } } /** * */ package com.wangbiao.design.builder; /** * @Title: Director.java * @Package com.wangbiao.design.builder * @Description: TODO * @author wangbiao * @date 2014-9-26 上午10:19:19 * @version V1.0 */ public class Director { public void createProduct(Builder builder){ builder.builderPartA(); builder.bulderPartB(); } } /** * */ package com.wangbiao.design.builder; /** * @Title: Client.java * @Package com.wangbiao.design.builder * @Description: TODO * @author wangbiao * @date 2014-9-26 上午10:16:10 * @version V1.0 */ public class Client { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub /* *建造者模式的好处是分离了建造代码和表示代码,隐藏了建造的过程。 *建造者模式适用于创建复杂对象的逻辑独立于创建过程和装配方式的时候 。 * */ Director director = new Director(); Builder builderA = new ConcreteBuilderA(); Builder builderB = new ConcreteBuilderB(); director.createProduct(builderA); Product p1 = builderA.getResult(); p1.display(); director.createProduct(builderB); Product p2 = builderB.getResult(); p2.display(); } }
java设计模式_建造者模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。