构造者模式其实就是这个目的——某些类创建实例的时候,过程是一样的,但由于过程繁琐,很容易缺东少西组件没构造全。为了防止这种情况,我们将构造过程抽出来,统一管理,避免该问题发生。
首先,我们定义这个统一的过程:
package interfaze;
/**
* 所有游戏人物的套装均需遵守的约束
*
* @author lxp
*
*/
public interface ISkin {
/**
* 获取头盔
*/
void gainHelmet();
/**
* 获取战衣
*/
void gainBattleSuit();
/**
* 获取坐骑
*/
void gainMount();
}
然后,让这些类都实现上述过程:
package impl;
import bean.BattleSuit;
import bean.Helmet;
import bean.Mount;
import interfaze.ISkin;
import supper.Person;
/**
* 软妹型游戏人物
*
* @author lxp
*
*/
public class SoftGirl extends Person implements ISkin {
/**
* 获得头盔
*/
@Override
public void gainHelmet() {
System.out.println("我有了" + this.getHelmet().getName());
}
@Override
public void gainBattleSuit() {
System.out.println("我有了" + this.getBattleSuit().getName());
}
@Override
public void gainMount() {
System.out.println("我有了" + this.getMount().getName());
}
public SoftGirl(Helmet helmet, BattleSuit battleSuit, Mount mount) {
super(helmet, battleSuit, mount);
}
}
package impl;
import bean.BattleSuit;
import bean.Helmet;
import bean.Mount;
import interfaze.ISkin;
import supper.Person;
/**
* 五大三粗型游戏人物
*
* @author lxp
*
*/
public class Strong extends Person implements ISkin {
@Override
public void gainHelmet() {
System.out.println("我有了" + this.getHelmet().getName());
}
@Override
public void gainBattleSuit() {
System.out.println("我有了" + this.getBattleSuit().getName());
}
@Override
public void gainMount() {
System.out.println("我有了" + this.getMount().getName());
}
public Strong(Helmet helmet, BattleSuit battleSuit, Mount mount) {
super(helmet, battleSuit, mount);
}
}
最后,我们还要将这个过程统一管理:
package common;
import interfaze.ISkin;
import supper.Person;
public class Builder {
/**
* 创建的任务
*/
private Person p;
/**
* 所有的人物的组装都在这里,不会遗漏某一环节。
*/
public void createPerson() {
((ISkin) p).gainHelmet();
((ISkin) p).gainBattleSuit();
((ISkin) p).gainMount();
}
public Builder(Person p) {
super();
this.p = p;
}
}
以上就是主要代码段。其他文件见这里:
package supper;
/**
* 装备的父类
*
* @author lxp
*
*/
public class Equpment {
/**
* 装备名称
*/
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package supper;
import bean.BattleSuit;
import bean.Helmet;
import bean.Mount;
/**
* 人物类共用的
*
* @author lxp
*
*/
public class Person {
/**
* 头盔
*/
private Helmet helmet;
/**
* 战衣
*/
private BattleSuit battleSuit;
/**
* 坐骑
*/
private Mount mount;
public Helmet getHelmet() {
return helmet;
}
public void setHelmet(Helmet helmet) {
this.helmet = helmet;
}
public BattleSuit getBattleSuit() {
return battleSuit;
}
public void setBattleSuit(BattleSuit battleSuit) {
this.battleSuit = battleSuit;
}
public Mount getMount() {
return mount;
}
public void setMount(Mount mount) {
this.mount = mount;
}
public Person(Helmet helmet, BattleSuit battleSuit, Mount mount) {
super();
this.helmet = helmet;
this.battleSuit = battleSuit;
this.mount = mount;
}
}
package bean;
import supper.Equpment;
/**
* 坐骑类
*
* @author lxp
*
*/
public class Mount extends Equpment {
/**
* 设置坐骑名称
*
* @param mount
*/
public Mount(String mount) {
this.setName(mount);
}
}
package bean;
import supper.Equpment;
/**
* 头盔类
*
* @author lxp
*
*/
public class Helmet extends Equpment {
/**
* 设置头盔名称
*
* @param helmet
*/
public Helmet(String helmet) {
this.setName(helmet);
}
}
package bean;
import supper.Equpment;
/**
* 战衣类
*
* @author lxp
*
*/
public class BattleSuit extends Equpment {
/**
* 设置战衣名称
*
* @param battleSuit
*/
public BattleSuit(String battleSuit) {
this.setName(battleSuit);
}
}
package test;
import bean.BattleSuit;
import bean.Helmet;
import bean.Mount;
import common.Builder;
import impl.SoftGirl;
import impl.Strong;
public class TestBuilder {
public static void main(String[] args) {
Helmet softHelmet = new Helmet("粉红头盔");
Helmet strongHelmet = new Helmet("飞天头盔");
BattleSuit softSuit = new BattleSuit("粉色战衣");
BattleSuit strongSuit = new BattleSuit("侠盗战衣");
Mount softMount = new Mount("粉红坐骑");
Mount strongMount = new Mount("瀚海麒麟");
Builder softGirl = new Builder(new SoftGirl(softHelmet, softSuit, softMount));
Builder strong = new Builder(new Strong(strongHelmet, strongSuit, strongMount));
// 无论是哪种任务的构建过程都是统一控制的,不会出现缺东少西,尤其是组件多的情况下。
// 缺点是,稍有这种情况,某些实例有相同的构建过程。
// 故只将共同的提取出来为好。
softGirl.createPerson();
strong.createPerson();
}
}
=====================================欢迎交流指点^_^,QQ:814274513,加QQ请注明“探讨设计模式”字样哦=================================