MATLAB Object-Oriented Programming

(Joyce) #1
class can specialize the subclass versions of the loan method. Each lender can have a
different value for its InterestRate property.

Factor out commonalities into a superclass and implement what is specific to each type of
lender in the subclass.
Add Only What Is Necessary

These institutions might engage in activities that are not of interest to your application.
During the design phase, determine what operations and data an object must contain
based on your problem definition.

Objects Manage Internal State

Objects provide several useful features not available from structures and cell arrays. For
example, objects can:


  • Constrain the data values assigned to any given property

  • Calculate the value of a property only when it is queried

  • Broadcast notices when any property value is queried or changed

  • Restrict access to properties and methods


Reducing Redundancy

As the complexity of your program increases, the benefits of an object-oriented design
become more apparent. For example, suppose that you implement the following
procedure as part of your application:

(^1) Check inputs
(^2) Perform computation on the first input argument
(^3) Transform the result of step 2 based on the second input argument
(^4) Check validity of outputs and return values
You can implement this procedure as an ordinary function. But suppose that you use this
procedure again somewhere in your application, except that step 2 must perform a
different computation. You could copy and paste the first implementation, and then
rewrite step 2. Or you could create a function that accepted an option indicating which
computation to make, and so on. However, these options lead to more complicated code.
An object-oriented design can factor out the common code into what is called a base
class. The base class would define the algorithm used and implement whatever is common
1 Using Object-Oriented Design in MATLAB

Free download pdf