状态模式就是将状态的条件判断语句转化成其函数重写形式,利用了面向对象语言的多态性,本文根据https://blog.csdn.net/lm324114/article/details/78819602的情景将状态模式用Matlab语言实现。
根据上图情景,用传统的方法实现如下:
RoomState.m
classdef RoomState < handle enumeration FREE BOOKED CHECKIN end end
IfRoom.m
classdef IfRoom < handle properties state; end methods function obj = IfRoom() obj.state = RoomState.FREE; end function bookRoom(obj) switch obj.state case RoomState.FREE disp('完成预订...'); obj.state = RoomState.BOOKED; case RoomState.BOOKED disp('该房间已经被预订了...'); case RoomState.CHECKIN disp('该房间已经有人入住了...'); end end function checkInRoom(obj) switch obj.state case RoomState.FREE disp('空闲房间,入住..'); obj.state = RoomState.CHECKIN; case RoomState.BOOKED disp('入住房间....'); obj.state = RoomState.CHECKIN; case RoomState.CHECKIN disp('该房间已经有人入住了...'); end end function unsubscribeRoom(obj) switch obj.state case RoomState.FREE disp('该房间尚未预订'); case RoomState.BOOKED disp('已退订房间...'); obj.state = RoomState.FREE; case RoomState.CHECKIN disp('该房间已入住,不能退订'); end end function checkOutRoom(obj) switch obj.state case {RoomState.FREE, RoomState.BOOKED} disp('该房间尚未入住'); case RoomState.CHECKIN disp('已退房'); obj.state = RoomState.FREE; end end end end
测试代码:
ri = IfRoom(); ri.bookRoom(); ri.checkOutRoom(); ri.unsubscribeRoom(); ri.checkInRoom(); ri.checkOutRoom();
用状态模式实现如下:
State.m
classdef State < handle properties state; end methods(Abstract) bookRoom(obj); checkInRoom(obj) unsubscribeRoom(obj) checkOutRoom(obj) end end
FreeState.m
classdef FreeState < State properties room end methods function obj = FreeState(room) obj.room = room; end function bookRoom(obj) disp('完成预订...'); obj.room.state = obj.room.BOOKED_STATE; end function checkInRoom(obj) disp('空闲房间,入住..'); obj.room.state = obj.room.CHECKIN_STATE; end function unsubscribeRoom(~) disp('该房间尚未预订'); end function checkOutRoom(~) disp('该房间尚未入住'); end end end
BookedState.m
classdef BookedState < State properties room end methods function obj = BookedState(room) obj.room = room; end function bookRoom(~) disp('该房间已经被预订了...'); end function checkInRoom(obj) disp('入住房间....'); obj.room.state = obj.room.CHECKIN_STATE; end function unsubscribeRoom(obj) disp('已退订房间...'); obj.room.state = obj.room.FREE_STATE; end function checkOutRoom(~) disp('该房间尚未入住'); end end end
CheckInState.m
classdef CheckInState < State properties room end methods function obj = CheckInState(room) obj.room = room; end function bookRoom(~) disp('该房间已经有人入住了...'); end function checkInRoom(~) disp('该房间已经有人入住了...'); end function unsubscribeRoom(~) disp('该房间已入住,不能退订'); end function checkOutRoom(obj) disp('已退房'); obj.room.state = obj.room.FREE_STATE; end end end
Room.m
classdef Room < handle properties FREE_STATE; BOOKED_STATE; CHECKIN_STATE; end properties state; end methods function obj = Room() obj.FREE_STATE = FreeState(obj); obj.BOOKED_STATE = BookedState(obj); obj.CHECKIN_STATE = CheckInState(obj); obj.state = obj.FREE_STATE; end function bookRoom(obj) obj.state.bookRoom(); end function checkInRoom(obj) obj.state.checkInRoom(); end function unsubscribeRoom(obj) obj.state.unsubscribeRoom(); end function checkOutRoom(obj) obj.state.checkOutRoom(); end end end
测试代码
r = Room(); r.bookRoom(); r.checkOutRoom(); r.unsubscribeRoom(); r.checkInRoom(); r.checkOutRoom();
来源:https://www.cnblogs.com/usaddew/p/10863181.html