目录:《一个实例讲完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);
}
}
//-----实现类-----------------------------------------------
//跑-70
class Shot70 extends Shot{
public Shot70() {
type = 70;
}
}
//跑-50
class Run70 extends Run{
public Run70() {
type = 70;
}
}
//发射-70
class Shot50 extends Shot{
public Shot50() {
type = 50;
}
}
//发射-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();
}
}
//--------抽象工厂----------------------------------------------
//抽象工程-实现-70
class AbstractFactory70 implements IAbstractFactory{
public IOperation createShot() {
return new Shot70();
}
public IOperation createRun() {
return new Run70();
}
}
//抽象工程-实现-50
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());
}
}
//创建者-70
class Builder70 extends Builder{
public Builder70() {
mIAbstractFactory = new AbstractFactory70();
}
}
//创建者-50
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;
}
}
//工厂方法-70
class Factory70 extends Factory{
public Factory70() {
mBuilder = new Builder70();
}
}
//工厂方法-50
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();
}
}
运行结果
标签:IOperation,void,class,用全,实例,extends,new,public,冗余 From: https://blog.csdn.net/xie__jin__cheng/article/details/144377236