1 遥控器功能接口 (tvState-PowerController两个接口可以合并一个)
/**
* 电视状态
*/
public interface TvState {
public void nextChannel();
public void prevChannel();
public void turnUp();
public void turnDown();
}
2 开机实现
/**
* 开机状态
*/
public class PowerOnState implements TvState {
@Override
public void nextChannel() {
Log.i("---","下一个频道");
}
@Override
public void prevChannel() {
Log.i("---","上一个频道");
}
@Override
public void turnUp() {
Log.i("---","调高音量");
}
@Override
public void turnDown() {
Log.i("---","调低音量");
}
}
3 关机状态
/**
* 关机状态,无效
*/
public class PowerOffState implements TvState {
@Override
public void nextChannel() {
}
@Override
public void prevChannel() {
}
@Override
public void turnUp() {
}
@Override
public void turnDown() {
}
}
4 电源控制共能接口
/**
* 电源控制
*/
public interface PowerController {
public void powerOn();
public void powerOff();
}
5遥控器
/**
* 电视遥控器
*/
public class TvController implements PowerController{
TvState mTvState;
public void setState(TvState tvState){
this.mTvState = tvState;
}
@Override
public void powerOn() {
setState(new PowerOnState());
Log.i("----","开机了");
}
@Override
public void powerOff() {
setState(new PowerOffState());
Log.i("----","关机了");
}
public void nextChannel() {
mTvState.nextChannel();
}
public void prevChannel() {
mTvState.prevChannel();
}
public void turnUp() {
mTvState.turnUp();
}
public void turnDown() {
mTvState.turnDown();
}
}
6 用户使用
/**
* 用户操作
*/
public class User {
TvController tvController = new TvController();
public void opertion(){
//设置开机
tvController.powerOn();
//下一个频道
tvController.nextChannel();
//调高音量
tvController.turnUp();
//设置关机
tvController.powerOff();
//调高音量 此时不会生效、
tvController.turnUp();
}
}