OBJECT-ORIENTED DESIGN JEAN SIMILIEN. WHAT IS OBJECT-ORIENTED DESIGN? Object-oriented design is the…
Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB...
Transcript of Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB...
![Page 1: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/1.jpg)
1 © 2014 The MathWorks, Inc.
Object Oriented & Event-Driven
Programming with MATLAB
Ameya Deoras
![Page 2: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/2.jpg)
2
Object-oriented programming in MATLAB
– Classes in MATLAB
– Advantages of object oriented design
– Example: Designing a portfolio tracker
Events in MATLAB
– Event-driven programming fundamentals
– Writing event handlers
– Example: Building a real-time portfolio tracker
Agenda
![Page 3: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/3.jpg)
3
Case Study: Portfolio Tracker
06:05 06:06 06:07
45.5
45.6
45.7
45.8
06:06 06:0748.7
48.8
48.9
49
06:04 06:05 06:06 06:0778.2
78.4
78.6
78.8
06:06 06:0761.8
61.82
61.84
61.86
61.88
06:02 06:03 06:04 06:05 06:06 06:07-675
-670
-665
-660
DD.N
WMT.N
MMM.N
JNJ.N
Portfolio
Subscribe to real-time quotes for 4 equities from Reuters service
Track real-time combined portfolio valueVisualize instrument &
portfolio history graphically in real-time
![Page 4: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/4.jpg)
4
What is a program?
x = 12
while (x < 100)
x = x+1
if (x == 23)
disp('Hello')
end
end
Code
Data
x = 12
while (x < 100)
x = x+1
if (x == 23)
disp('Hello')
end
end
Assignment
Looping Test
Increment
Test to Act
Take Action
End
End
Actions
![Page 5: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/5.jpg)
5
Progression of Programming Techniques
Algorithm
Data
function
script
command line
value
variable
structure
Level of Abstraction / Sophistication
![Page 6: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/6.jpg)
6
Progression of Programming Techniques
Algorithm
Data
function
script
command line
value
variable
structure
Level of Abstraction / Sophistication class
(properties)
(methods)
![Page 7: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/7.jpg)
7
Object-Oriented Terminology
Class
– Outline of an idea
– Properties (data)
– Methods (algorithms)
Object
– Specific example of a class
– Instance
An element of
the set – object
Defined set – class
Eg. Stock
YHOO GOOG
AKAM
MSFT ORCL
![Page 8: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/8.jpg)
8
From Structures to Objects
Operation Structure Class/Object
Initialization,
Property Access
x = struct;
x.Ticker = 'GOOG';
x.Price = 550;
x = MarketInstrument;
x.Ticker = 'GOOG';
x.Price = 550;
Example: Class MarketInstrument with properties Ticker and Price.
Erroneous
property name
x.ticker = 'MSFT';
x.ticker = 'MSFT';
No public field ticker exists for class
MarketInstrument
Erroneous
property value
x.Price = 'blah'; x.Price = 'blah';
Error using MarketInstrument/set.Price
Price must be a scalar numeric value
![Page 9: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/9.jpg)
9
Unique Advantages of Classes & Objects
Property Access Control, Error Checking
– Specify default values
– Create constants
– Make values interdependent
– Execute methods when properties change
properties
Components % Components of portfolio
Quantity % Number of units of each component
end
properties (SetAccess = protected)
CurrentCompPrice % Up-to-date component price
end
properties (Dependent)
CurrentPrice % Up-to-date price of portfolio
end
![Page 10: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/10.jpg)
10
Unique Advantages of Classes & Objects
Property Access Control, Error Checking
– Specify default values
– Create constants
– Make values interdependent
– Execute methods when properties change
>> x.Price = 'twelve';
set.Price(obj, val)
assert(isnumeric(val))
Error using set.Price
Price must be a numeric value
![Page 11: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/11.jpg)
11
Unique Advantages of Classes & Objects
Property Access Control, Error Checking
Reference Semantics
>> x = Instrument;
>> x.Price = 12;
>> myFun(x);
>> x.Price
ans =
12
classdef Instrument
function myFun(y)
y.Price = 15;
![Page 12: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/12.jpg)
12
Unique Advantages of Classes & Objects
Property Access Control, Error Checking
Reference Semantics
>> x = Instrument;
>> x.Price = 12;
>> myFun(x);
>> x.Price
ans =
15
classdef Instrument < handle
function myFun(y)
y.Price = 15;
![Page 13: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/13.jpg)
13
Unique Advantages of Classes & Objects
Property Access Control, Error Checking
Reference Semantics
Overload default functions & operations
– plot, disp, size
– [ ], +, /, &, ()
>> x.Price = 12;
>> x
My price is $12
function disp(obj)
disp(['My price is $'...
num2str(obj.Price)])
![Page 14: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/14.jpg)
14
Unique Advantages of Classes & Objects
Property Access Control, Error Checking
Reference Semantics
Overload default functions & operations
Automatic documentation
![Page 15: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/15.jpg)
15
Unique Advantages of Classes & Objects
Property Access Control, Error Checking
Reference Semantics
Overload default functions & operations
Automatic documentation
Support for MATLAB-like constructs
– Dynamic allocation
– Vectorized access of properties
>> x(2) = MarketInstrument;
>> x(1).Price = 12;
>> x(2).Price = 15;
>> x(3).Price = 20;
>> y = [x.Price]
y =
12 15 20
![Page 16: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/16.jpg)
16
Unique Advantages of Classes & Objects
Property Access Control, Error Checking
Reference Semantics
Overload default functions & operations
– plot, disp, size
– [ ], +, /, &,
Automatic documentation
Support for MATLAB-like constructs
– Dynamic allocation
– Vectorized access of properties
Plus all the benefits of OOP in general (encapsulation, inheritance…)
![Page 17: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/17.jpg)
17
Object-oriented programming in MATLAB
– Classes in MATLAB
– Advantages of object oriented design
– Example: Designing a portfolio tracker
Events in MATLAB
– Event-driven programming fundamentals
– Writing event handlers
– Example: Building a real-time portfolio tracker
Agenda
![Page 18: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/18.jpg)
18
Introduction to Event-Driven Programming
! ! !
time
Terminology
Event handler/Callback: Function called to respond to event
Listener: Object that monitors for a specific event & calls handler
Event data: Data associated with event
![Page 19: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/19.jpg)
19
Procedural vs Event-Driven Implementation
Instrument
Price
setPrice(newPrice)
display()
Goal: When Instrument price changes, display new price
Procedural Event-Driven
function setPrice(obj, newPr)
obj.price = newPr;
display(obj)
end
function setPrice(obj, newPr)
obj.price = newPr;
notify(obj,'priceChanged')
end
addlistener(obj,...
'priceChanged', @obj.display)
![Page 20: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/20.jpg)
20
Instrument
Price
setPrice(newPrice)
display()
Goal: When Instrument price changes, display new price and update
value of Portfolios
Portfolio
Instruments
Value
updateValue()
Procedural vs Event-Driven Implementation
Event-Driven
function setPrice(obj, newPr)
obj.price = newPr;
notify(obj,'priceChanged')
end
addlistener(instObj,'priceChanged', @instObj.display)
addlistener(instObj,'priceChanged', @portObj.updateValue)
![Page 21: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/21.jpg)
22
Sources of Events in MATLAB
MATLAB Graphics
MATLAB Classes
MATLAB timer Object
External Interfaces
(COM, Java, .NET)
Datafeed Toolbox
(Bloomberg, Reuters)
Trading Toolbox (Bloomberg, TT, IB,
CQG)
![Page 22: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/22.jpg)
23
Putting it Together: Portfolio Tracker
06:05 06:06 06:07
45.5
45.6
45.7
45.8
06:06 06:0748.7
48.8
48.9
49
06:04 06:05 06:06 06:0778.2
78.4
78.6
78.8
06:06 06:0761.8
61.82
61.84
61.86
61.88
06:02 06:03 06:04 06:05 06:06 06:07-675
-670
-665
-660
DD.N
WMT.N
MMM.N
JNJ.N
Portfolio
Subscribe to real-time quotes for 4 equities (MarketInstrument)
Track real-time combined portfolio value (MarketPortfolio)
Handle market events from Reuters service
Visualize instrument & portfolio history graphically in real-time
![Page 23: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/23.jpg)
24
Benefits of OOP in MATLAB
Separate interface from implementation
– More checks and balances and control over how your code is used
Manage complex data types with familiar interfaces
– Custom objects that override default operators and functions
Pass by reference
– Maintain one true copy of your data
Use event-driven programming
– Simplify design of complex real-time applications
![Page 24: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/24.jpg)
25
Additional Resources
![Page 25: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed153af238043426329d257/html5/thumbnails/25.jpg)
26
Questions and Answers