• Matlab状态模式


    状态模式就是将状态的条件判断语句转化成其函数重写形式,利用了面向对象语言的多态性,本文根据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();
  • 相关阅读:
    解题:HNOI 2008 玩具装箱
    2016级算法第一次上机助教版解题报告
    求解斐波那契数列复杂度分析
    数据库复习之规范化理论应用(第八次上机内容)
    数据库复习之规范化理论
    题目1042:Coincidence(最长公共子序列)
    题目1020:最小长方形(简单)
    题目1016:火星A+B(字符串拆分)
    题目1014:排名(结构体排序)
    题目1021:统计字符(hash简单应用)
  • 原文地址:https://www.cnblogs.com/usaddew/p/10863181.html
Copyright © 2020-2023  润新知