狀態模式就是將狀態的條件判斷語句轉化成其函數重寫形式,利用了面向對象語言的多態性,本文根據https://blog.csdn.net/lm324114/article/details/78819602的情景將狀態模式用Matlab語言實現。 根據上圖情景,用傳統的方法實現如下: RoomState. ...
狀態模式就是將狀態的條件判斷語句轉化成其函數重寫形式,利用了面向對象語言的多態性,本文根據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();