MATLAB Object-Oriented Programming

(Joyce) #1

Classes and Objects


A class describes a set of objects with common characteristics. Objects are specific
instances of a class. The values contained in an object's properties are what make an
object different from other objects of the same class. The functions defined by the class
(called methods) are what implement object behaviors that are common to all objects of a
class.


When Should You Create Object-Oriented Programs


You can implement simple programming tasks as simple functions. However, as the
magnitude and complexity of your tasks increase, functions become more complex and
difficult to manage.


As functions become too large, you can break them into smaller functions and pass data
from one to function to another. However, as the number of functions becomes large,
designing, and managing the data passed to functions becomes difficult and error prone.
At this point, consider moving your MATLAB programming tasks to object-oriented
designs.


Understand a Problem in Terms of Its Objects


Thinking in terms of objects is simpler and more natural for some problems. Think of the
nouns in your problem statement as the objects to define and the verbs as the operations
to perform.


Consider the design of classes to represent money lending institutions (banks, mortgage
companies, individual money lenders, and so on). It is difficult to represent the various
types of lenders as procedures. However, you can represent each one as an object that
performs certain actions and contains certain data. The process of designing the objects
involves identifying the characteristics of a lender that are important to your application.


Identify Commonalities


What do all money lenders have in common? All MoneyLender objects can have a loan
method and an InterestRate property, for example.


Identify Differences


How does each money lender differ? One can provide loans to businesses while another
provides loans only to individuals. Therefore, the loan operation might need to be
different for different types of lending institutions. Subclasses of a base MoneyLender


Why Use Object-Oriented Design
Free download pdf