一个实例用全创建型模式-优化(冗余消除)
上一篇:《一个实例用全创建型模式》
目录:《一个实例讲完23种设计模式》
当前:单件+抽象工厂+创建者+工厂方法+优化
需求:坦克大战
创建两种坦克
坦克类型 射程 速度
b70 70米 时/70公里
b50 50米 时/50公里
设计说明
1.抽象工厂承担了创建不减的任务
2.创建者承担了讲部件组装的任务
3.工厂方法类相当于创建者模式的导演,但是他是并未给用户提供选择创建者的接口。
而是通过自己的多态来实现加载不同的创建者。
类图(红色为优化的部分)
代码
class Function{
public String mOperation;
public void exe(int type) {
System.out.println(mOperation+type);
}
};
class ShotFun extends Function{
static ShotFun mFunction = new ShotFun();
static ShotFun get() {
return mFunction;
}
public ShotFun() {
mOperation = "射击:";
}
}
class RunFun extends Function{
static RunFun mFunction = new RunFun();
static RunFun get() {
return mFunction;
}
public RunFun() {
mOperation = "跑:";
}
}
// interface ----------------
interface ITank{
void shot();
void run();
void setmShot(IOperation mShot);
void setmRun(IOperation mRun);
}
interface IOperation{
void exe();
}
interface IAbstractFactory{
IOperation createShot();
IOperation createRun();
}
interface IBuilder{
void createShout(ITank t);
void createRun(ITank t);
}
interface IFactory{
ITank createTank();
}
abstract class Shot implements IOperation{
int type;
public void exe() {
ShotFun.get().exe(type);
}
}
abstract class Run implements IOperation{
int type;
public void exe() {
RunFun.get().exe(type);
}
}
class Shot70 extends Shot{
public Shot70() {
type = 70;
}
}
class Run70 extends Run{
public Run70() {
type = 70;
}
}
class Shot50 extends Shot{
public Shot50() {
type = 50;
}
}
class Run50 extends Run{
public Run50() {
type = 50;
}
}
class Tank implements ITank{
IOperation mShot;
IOperation mRun;
public void setmShot(IOperation mShot) {
this.mShot = mShot;
}
public void setmRun(IOperation mRun) {
this.mRun = mRun;
}
public void shot() {
mShot.exe();
}
public void run() {
mRun.exe();
}
}
class AbstractFactory70 implements IAbstractFactory{
public IOperation createShot() {
return new Shot70();
}
public IOperation createRun() {
return new Run70();
}
}
class AbstractFactory50 implements IAbstractFactory{
public IOperation createShot() {
return new Shot50();
}
public IOperation createRun() {
return new Run50();
}
}
abstract class Builder implements IBuilder{
IAbstractFactory mIAbstractFactory;
public void createShout(ITank t) {
t.setmShot(mIAbstractFactory.createShot());
}
public void createRun(ITank t) {
t.setmRun(mIAbstractFactory.createRun());
}
}
class Builder70 extends Builder{
public Builder70() {
mIAbstractFactory = new AbstractFactory70();
}
}
class Builder50 extends Builder{
public Builder50() {
mIAbstractFactory = new AbstractFactory50();
}
}
abstract class Factory implements IFactory{
IBuilder mBuilder;
public ITank createTank() {
ITank t = new Tank();
mBuilder.createRun(t);
mBuilder.createShout(t);
return t;
}
}
class Factory70 extends Factory{
public Factory70() {
mBuilder = new Builder70();
}
}
class Factory50 extends Factory{
public Factory50() {
mBuilder = new Builder50();
}
}
public class Client {
public static void main(String[] args) {
System.out.println("hello worldff !");
Factory70 f7 = new Factory70();
ITank t = f7.createTank();
t.shot();
t.run();
}
}
运行结果