当前:单件+抽象工厂+创建者+工厂方法
需求:坦克大战
创建两种坦克
坦克类型 | 射程 | 速度 |
b70 | 70米 | 时/70公里 |
b50 | 50米 | 时/50公里 |
设计说明
1.抽象工厂(AbstractFActory)承担了创建部件的任务
2.创建者(IBuilder)承担了讲部件组装的任务
3.工厂方法(IFactory)类相当于创建者模式的导演,但是他是并未给用户提供选择创建者的接口。
而是通过自己的多态来实现加载不同的创建者。
类图
代码
//-------功能类-------------------
//功能基类
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();
}
//====== 实装类如下 ========================
//------功能类-----------------------------
//70的发射功能
class Shot70 implements IOperation{
public void exe() {
ShotFun.get().exe(70);
}
}
//70的行走功能
class Run70 implements IOperation{
public void exe() {
RunFun.get().exe(70);
}
}
//50的发射功能
class Shot50 implements IOperation{
public void exe() {
ShotFun.get().exe(50);
}
}
//50的行走功能
class Run50 implements IOperation{
public void exe() {
RunFun.get().exe(50);
}
}
class Tank implements ITank{
IOperation mShot;
IOperation mRun;
int mSubType;
public void setmSubType(int mSubType) {
this.mSubType = mSubType;
}
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();
}
}
运行结果
下一篇:一个实例用全创建型模式-优化(冗余消除)-CSDN博客
标签:IOperation,exe,创建,void,class,用全,实例,new,public From: https://blog.csdn.net/xie__jin__cheng/article/details/144377140