欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

State

程序员文章站 2024-03-13 16:22:33
...

State Pattern:当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类.

状态模式主要解决的是当控制一个对象状态的条件表达式过于复杂时的情况。把状态的判断逻辑转移到表示不同状态的一系列类中,可以把复杂的判断逻辑简化

下面以Player的几种状态为例,代码如下:

State.java

 

package com.flyingh.istate;

public interface State {
	void play();

	void pause();

	void quickForward();

	void quickBackward();

	void stop();
}

 Player.java

 

package com.flyingh.player;

import com.flyingh.istate.State;
import com.flyingh.state.PauseState;
import com.flyingh.state.PlayState;
import com.flyingh.state.QuickBackwardState;
import com.flyingh.state.QuickForwardState;
import com.flyingh.state.StopState;

public class Player {
	private State playState;
	private State pauseState;
	private State quickForwardState;
	private State quickBackwardState;
	private State stopState;
	private State currentState;

	public Player() {
		super();
		playState = new PlayState(this);
		pauseState = new PauseState(this);
		quickForwardState = new QuickForwardState(this);
		quickBackwardState = new QuickBackwardState(this);
		stopState = new StopState(this);
		currentState = stopState;
	}

	public void run() {
		System.out.println("运行播放器!");
	}

	public void play() {
		currentState.play();
	}

	public void pause() {
		currentState.pause();
	}

	public void quickForward() {
		currentState.quickForward();
	}

	public void quickBackward() {
		currentState.quickBackward();
	}

	public void stop() {
		currentState.stop();
	}

	public void exit() {
		System.out.println("退出播放器!");
	}

	public State getPlayState() {
		return playState;
	}

	public void setPlayState(State playState) {
		this.playState = playState;
	}

	public State getPauseState() {
		return pauseState;
	}

	public void setPauseState(State pauseState) {
		this.pauseState = pauseState;
	}

	public State getQuickForwardState() {
		return quickForwardState;
	}

	public void setQuickForwardState(State quickForwardState) {
		this.quickForwardState = quickForwardState;
	}

	public State getQuickBackwardState() {
		return quickBackwardState;
	}

	public void setQuickBackwardState(State quickBackwardState) {
		this.quickBackwardState = quickBackwardState;
	}

	public State getStopState() {
		return stopState;
	}

	public void setStopState(State stopState) {
		this.stopState = stopState;
	}

	public State getCurrentState() {
		return currentState;
	}

	public void setCurrentState(State currentState) {
		this.currentState = currentState;
	}
}

 PlayState.java

 

package com.flyingh.state;

import com.flyingh.istate.State;
import com.flyingh.player.Player;

public class PlayState implements State {
	private Player player;

	public PlayState(Player player) {
		super();
		this.player = player;
	}

	@Override
	public void play() {
		// TODO Auto-generated method stub
		System.out.println("已经处于播放状态了!");
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		System.out.println("由播放状态进入暂停状态!");
		player.setCurrentState(player.getPauseState());
	}

	@Override
	public void quickForward() {
		// TODO Auto-generated method stub
		System.out.println("由播放状态进入快进状态!");
		player.setCurrentState(player.getQuickForwardState());
	}

	@Override
	public void quickBackward() {
		// TODO Auto-generated method stub
		System.out.println("由播放状态进入快退状态!");
		player.setCurrentState(player.getQuickBackwardState());
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		System.out.println("由播放状态进入停止状态!");
		player.setCurrentState(player.getStopState());
	}

}

 PauseState.java

 

package com.flyingh.state;

import com.flyingh.istate.State;
import com.flyingh.player.Player;

public class PauseState implements State {
	private Player player;

	public PauseState(Player player) {
		super();
		this.player = player;
	}

	@Override
	public void play() {
		// TODO Auto-generated method stub
		System.out.println("由暂停状态进入播放状态!");
		player.setCurrentState(player.getPlayState());
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		System.out.println("已经处于暂停状态!");
	}

	@Override
	public void quickForward() {
		// TODO Auto-generated method stub
		System.out.println("暂停时不能快进!");
	}

	@Override
	public void quickBackward() {
		// TODO Auto-generated method stub
		System.out.println("暂停时不能快退!");
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		System.out.println("由暂停状态进入停止状态!");
		player.setCurrentState(player.getStopState());
	}

}

 QuickForwardState.java

 

package com.flyingh.state;

import com.flyingh.istate.State;
import com.flyingh.player.Player;

public class QuickForwardState implements State {
	private Player player;

	public QuickForwardState(Player player) {
		super();
		this.player = player;
	}

	@Override
	public void play() {
		// TODO Auto-generated method stub
		System.out.println("由快进状态进入播放状态!");
		player.setCurrentState(player.getPlayState());
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		System.out.println("快进时不能暂停!");
	}

	@Override
	public void quickForward() {
		// TODO Auto-generated method stub
		System.out.println("已经处于快进状态!");
	}

	@Override
	public void quickBackward() {
		// TODO Auto-generated method stub
		System.out.println("由快进状态进入快退状态!");
		player.setCurrentState(player.getQuickBackwardState());
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		System.out.println("由快进状态进入停止状态!");
		player.setCurrentState(player.getStopState());
	}

}

 QuickBackwardState.java

 

package com.flyingh.state;

import com.flyingh.istate.State;
import com.flyingh.player.Player;

public class QuickBackwardState implements State {
	private Player player;

	public QuickBackwardState(Player player) {
		super();
		this.player = player;
	}

	@Override
	public void play() {
		// TODO Auto-generated method stub
		System.out.println("由快退状态进入播放状态!");
		player.setCurrentState(player.getPlayState());
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		System.out.println("快退时不能暂停!");
	}

	@Override
	public void quickForward() {
		// TODO Auto-generated method stub
		System.out.println("由快退状态进入快进状态!");
		player.setCurrentState(player.getQuickForwardState());
	}

	@Override
	public void quickBackward() {
		// TODO Auto-generated method stub
		System.out.println("已经处于快退状态!");
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		System.out.println("由快退状态进入停止状态!");
		player.setCurrentState(player.getStopState());
	}

}

 StopState.java

 

package com.flyingh.state;

import com.flyingh.istate.State;
import com.flyingh.player.Player;

public class StopState implements State {
	private Player player;

	public StopState(Player player) {
		super();
		this.player = player;
	}

	@Override
	public void play() {
		// TODO Auto-generated method stub
		System.out.println("由停止状态进入播放状态!");
		player.setCurrentState(player.getPlayState());
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		System.out.println("停止时不能暂停!");
	}

	@Override
	public void quickForward() {
		// TODO Auto-generated method stub
		System.out.println("停止时不能快进!");
	}

	@Override
	public void quickBackward() {
		// TODO Auto-generated method stub
		System.out.println("停止时不能快退!");
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		System.out.println("已经处于停止状态!");
	}

}

 Client.java

 

package com.flyingh.client;

import com.flyingh.player.Player;

public class Client {
	public static void main(String[] args) {
		Player player = new Player();
		player.run();
		player.play();
		player.pause();
		player.play();
		player.quickForward();
		player.quickBackward();
		player.pause();
		player.play();
		player.stop();
		player.quickBackward();
		player.quickForward();
		player.play();
		player.quickBackward();
		player.pause();
		player.stop();
		player.exit();
	}
}

 程序运行结果如下:

 

运行播放器!
由停止状态进入播放状态!
由播放状态进入暂停状态!
由暂停状态进入播放状态!
由播放状态进入快进状态!
由快进状态进入快退状态!
快退时不能暂停!
由快退状态进入播放状态!
由播放状态进入停止状态!
停止时不能快退!
停止时不能快进!
由停止状态进入播放状态!
由播放状态进入快退状态!
快退时不能暂停!
由快退状态进入停止状态!
退出播放器!