Alternate Constructor Behavior with inputParser
13 次查看(过去 30 天)
显示 更早的评论
Hi,
I have several classes whose constructors rely on InputParser to collect all the relevant properties. I feed all of the varargin array to the inputParser object. Then I loop over the results struct of the inputParser object and set each field of my class instance equal to the value of the matching field of the struct.
parser.parse(varargin{:});
results = parser.Results;
field_list = fields(results);
for field_index = 1:length(field_list)
field = field_list{field_index};
self.(field) = results.(field);
end
While this works well and allows me to avoid complex syntax in the constructor (not to mention implementation logic), I don't have a good way to emulate multiple-constructor behavior. As I understand it, the standard way to have a non-default constructor is to switch on nargin inside the main constructor. With the inputParser approach, though, that's not reliable (as some inputs are optional, I can't predict nargin with any certainty). I could probably add an optional argument(s), but it's still awkward.
My typical use case is that I need to build an object directly from a string or input file with a syntax defined externally. While in other languages I'd probably use a secondary constructor that takes in either the file path or a string, in MATLAB the best I've come up with is to have a separate function called load_whatever.m that takes in the non-default constructor arguments and calls the default (only) constructor in the usual way. (This is slightly reminiscent of the Factory Design Pattern, but I don't know if the inventors of that one would approve.) Unfortunately, this involves moving class-specific code outside of the class definition file, and is neither object-oriented nor easily configurable.
The only other thing I can think of would be to have a static class method that could act as a secondary constructor. I have a feeling that's a bad idea on more than one count, so I haven't tried that yet.
What's the preferred practice for doing things in single-constructor languages like MATLAB that in multiple-constructor languages would use a second constructor? Is the thing I want to do here even one of those things?
Thanks, Jay
1 个评论
Jiro Doke
2011-4-6
Can you elaborate on what type constructor signature you want to have? Is the example below your actual case, i.e. two input constructor and a structure input constructor?
I'm not promising anything, but maybe if we have an idea of what exact constructor behaviors you're looking for, we can try to figure it out.
采纳的回答
Jay
2011-4-6
2 个评论
Jiro Doke
2011-4-6
I'm kind of confused. Typically, if you want to allow an equivalent structure input, then I feel that the corresponding non-structure syntax should be a param-value pair. For example, one might use this syntax:
a = classA('foo', 1, 'bar', 0)
or this syntax:
b = classA(struct('foo', 2, 'bar', 0))
In this case, inputParser would take care of it with the "addParamValue" method.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Argument Definitions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!