|
"Steven_Lord" <slord@mathworks.com> wrote in message <k15bcl$6vb$1@newscl01ah.mathworks.com>...
>
>
> "Ben Ruppel" <brspam@cox.net> wrote in message
> news:k13sb4$1oh$1@newscl01ah.mathworks.com...
> > I am trying to make it easy to switch out certain behaviors of an object
> > in Matlab (kind of the like the "Strategy" design pattern, but without
> > bothering to do the interface part). What I want to do is assign a string
> > to a variable. This string will match the name of a class definition file
> > I have. I want to create a new object based on this class definition
> > using the string variable contents.
> > So for instance:
> > string = 'behaviorA'
> > ....
> > thisBehavior = string % <- I want this line of code to tell matlab to
> > create a new object of type 'behaviorA'
> >
> > %then my code can do whatever the object based on the class def does:
> > thisBehavior.whatIdo(....)
> >
> > so the problem is that matlab doesn't take the hint that "thisBehavior =
> > string"
> > wants it to create a new instance of the object with a name specified
> > within 'string'.
>
> Yes. If it did, you wouldn't be able to define the variable string in the
> first place, as that would need to be considered a request to construct a
> behaviorA for consistency.
>
> > I tried surrounding "string" with parentheses (this is what I would do if
> > I was trying to use a string to specify a component of a data structure)
> > but this doesn't work.
> >
> > Anyone have any ideas?
> >
> > There is a str2func function that kind of does what I want, but for
> > functions instead of objects. I think.
>
> There are several ways to do what you're looking for, and the one I'm going
> to recommend is to step away from specifying the class name as a string.
> Instead specify it as a function handle to the constructor of the object:
>
> makeANew = @behaviorA;
> thisBehavior = makeANew(requiredInputsForConstructorOfbehaviorA);
>
> If the constructor does not require any inputs, you will still need to use
> the parentheses.
>
> thisBehavior = makeANew();
>
> Slightly less recommended is to convert the string into a function handle:
>
> string = 'behaviorA';
> makeANew = str2func(string);
> thisBehavior = makeANew(requiredInputsForConstructorOfbehaviorA);
>
> The reason I recommend skipping the string step entirely is that it will
> avoid this problem if you plan to create a standalone application from your
> code:
>
> http://www.mathworks.com/help/toolbox/compiler/br2cqa0-2.html#br2cqa0-5
>
> The explicit function handle tells MATLAB that it will need the behaviorA
> function/constructor. The string + str2func doesn't make that explicit.
>
> If you only care about a small number of different potential classes, you
> could use a factory function.
>
> function obj = buildMeA(classname, varargin)
> switch classname
> case 'behaviorA'
> obj = behaviorA(varargin{:});
> case 'behaviorB'
> obj = behaviorB(varargin{:});
> otherwise
> error('I don''t know what to make.');
> end
>
> The explicit calls to the constructors inside buildMeA avoid the standalone
> application issue.
>
> There's at least one other way to do this, but I don't recommend it on
> general principles.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
Thank you very much Steve! I will give your recommended approach a shot. The factory function also occurred to me, but I was looking for a way to do this without having to edit a case statement. Mostly it is an exercise in seeing what I can do with the language :) Thanks again!
|