OOP in Matlab: Typical Workflow for Defining Class

This tutorial presents an example discussing the typical workflow of object oriented programming in Matlab.

Define a class representing a bank account
saved in m- file BankAccount.m

classdef BankAccount < handle
   properties (Hidden)
      AccountStatus = 'open';
   end
   
   properties (SetAccess = private)
      AccountNumber
      AccountBalance = 0;
   end
   
   events
      InsufficientFunds
   end
   
   methods
      function BA = BankAccount(AccountNumber,InitialBalance)
         if nargin < 2
            error('BankAccount:InvalidInitialization',...
               'Must provide an account number and initial balance')
         end
         BA.AccountNumber  = AccountNumber;
         BA.AccountBalance = InitialBalance;
         AccountManager.addAccount(BA);
      end % BankAccount
      
      function deposit(BA,amt)
         BA.AccountBalance = BA.AccountBalance + amt;
         if BA.AccountBalance > 0
            BA.AccountStatus = 'open';
         end
      end % deposit
      
      function withdraw(BA,amt)
         if (strcmp(BA.AccountStatus,'closed')&& BA.AccountBalance < 0)
            disp(['Account ',num2str(BA.AccountNumber),' has been closed.'])
            return
         end
         newbal = BA.AccountBalance - amt;
         BA.AccountBalance = newbal;
         if newbal < 0
            notify(BA,'InsufficientFunds')
         end
      end % withdraw
   end % methods
end % classdef

 

Define a class representing account management
saved in m-file: AccountManager.m

classdef AccountManager
% Instantiate a listener for the bankaccount class
 
   methods (Static)      
      function assignStatus(BA)
         if BA.AccountBalance < 0  
            if BA.AccountBalance < -200
               BA.AccountStatus = 'closed';
            else
               BA.AccountStatus = 'overdrawn';
            end
         end
      end
      function addAccount(BA)
      % Add a listener for Insufficient Funds
         addlistener(BA, 'InsufficientFunds', ...
            @(src, evnt)AccountManager.assignStatus(src));
      end
   end
end

Remarks

  • In the class BankAccount, the properties AccountNumber and AccountBalance can only be changed by the class itself, so SetAccess = private, meaning that only class methods can set these values.
  • An external program can set the value of AccountStatus in the class BankAccount, so SetAccess = public, meaning that any code can access this property value.
  • In the class BankAccount, an event of InsufficientFunds is defined by the events block.
    • A listener (for the InsufficientFunds event) is registered in the constructor BankAccount(…) on the instance of BankAccount class, by calling the addAccount(…) method of the AccountManager class.
      Note here that, since the addAccount(…) method is static, the class (not instance) name AccountManager is required. Indeed, the AccountManager class is never instantiated.
    • The withdraw(…) send an event notice (to its listeners) if the triggering condition, low balance, is met.
  • The AccountManager class responds to event notice from instance of BankAccount class by
    • Use method addAccount(…) to register itself as a listener to the notice of InsufficientFunds event sent from the instance of BankAccount class.
    • Responding to event notice, the assignStatus(…) method is called to change the AccountStatus property in the BankAccount instance.
  • Note that the AccountManager class is never instantiated. It serves as a container for the event listener used by all BankAccount objects.

 

Using of BankAccount class

BA = BankAccount(1234567,500);
BA.AccountNumber
ans =
1234567
BA.AccountBalance
ans =
   500
BA.AccountStatus
ans =
open

BA.withdraw(600)
BA.AccountBalance
ans =
  -100
BA.AccountStatus
ans =
overdrawn

BA.withdraw(200)
BA.AccountBalance
ans =
  -300
BA.AccountStatus
ans =
closed

BA.withdraw(100)
Account 1234567 has been closed

BA.deposit(700)
BA.AccountStatus
ans =
open
BA.withdraw(100)
BA.AccountBalance
ans =
  300

 

Tags
Subject
Category
Coding language