Object-oriented MATLAB programming small demo_ theory

Source: Internet
Author: User

1. Foreword

Matlab Beginners, in the compilation of MATLAB code, the feeling of the code is messy, not easy to read, suddenly think of whether you can use object-oriented thinking to write Matlab program, found on the internet, there is a special to explain the object-oriented MATLAB book, and the author also explained the design pattern, Very good. I also try to use the way to write a Java program before writing, simply write a demo program to facilitate their own later view.

Reference books: "MATLAB object-oriented programming---from Introduction to design patterns," author Xu Xian, Li Yuan. Thanks to the two authors

Operating system: Win7

MATLAB version: r2010b

2. Steps

This simple demo function is to send a message to two observers at intervals, and the receiver prints the message after receiving the message.

(1) Set up a folder called demo, open it, and then gradually set up a folder "+CN"--> "+jxm"--> "+demo", similar to the package in Java, note in the MATLAB package name before adding "+"


(2) Click on "+demo" file, right key--> "New file"--> "class" To create a Publisher class

ClassDef Publisher < handle events myevent1;% Define Event 1 myevent2;% Define event 2 End properties
        deltatime;% interval, in milliseconds mytimer;%timer count = 0;% count, close Timer end methods% constructor after 5 times
            function obj = Publisher (deltatime) obj.deltatime = deltatime; %timer Obj.mytimer = timer (' TIMERFCN ', @obj. Timerfunction, ' Period ', obj.deltatime/1000, ' ExecutionMode ', ' Fixedrat
        E ');
        End% to start Timer functions function Start (obj) Start (Obj.mytimer);
            End% Stop timer functions function stop (obj) ts=timerfind;
                if (~isempty (TS)) stop (TS);
            Delete (TS);
            End-end%timer response function timerfunction (obj,timer,event)% notifies Observer 1
            Obj.notify (' MyEvent1 ');
            % notify Observer 2, deliver message "Hello observer!"
  msg = CN.JXM.DEMO.MSG (' Hello observer! ');          Obj.notify (' MyEvent2 ', msg);
            Obj.count = Obj.count + 1;
            if (Obj.count = = 5) obj.stop ();
 End End End
(2) The same method of establishing the MSG class, note that the class of the parent class

ClassDef MSG < event. EventData    
    Properties
        str;
    End
    
    methods
        Function obj = MSG (str)
            obj.str = str;
        End
    End
    


(3) Establish Observer1 class

ClassDef Observer1 < handle
    properties
        str = ' I am Observer1, response to MyEvent1 ';
    End
    
    methods
        % adds the message publisher
        function Addpublisher (obj,publisher)
            publisher.addlistener (' MyEvent1 ', @obj. listenerfunction);
        End% Response
        function listenerfunction (obj,src,data)
            disp (OBJ.STR) after receiving the message;
        End
    End
    


(4) Establish Observer2 class

ClassDef Observer2 < handle    
    properties
         str = ' I am Observer2, response to Myevent2.the msg is: ';
    End
    
    Methods
        % Add message publisher
        function Addpublisher (obj,publisher)
            publisher.addlistener (' MyEvent2 ', @ obj.listenerfunction);
        End% Response
        function listenerfunction (obj,src,data)
            disp ([OBJ.STR,DATA.STR]) after receiving the message;
        End
    End
    


(5) Set test class

ClassDef Test    
    Properties
    End
    
    Methods (Static)
        function main ()
            Publsher = Cn.jxm.demo.Publisher (1000);
            Observer1 = Cn.jxm.demo.Observer1 ();
            Observer1.addpublisher (publsher);
            Observer2 = Cn.jxm.demo.Observer2 ();
            Observer2.addpublisher (publsher);
            
            Publsher.start ();
        End
    End
    


3. Test

In the MATLAB input:

Import Cn.jxm.demo.Test

Test.main ()

Get the results



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.