当前位置: 移动技术网 > IT编程>软件设计>设计模式 > Matlab状态模式

Matlab状态模式

2019年05月15日  | 移动技术网IT编程  | 我要评论

状态模式就是将状态的条件判断语句转化成其函数重写形式,利用了面向对象语言的多态性,本文根据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();

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网