Example — Containing Assets in a Portfolio

Aggregation is the containment of one class by another class. The basic relationship is that each contained class "is a part of" the container class.

For example, consider a financial portfolio class as a container for a set of assets (stocks, bonds, savings, and so on). It can group, analyze, and return useful information about the individual assets. The contained objects are not accessible directly, but only via the portfolio class methods.

Example — A Simple Class Hierarchy provides information about the assets collected by this portfolio class.

Designing the DocPortfolio Class

The DocPortfolio class is designed to contain the various assets owned by an individual client and to provide information about the status of his or her investment portfolio. This example implements a somewhat over-simplified portfolio class that:

Displaying the Class Files

Open the DocPortfolio class definition file in the MATLAB® Editor.

To use the class, create a directory named @DocPortfolio and save DocPortfolio.m to this directory. The parent directory of @DocPortfolio must be on the MATLAB path.

Summary of the DocPortfolio Class

This class is defined in one file, DocPortfolio.m, which you must place in an @ directory of the same name. The parent directory of the @DocPortfolio directory must on the MATLAB path. See the addpath function for more information.

The following table summarizes the properties defined for the DocPortfolio class.

DocPortfolio Class Properties

Name

Class

Default

Description

Name

char

''

Name of client owning the portfolio

IndAssets

cell

{}

A cell array containing individual asset objects

TotalValue

double

0

Value of all assets (calculated in the constructor method)

The following table summarizes the methods for the DocPortfolio class.

DocBond Class Methods

Name

Description

DocPortfolio

Class constructor

disp

Displays information about this object and calls the DocAsset disp method

pie3

Overloaded version of pie3 function designed to take a single portfolio object as an argument

Property Definition Block

The following code block shows how the properties are defined:

properties
   Name = '';
end
properties (SetAccess = private) 
   IndAssets = {};
   TotalValue = 0;
end

How Class Properties Are Used

Using the DocPortfolio Class

The DocPortfolio class is designed to provide information about the financial assets owned by a client. There are three possible types of assets that a client can own: stocks, bonds, and savings accounts.

The first step is to create an asset object to represent each type of asset owned by the client:

XYZStock = DocStock('XYZ Stocks',200,12.34);
USTBonds = DocBond('U.S. Treasury Bonds',1600,3.2,2.8);
SaveAccount = DocSavings('MyBank Acc # 123',2000,6);
VictoriaSelna = DocPortfolio('Victoria Selna',...
   XYZStock,...
   SaveAccount,...
   USTBonds)

The DocPortfolio object displays the following information:

VictoriaSelna = 

Assets for Client: Victoria Selna
Description: XYZ Stocks
Date: 11-Mar-2008
Type: stock
Current Value: $  2468.00
Number of shares: 200
Share price: 12.34
Description: MyBank Acc # 123
Date: 11-Mar-2008
Type: savings
Current Value: $  2000.00
Interest Rate: 6.00%
Description: U.S. Treasury Bonds
Date: 11-Mar-2008
Type: bond
Current Value: $  1828.57
Face value of bonds: $1600
Yield: 3.20%

Total Value: $6296.57

The DocPortfolio pie3 Method provides a graphical display of the portfolio.

The DocPortfolio Constructor Method

The DocPortfolio constructor method takes as input arguments a client's name and a variable length list of asset objects (DocStock, DocBond, and DocSavings objects in this example).

The IndAssets property is a cell array used to store all asset objects. From these objects, the constructor determines the total value of the client's assets. This value is stored in the TotalValue property:

function p = DocPortfolio(name,varargin)
   p.Name = name;
   for k = 1:length(varargin)
      p.IndAssets{k} = {varargin{k}};
      asset_value = p.IndAssets{k}{1}.CurrentValue;
      p.TotalValue = p.TotalValue + asset_value;
   end
end % DocPortfolio

The DocPortfolio disp Method

The portfolio disp method lists the contents of each contained object by calling the object's disp method. It then lists the client name and total asset value:

function disp(p)
   fprintf('\nAssets for Client: %s\n',p.Name);
   for k = 1:length(p.IndAssets)
      disp(p.IndAssets{k}{1}) % Dispatch to corresponding disp
   end
   fprintf('\nTotal Value: $%0.2f\n',p.TotalValue);
end % disp

The DocPortfolio pie3 Method

The DocPortfolio class overloads the MATLAB pie3 function to accept a portfolio object and display a 3-D pie chart illustrating the relative asset mix of the client's portfolio. MATLAB calls the @DocPortfolio/pie3.m version of pie3 whenever the input argument is a single portfolio object:

function pie3(p)
% Step 1: Get the current value of each asset
   stock_amt = 0; bond_amt = 0; savings_amt = 0;
   for k = 1:length(p.IndAssets)
      if isa(p.IndAssets{k},'DocStock')
         stock_amt = stock_amt + p.IndAssets{k}.CurrentValue;
      elseif isa(p.IndAssets{k},'DocBond')
         bond_amt = bond_amt + p.IndAssets{k}.CurrentValue;
      elseif isa(p.IndAssets{k},'DocSavings')
         savings_amt = savings_amt + p.IndAssets{k}.CurrentValue;
      end % if
   end % for

% Step 2: Create labels and data for the pie graph
   k = 1;
   if stock_amt ~= 0
      label(k) = {'Stocks'};
      pie_vector(k) = stock_amt;
      k = k + 1;
   end % if
   if bond_amt ~= 0
      label(k) = {'Bonds'};
      pie_vector(k) = bond_amt;
      k = k + 1;
   end % if
   if savings_amt ~= 0
      label(k) = {'Savings'};
      pie_vector(k) = savings_amt;
   end % if

% Step 3: Call pie3, adjust fonts and colors
   pie3(pie_vector,label);set(gcf,'Renderer','zbuffer')
   set(findobj(gca,'Type','Text'),...
   'FontSize',14,'FontWeight','bold')
   colormap prism
   stg(1) = {['Portfolio Composition for ',p.Name]};
   stg(2) = {['Total Value of Assets: $',num2str(p.TotalValue,'%0.2f')]};
   title(stg,'FontSize',10)
end % pie3

There are three parts in the overloaded pie3 method.

Visualizing a Portfolio

You can use a DocPortfolio object to present an individual's financial portfolio. For example, given the following assets:

XYZStock = DocStock('XYZ Stocks',200,12.34);
USTBonds = DocBond('U.S. Treasury Bonds',1600,3.2,2.8);
SaveAccount = DocSavings('MyBank Acc # 123',2000,6);
VictoriaSelna = DocPortfolio('Victoria Selna',...
            XYZStock,...
            SaveAccount,...
            USTBonds);

you can use the class's pie3 method to display the relative mix of assets as a pie chart.

pie3(VictoriaSelna)

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS