Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Inferring class hierarchy from within static method
Replies: 4   Last Post: Oct 10, 2012 4:55 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Julian

Posts: 93
Registered: 12/6/04
Re: Inferring class hierarchy from within static method
Posted: Oct 10, 2012 2:02 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hi Steven,

Let me give you another example that I think may more clearly establish what I'm trying to do. I disagree with your notion that the base class would need to be modified to "know" about the subclasses. One case in particular was an example of the xunit class. We created a base class which is a "test case" for Simulink files. This test case has required abstract methods "setup" and "PostProcess". The base class takes care of the "run" operation which runs setup, then runs the simulink model, then runs "PostProcess". I wanted to make an easy way for users to launch their test case in a single line from the command line, hence a static "Run" method. In this case, the base class does something like this:

methods(Static)
function Run
callingClassName = DetectCallingClass; %Magic function to determine the calling class
testobj = feval(callingClassName); %call default constructor of calling class
testobj.runTest; %run the test
end
end

In this way, once a user has authored a test case, they could execute their case via at the command line via:
TestCase.Run;

I have this pattern implemented currently, but including the Static Run method in our TestCase template that includes a call to the baseclass:
function varargout = Run(varargin)
varargout{1:nargout} = BaseClass.Run(varargin{:})
end

In this case, BaseClass can figure out the calling class via dbstack. However, if the author of the test case accidentally deletes the Run method, then we're out of luck. Also it may become difficult if there's multiple levels of inheritence.

This is a pretty simple example, but I've used this pattern in other ways to come up with an template analysis scripts, in which the "Run" method does some other very important stuff that really doesn't care who subclassed it.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.