建造者模式Builder Pattern
建造者模式(Builder Pattern)强调的是将一个复杂对象的创建过程与它的表示分离,使得同样的构建过程可以创建不同的表示。建造者模式描述的是如何一步一步地创建一个复杂的对象,它允许用户通过指定复杂对象的类型和内容就可以构建它们,用户不需要知道内部的具体构建细节。
建造者模式结构图:
Builder表示抽象建造者,它为创建一个Product对象的各个部件指定抽象接口;
ConcreteBuilder表示具体建造者,它实现了Builder接口,用于构造和装配产品的各个部件,在其中定义并明确它所创建的产品,并返回产品的接口;
Directior表示指挥者,它用于构建一个实现builder接口的对象;
Product表示产品的角色,它是被构建的复杂对象,具体建造者创建改产品的内部表示并定义它的装配过程。
建造者与抽象工厂模式和相似,但是Builder返回一个完整的产品,而AbstractFactory返回的是一系列相关的产品;在AbstractFactory中客户生自己要用的对象,而在builder中客户指导director类如何去生成对象。
建造模式之游戏人物角色
product:Actor
package Product;
public class Actor {
private String type;
private String sex;
private String face;
private String constume;
private String hairStyle;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getFace() {
return face;
}
public void setFace(String face) {
this.face = face;
}
public String getConstume() {
return constume;
}
public void setConstume(String constume) {
this.constume = constume;
}
public String getHairStyle() {
return hairStyle;
}
public void setHairStyle(String hairStyle) {
this.hairStyle = hairStyle;
}
}
Builder:ActorBuilder
package Builder;
import Product.Actor;
public abstract class ActorBuilder {
protected Actor actor=new Actor();
public abstract void builderType();
public abstract void builderSex();
public abstract void builderFace();
public abstract void builderCostume();
public abstract void builderHairStyle();
public Actor createActor(){
return actor;
}
}
ConcrteBuilder:HeroBuilder
package ConcreteBuilder;
import Builder.ActorBuilder;
public class HeroBuilder extends ActorBuilder {
@Override
public void builderCostume() {
actor.setConstume("盔甲");
}
@Override
public void builderFace() {
actor.setFace("英俊");
}
@Override
public void builderHairStyle() {
actor.setHairStyle("飘逸");
}
@Override
public void builderSex() {
actor.setSex("男");
}
@Override
public void builderType() {
actor.setType("英雄");
}
}
ConcreteBuilder:AngleBuilder
package ConcreteBuilder;
import Builder.ActorBuilder;
public class AngleBuilder extends ActorBuilder {
@Override
public void builderCostume() {
actor.setConstume("白裙");
}
@Override
public void builderFace() {
actor.setFace("漂亮");
}
@Override
public void builderHairStyle() {
actor.setHairStyle("披肩长发");
}
@Override
public void builderSex() {
actor.setSex("女");
}
@Override
public void builderType() {
actor.setType("天使");
}
}
ConcreteBuilder:GhostBuilder
package ConcreteBuilder;
import Builder.ActorBuilder;
public class GhostBuilder extends ActorBuilder {
@Override
public void builderCostume() {
actor.setConstume("黑衣");
}
@Override
public void builderFace() {
actor.setFace("丑陋");
}
@Override
public void builderHairStyle() {
actor.setHairStyle("光头");
}
@Override
public void builderSex() {
actor.setSex("妖");
}
@Override
public void builderType() {
actor.setType("魔鬼");
}
}
测试类
package Test;
import Builder.ActorBuilder;
import ConcreteBuilder.AngleBuilder;
import Director.ActorController;
import Product.Actor;
public class Test {
public static void main(String args[]){
ActorController ac=new ActorController();
ActorBuilder ab;
ab=new AngleBuilder();
Actor angle;
angle=ac.constract(ab);
System.out.println(angle.getType()+"的外观");
System.out.println("性别:"+angle.getSex());
System.out.println("面容:"+angle.getFace());
System.out.println("服装:"+angle.getConstume());
System.out.println("发型:"+angle.getHairStyle());
}
}
测试结果:
优点:
1:将复杂产品的创建步骤分解在不同的方法中,使创建过程更加清晰,能够更精准的控制复杂产品的产生过程