MATLAB Object-Oriented Programming

(Joyce) #1

'Callback',@(src,evnt)zoom(gobj,2),...
'Position',[100 20 60 20]);
end
end
end


The GraphInterface class implements the property set method (set.Data) to monitor
changes to the Data property. An alternative is to define the Data property as Abstract
and enable the subclasses to determine whether to implement a set access method for
this property. The GraphInterface class defines a set access method that calls an
abstract method (updateGraph, which each subclass must implement). The
GraphInterface interface imposes a specific design on the whole package of classes,
without limiting flexibility.


Method to Work with All Subclasses


The addButtons method adds push buttons for the zoom methods, which each subclass
must implement. Using a method instead of an ordinary function enables addButtons to
access the protected class data (the axes handle). Use the object zoom method as the
push-button callback.


function addButtons(gobj)
hfig = get(gobj.AxesHandle,'Parent');
uicontrol(hfig,'Style','pushbutton',...
'String','Zoom Out',...
'Callback',@(src,evnt)zoom(gobj,.5));
uicontrol(hfig,'Style','pushbutton',...
'String','Zoom In',...
'Callback',@(src,evnt)zoom(gobj,2),...
'Position',[100 20 60 20]);
end


Derive a Concrete Class — LineGraph


This example defines only a single subclass used to represent a simple line graph. It
derives from GraphInterface, but provides implementations for the abstract methods
draw, zoom, updateGraph, and its own constructor. The base class GraphInterface
and subclass are all contained in a package (graphics), which you must use to reference
the class name:


classdef LineGraph < graphics.GraphInterface


Define an Interface Superclass
Free download pdf