Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to implement a factory pattern in matlab using the new CLASSDEF classes?

522 views
Skip to first unread message

Riccardo

unread,
Aug 5, 2009, 5:13:01 AM8/5/09
to
Hi,

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

Kai

unread,
Nov 11, 2009, 4:26:06 PM11/11/09
to
"Riccardo" <riccardo....@swri.org> wrote in message <h5bies$oru$1...@fred.mathworks.com>...

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

per isakson

unread,
Nov 12, 2009, 6:31:03 AM11/12/09
to
"Kai " <kai.voge...@gmx.net> wrote in message <hdfa5d$3fh$1...@fred.mathworks.com>...

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

Kai

unread,
Nov 12, 2009, 8:59:03 AM11/12/09
to
"per isakson" <poi.n...@bimDOTkthDOT.se> wrote in message <hdgrln$j14$1...@fred.mathworks.com>...

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

Matt

unread,
Nov 12, 2009, 10:02:04 AM11/12/09
to
"Riccardo" <riccardo....@swri.org> wrote in message <h5bies$oru$1...@fred.mathworks.com>...
> Hi,
>
> 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.

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.

Steven Lord

unread,
Nov 12, 2009, 10:32:48 AM11/12/09
to

"Kai " <kai.voge...@gmx.net> wrote in message
news:hdfa5d$3fh$1...@fred.mathworks.com...

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


per isakson

unread,
Nov 12, 2009, 3:27:02 PM11/12/09
to
"Kai " <kai.voge...@gmx.net> wrote in message <hdh4b7$8ru$1...@fred.mathworks.com>...

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



Kai

unread,
Nov 12, 2009, 4:17:01 PM11/12/09
to
"per isakson" <poi.n...@bimDOTkthDOT.se> wrote in message <hdhr2m$5ik$1...@fred.mathworks.com>...
Let's stay with the pizza example: You have a cheese pizza and a calzone. Both, the interface regulates that, have a method 'bake'. This bake method differs in some points, e.g. temperature, baking time.... While running the program the user decides to bake a calzone. Now, you, the programer, have to ensure programmaticaly, that the appropriate calzone.bake method is called. To track the type of the pizza throughout the whole code will result in even more code. Imagine it is a program for a restaurant not only with two, but 15 types of pizza. It ll be a lot of fun to program that. And it ll be even more fun if the chef of kitchen creates a new type of pizza, which then has to be added to the code.

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

0 new messages