首先设计一个状态接口,接口中定义几个行为方法,再设计几个状态实现该接口,接着设计一个管理状态的容器,容器中定义几个行为方法,该行为方法直接使用接口的行为方法,容器中定义一个可以修改状态的方法,最后是客户端主类。
状态接口:
package com.freshbin.pattern.state.myexample.game;
/**
* 房间状态接口
*
* @author freshbin
* @date 2019年1月9日 下午7:26:14
*/
public interface RoomStateInterface {
/**
* 预定房间
*/
void reserveRoom();
/**
* 取消预定房间
*/
void cancleReserveRoom();
/**
* 入住
*/
void checkInRoom();
/**
* 退房
*/
void checkOutRoom();
}
状态对象:
package com.freshbin.pattern.state.myexample.game;
import com.freshbin.pattern.state.myexample.Room;
/**
* 空闲状态
*
* @author freshbin
* @date 2019年1月9日 下午7:59:33
*/
public class FreeState implements RoomStateInterface {
private Room room;
public FreeState(Room room) {
this.room = room;
}
@Override
public void reserveRoom() {
room.setRoomStateInterface(room.getReserveState());
System.out.println("预定房间成功!");
}
@Override
public void cancleReserveRoom() {
System.out.println("该房间暂时空闲,请预定后再取消退订!");
}
@Override
public void checkInRoom() {
room.setRoomStateInterface(room.getCheckInState());
System.out.println("入住成功!");
}
@Override
public void checkOutRoom() {
System.out.println("该房间暂时空闲,请预定并且入住后再退房!");
}
}
package com.freshbin.pattern.state.myexample.game;
import com.freshbin.pattern.state.myexample.Room;
/**
* 入住状态
*
* @author freshbin
* @date 2019年1月9日 下午7:59:33
*/
public class CheckInState implements RoomStateInterface {
private Room room;
public CheckInState(Room room) {
this.room = room;
}
@Override
public void reserveRoom() {
System.out.println("该房间处于入住状态,请退房后再预定!");
}
@Override
public void cancleReserveRoom() {
System.out.println("该房间处于入住状态,请退房后并且预定后再取消!");
}
@Override
public void checkInRoom() {
System.out.println("该房间处于入住状态,请退房后并且预定后再入住!");
}
@Override
public void checkOutRoom() {
room.setRoomStateInterface(room.getFreeState());
System.out.println("退房成功!");
}
}
package com.freshbin.pattern.state.myexample.game;
import com.freshbin.pattern.state.myexample.Room;
/**
* 预定状态
*
* @author freshbin
* @date 2019年1月9日 下午7:59:33
*/
public class ReserveState implements RoomStateInterface {
private Room room;
public ReserveState(Room room) {
this.room = room;
}
@Override
public void reserveRoom() {
System.out.println("该房间处于预定状态,请取消预定后再预定!");
}
@Override
public void cancleReserveRoom() {
room.setRoomStateInterface(room.getFreeState());
System.out.println("取消预定成功!");
}
@Override
public void checkInRoom() {
room.setRoomStateInterface(room.getCheckInState());
System.out.println("入住成功!");
}
@Override
public void checkOutRoom() {
System.out.println("该房间处于预定状态,请入住后再退房!");
}
}
管理状态的容器:
package com.freshbin.pattern.state.myexample;
import com.freshbin.pattern.state.myexample.game.CheckInState;
import com.freshbin.pattern.state.myexample.game.FreeState;
import com.freshbin.pattern.state.myexample.game.ReserveState;
import com.freshbin.pattern.state.myexample.game.RoomStateInterface;
/**
* 房间管理类
*
* @author freshbin
* @date 2019年1月9日 下午8:03:23
*/
public class Room {
private RoomStateInterface roomStateInterface;
private FreeState freeState;
private ReserveState reserveState;
private CheckInState checkInState;
public Room() {
freeState = new FreeState(this);
reserveState = new ReserveState(this);
checkInState = new CheckInState(this);
roomStateInterface = freeState;
}
/**
* 预定房间
*/
public void reserveRoom() {
roomStateInterface.reserveRoom();
}
/**
* 取消预定房间
*/
public void cancleReserveRoom() {
roomStateInterface.cancleReserveRoom();
}
/**
* 入住房间
*/
public void checkInRoom() {
roomStateInterface.checkInRoom();
}
/**
* 退房
*/
public void checkOutRoom() {
roomStateInterface.checkOutRoom();
}
public RoomStateInterface getRoomStateInterface() {
return roomStateInterface;
}
public void setRoomStateInterface(RoomStateInterface roomStateInterface) {
this.roomStateInterface = roomStateInterface;
}
public FreeState getFreeState() {
return freeState;
}
public void setFreeState(FreeState freeState) {
this.freeState = freeState;
}
public ReserveState getReserveState() {
return reserveState;
}
public void setReserveState(ReserveState reserveState) {
this.reserveState = reserveState;
}
public CheckInState getCheckInState() {
return checkInState;
}
public void setCheckInState(CheckInState checkInState) {
this.checkInState = checkInState;
}
}
客户端主类:
package com.freshbin.pattern.state.myexample;
import java.util.ArrayList;
import java.util.List;
/**
* 状态模式
*
* @author freshbin
* @date 2019年1月9日 下午2:49:32
*/
public class StatePatternDemo {
public static void main(String[] args) {
System.out.println("流程:预定->入住->退订->退房");
Room room0 = new Room();
room0.reserveRoom();
room0.checkInRoom();
room0.cancleReserveRoom();
room0.checkOutRoom();
System.out.println("===================");
System.out.println("流程:入住->预定->退订->退房->预定-退订");
room0.checkInRoom();
room0.reserveRoom();
room0.cancleReserveRoom();
room0.checkOutRoom();
room0.reserveRoom();
room0.cancleReserveRoom();
}
}
最后是效果图: