I want to implement a factory pattern in matlab, whereby when calling the parent construction method a check is done on the input parameters and a decision is made on which child object is returned (instead of the parent one).
I am looking for a method which works for the new CLASSDEF class types.
Can anyone help?
Thanks
Hi,
I just started oop with Matlab and asked myself the same question. But since I haven't noticed any polymorphic capabilities of Matlab classes, I guess it's impossible.
Please correct me, if I am wrong.
greetz, kai
Wiki says: "Subtype polymorphism, almost universally called just polymorphism in the context of object-oriented programming, is the ability of one type, A, to appear as and be used like another type, B."
I would say you are wrong. However, I'm naive regarding OOD/OOP and I have a hard time with all those fancy concepts - design principles - patterns - you name it. Mathwork don't want to scare me off and avoids everything that might be scary to a Matlab hacker. They have succeded to write 1000+ pages of OOP documentation without using the word "pattern" once (there are two exceptions and they are titles of books they refer to).
A search for "polymorphic" and variants on the Matlab support site doesn't return anything useful - i.e. useful to answere your question.
There is no OOD/OOP book with Matlab examples (?).
I have done some excercises of "Head First Design Pattern" (Java-book). What about
classdef naivePizzaStore < handle
....
methods
function pizza = createPizza( type )
switch type
case 'cheese'
pizza = CheesePizza()
....
end
end
end
The Factory Pattern chapter comprises sixty pages. Most of the examples can be implemented in Matlab - more or less well.
/per
Nice that you have done so much reading. Did you stumble over something like a vtable or vptr? In C++ the mechanism for late binding is based on this table and the corresponding ptr.
However, your example might work for creating different subclass objects, but I doubt that Matlab can find the appropriate subclass method, if your object is of the base class type. You have to call it explicitly and therefore it is not polymorph.
Honestly, I don't know how a language can call itself object oriented if it lacks this feature.
greetz, kai
You can imitate the behavior indirectly.
Basically, you have to put all possible child properties and methods in the parent class, but then use the constructor to control which properties and methods the constructed object will be able to access without errors.
To do that, you need a class property called ChildType which sets the object type based on the input data to the constructor. You then have set() and get() methods for each property which control access to the property based on the value of ChildType.
The constructor of an object must return an object of that class, not of a
subclass. The way I would do this would be to write a factory _function_,
not a class, that performs the checking and returns the appropriate object.
Try calling the givemeanobject function I wrote below with the three classes
in the current directory, each in its own file. If you wanted to prevent
parentobj from being instantiated alone, make it an abstract class as
described in the documentation here:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brgxk22-1.html
% begin givemeanobject.m
function obj = givemeanobject(x)
if mod(x, 2) == 0
obj = evenobj(x);
else
obj = oddobj(x);
end
% begin parentobj.m
classdef parentobj
end
% begin evenobj.m
classdef evenobj < parentobj
properties
y
end
methods
function obj = evenobj(x)
obj@parentobj();
obj.y = x;
end
function tf = iseven(obj)
tf = true;
end
end
end
% begin oddobj.m
classdef oddobj< parentobj
properties
y
end
methods
function obj = oddobj(x)
obj@parentobj();
obj.y = x;
end
function tf = iseven(obj)
tf = false;
end
end
end
--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
I believe C and C++ are an April Fools prank ("CREATORS ADMIT Unix, C HOAX", http://www.gnu.org/fun/jokes/unix-hoax.html) and thus I have never read or written a line of C or C++. I've certainly not stumbled over something like vtable or vptr.
When The Mathwork introduced support for OOP in R2008a (four decades after Simula) I though I ought to try to use it .
I don't understand why "polymorph" (which I obviously don't know the meaning of) is needed to implement a factory pattern.
Weather or not language X deserves to call itselt object oriented is discussed at length elsewhere - I cannot judge.
/ per
This is a typical scenario where a factory pattern comes in very handy: Just add a new class and modify the factory. That's it. No more code has to be changed, provided the language supports polymorphism.
I know, that this is not the type of task Matlab is meant for, but it explains the advantage of polymorphism.
By the way, I don't know where your reservations about C/C++ comes from, but the link isn't working.
greetz, kai