简介:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
目的:
总结:使得对象的构建与表示分离 CreateHuman和BuilderServiceImpl分离
组成:构建器, 表示接口,接口实现
1,基础类
package com.design.o.builder; /** * 人体构造 */ public class Human { private String userName; private String head; //头 private String body; //身体 private String hand; //手 private String foot; //脚 public Human(String userName) { super(); this.userName = userName; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getHead() { return head; } public void setHead(String head) { this.head = head; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getHand() { return hand; } public void setHand(String hand) { this.hand = hand; } public String getFoot() { return foot; } public void setFoot(String foot) { this.foot = foot; } @Override public String toString() { return userName; } }
2,表示接口
package com.design.o.builder; /** * 建造者接口 */ public interface BuilderService { void createHuman(String userName); Human getHuman(); void createHead(); void createBody(); void createHand(); void createFoot(); }
3,表示接口实现
package com.design.o.builder; public abstract class BuilderServiceImpl implements BuilderService { private Human human; protected String bodyType; @Override public void createHuman(String userName) { human = new Human(userName); } @Override public Human getHuman() { try{ return human; }finally{ human = null; } } @Override public void createHead() { printInfo("head"); human.setHand(bodyType + "head"); } @Override public void createBody() { printInfo("body"); human.setBody(bodyType + "body"); } @Override public void createHand() { printInfo("hand"); human.setHand(bodyType + "hand"); } @Override public void createFoot() { printInfo("foot"); human.setFoot(bodyType + "foot"); } private void printInfo(String info){ System.out.println("创建 - "+bodyType+"- 用户: " + human.getUserName() + " 的" + info); } }
package com.design.o.builder; /** * 偏胖型 - 用户创建 */ public class FatBuilderServiceImpl extends BuilderServiceImpl { private static final String BODY_TYPE = "偏胖型"; public FatBuilderServiceImpl() { super.bodyType = BODY_TYPE; } }
package com.design.o.builder; /** * 偏瘦型 - 用户创建 */ public class ThinBuilderServiceImpl extends BuilderServiceImpl { private static final String BODY_TYPE = "偏瘦型"; public ThinBuilderServiceImpl(){ super.bodyType = BODY_TYPE; } }
4,构建器
package com.design.o.builder; /** * 建造者模式 */ public class CreateHuman { public Human createHuman(BuilderService builderService, String userName){ builderService.createHuman(userName); builderService.createHead(); builderService.createBody(); builderService.createHand(); builderService.createFoot(); return builderService.getHuman(); } }
5,Main
package com.design.o.builder; /** * 建造者模式 * * 目的: 使得对象的构建与表示分离 CreateHuman和BuilderServiceImpl分离 */ public class MainTest { public static void main(String[] args) { CreateHuman human = new CreateHuman(); System.out.println(human.createHuman(new FatBuilderServiceImpl(), "小胖子"));; System.out.println(human.createHuman(new ThinBuilderServiceImpl(), "小瘦子"));; } }
6,Result
创建 - 偏胖型- 用户: 小胖子 的head 创建 - 偏胖型- 用户: 小胖子 的body 创建 - 偏胖型- 用户: 小胖子 的hand 创建 - 偏胖型- 用户: 小胖子 的foot 小胖子
创建 - 偏瘦型- 用户: 小瘦子 的head 创建 - 偏瘦型- 用户: 小瘦子 的body 创建 - 偏瘦型- 用户: 小瘦子 的hand 创建 - 偏瘦型- 用户: 小瘦子 的foot 小瘦子