当前:观察者模式 职责链模 状态模式
需求:坦克大战
创建两种坦克
坦克类型 | 射程 | 速度 |
b70 | 70米 | 时/70公里 |
b50 | 50米 | 时/70公里 |
模式说明
观察者:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。
职责链:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
状态模式:允许一个对象在其内部状态改变时改变它的行为。从而使对象看起来似乎修改了其行为。
类图
代码
java实现
// static
class Function{
static void shot(int type) {
exe("射击:"+type);
}
static void run(int type) {
exe("前进:"+type);
}
static void exe(String str) {
System.out.println(str);
}
}
// interface
interface ICommandCenter{
void notifys();
void attach(ITank t);
IState getmState();
void setmState(IState mState);
}
interface ITank{
void update();
void setNextTank(ITank t);
void setCommandCenter(ICommandCenter c);
}
interface IState{
void exe(int type);
}
// concrete
class Shot implements IState{
static final Shot mShot = new Shot();
private Shot() {}
static Shot get() {
return mShot;
}
public void exe(int type) {
Function.shot(type);
}
}
class Run implements IState{
static final Run mRun = new Run();
private Run() {}
static Run get() {
return mRun;
}
public void exe(int type) {
Function.run(type);
}
}
class CommandCenter implements ICommandCenter{
ITank mLastTank = null;
IState mState;
public IState getmState() {
return mState;
}
public void setmState(IState mState) {
this.mState = mState;
}
public void notifys() {
if(mLastTank!=null) {
mLastTank.update();
}
}
public void attach(ITank t) {
t.setNextTank(mLastTank);
mLastTank = t;
t.setCommandCenter(this);
}
}
abstract class Tank implements ITank{
ITank mNextTank;
IState mState;
ICommandCenter mCommandCenter;
int mType;
public void update() {
mState = mCommandCenter.getmState();
mState.exe(mType);
if(null != mNextTank) {
mNextTank.update();
}
}
public void setNextTank(ITank t) {
mNextTank = t;
}
public void setCommandCenter(ICommandCenter c) {
mCommandCenter = c;
}
}
class B70Tank extends Tank{
public B70Tank() {
mType = 70;
}
}
class B50Tank extends Tank{
public B50Tank() {
mType = 50;
}
}
public class Client {
public static void main(String[] args) {
System.out.println("hello world !");
ICommandCenter c = new CommandCenter();
c.attach(new B70Tank());
c.attach(new B50Tank());
c.attach(new B70Tank());
c.setmState(Shot.get());
c.notifys();
c.setmState(Run.get());
c.notifys();
}
}
运行结果
优化前的代码,任何时候优化都等同于消除冗余+结构切割
// static
class Function{
static void shot(int type) {
exe("射击:"+type);
}
static void ruan(int type) {
exe("前进:"+type);
}
static void exe(String str) {
System.out.println(str);
}
}
// interface
interface ICommandCenter{
void notifys();
void attach(ITank t);
int getState();
void setState(int s);
}
interface ITank{
void update();
void setNextTank(ITank t);
void setCommandCenter(ICommandCenter c);
}
// concrete
class CommandCenter implements ICommandCenter{
ITank mLastTank = null;
int mState = 0;
public void notifys() {
if(mLastTank!=null) {
mLastTank.update();
}
}
public void attach(ITank t) {
t.setNextTank(mLastTank);
mLastTank = t;
t.setCommandCenter(this);
}
public int getState() {
return mState;
}
public void setState(int s) {
mState = s;
}
}
abstract class Tank implements ITank{
ITank mNextTank;
ICommandCenter mCommandCenter;
int mType;
int mState = 0;
public void update() {
mState = mCommandCenter.getState();
exe();
if(null != mNextTank) {
mNextTank.update();
}
}
public void setNextTank(ITank t) {
mNextTank = t;
}
public void setCommandCenter(ICommandCenter c) {
mCommandCenter = c;
}
private void exe() {
if(mState==0) {
shot();
}else{
run();
}
}
private void shot() {
Function.shot(mType);
}
private void run() {
Function.ruan(mType);
}
}
class B70Tank extends Tank{
public B70Tank() {
mType = 70;
}
}
class B50Tank extends Tank{
public B50Tank() {
mType = 50;
}
}
public class Client {
public static void main(String[] args) {
System.out.println("hello worldff !");
ICommandCenter c = new CommandCenter();
c.attach(new B70Tank());
c.attach(new B50Tank());
c.attach(new B70Tank());
c.setState(0);
c.notifys();
}
}