18
1 © 2016 The MathWorks, Inc. Introduction to Object-Oriented MATLAB Antti Löytynoja, Application Engineer

Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

  • Upload
    others

  • View
    29

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

1© 2016 The MathWorks, Inc.

Introduction to Object-Oriented MATLAB

Antti Löytynoja, Application Engineer

Page 2: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

2

Demonstration: Procedural Programming

Simulate a bank account using a structure and

some functions

– Structures (data)

– Functions (actions)

Page 3: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

4

Procedural Programming

Easy to learn

Minimal planning

No formal relationship between data and functions

Every detail is exposed

Page 4: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

5

Why Object-Oriented Programming?

Build robust applications

– Build a relationship between data and actions

– Control data and actions

Design building blocks for developing applications

– Eases code reuse

– Enhance collaboration

Page 5: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

6

Progression of Programming Techniques

Actions

Data

function

script

command line

value

variable

structure

classLevel of Abstraction / Sophistication

Application Complexity

Page 6: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

7

Object-Oriented Terminology

Class

– Blueprint of an idea

– Properties (data)

– Methods (actions)

Object

– Specific example of a class

– Instance

– Any number of objects of a class can exist

An element – object

Definition – Class

Page 7: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

14

Demonstration: Building a Simple Class

Describe a bank account using a class

– Properties (data)

– Methods (actions)

Use the object like a structure

Page 8: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

15

Objects

Easy to create

Manage their own data

Interchangeable with a structure

– No other code changes required

– Properties behave similar to field names

– Can’t add fields arbitrarily

Page 9: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

17

Objects with Methods

Have immediate access to

their own data (properties)

Allow you to overload

existing functions

Allow you to perform custom actions at

creation and deletion (constructor and destructor)

Page 10: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

18

AccountAccount

Address

VIP statusDeactivate

Number

etc.

Credit

history

Balance

Name

Withdraw

Accumulate

Deposit

Encapsulation

Page 11: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

19

Separates the interface

from the implementation

Simplifies object use

Becomes a building block

Withdraw

Accumulate

Deposit

Balance

Name

Encapsulation

Page 12: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

20

Demonstration: Applying Attributes

Control access

Access = public

Access = protected

Restrict modification

Constant

Dependent

Page 13: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

21

Using a Class as a Building Block

All accounts

Normal account

All private accounts

Credit account

Page 14: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

22

Demonstration: Creating a Credit Account

Define a new class

CreditAccount

Inherit from the existing

class Account to reuse code

Add two additional properties,

overload withdraw -function

Use the credit account

Credit

Name

Balance

Page 15: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

23

Inheritance

Subclass substitutes

for the superclass

Allows re-envisioning

and re-implementing

the superclass

Builds on proven code

Allows inheriting from the base MATLAB classes

Page 16: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

24

Summary

O-O MATLAB is a sophisticated way of building MATLAB apps

– Follows the same principles as any other O-O language

Builds robust code (encapsulation)

Allows you to reuse code (inheritance)

Good when building larger apps and if many people contributing to/using

code

Page 17: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

25

Additional material

http://se.mathworks.com/discovery/object-oriented-programming.html

Page 18: Introduction to Object-Oriented MATLAB · Introduction to Object-Oriented MATLAB ... Why Object-Oriented Programming? Build robust applications –Build a relationship between data

47© 2016 The MathWorks, Inc.

Thank You!