The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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 ]

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:

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

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

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{:})

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]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.