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

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

From: Riccardo

Date: 5 Aug, 2009 09:13:01

Message: 1 of 8

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

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

From: Kai

Date: 11 Nov, 2009 21:26:06

Message: 2 of 8

"Riccardo" <riccardo.meldolesi@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.
>
> 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

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

From: per isakson

Date: 12 Nov, 2009 11:31:03

Message: 3 of 8

"Kai " <kai.voges.nospam@gmx.net> wrote in message <hdfa5d$3fh$1@fred.mathworks.com>...
> "Riccardo" <riccardo.meldolesi@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.
> >
> > 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

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

From: Kai

Date: 12 Nov, 2009 13:59:03

Message: 4 of 8

"per isakson" <poi.nospam@bimDOTkthDOT.se> wrote in message <hdgrln$j14$1@fred.mathworks.com>...
> "Kai " <kai.voges.nospam@gmx.net> wrote in message <hdfa5d$3fh$1@fred.mathworks.com>...
> > "Riccardo" <riccardo.meldolesi@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.
> > >
> > > 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

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

From: Matt

Date: 12 Nov, 2009 15:02:04

Message: 5 of 8

"Riccardo" <riccardo.meldolesi@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.

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

From: Steven Lord

Date: 12 Nov, 2009 15:32:48

Message: 6 of 8


"Kai " <kai.voges.nospam@gmx.net> wrote in message
news:hdfa5d$3fh$1@fred.mathworks.com...
> "Riccardo" <riccardo.meldolesi@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.
>>
>> 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

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
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

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

From: per isakson

Date: 12 Nov, 2009 20:27:02

Message: 7 of 8

"Kai " <kai.voges.nospam@gmx.net> wrote in message <hdh4b7$8ru$1@fred.mathworks.com>...
> "per isakson" <poi.nospam@bimDOTkthDOT.se> wrote in message <hdgrln$j14$1@fred.mathworks.com>...
> > "Kai " <kai.voges.nospam@gmx.net> wrote in message <hdfa5d$3fh$1@fred.mathworks.com>...
> > > "Riccardo" <riccardo.meldolesi@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.
> > > >
> > > > 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

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



 




 

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

From: Kai

Date: 12 Nov, 2009 21:17:01

Message: 8 of 8

"per isakson" <poi.nospam@bimDOTkthDOT.se> wrote in message <hdhr2m$5ik$1@fred.mathworks.com>...
> "Kai " <kai.voges.nospam@gmx.net> wrote in message <hdh4b7$8ru$1@fred.mathworks.com>...
> > "per isakson" <poi.nospam@bimDOTkthDOT.se> wrote in message <hdgrln$j14$1@fred.mathworks.com>...
> > > "Kai " <kai.voges.nospam@gmx.net> wrote in message <hdfa5d$3fh$1@fred.mathworks.com>...
> > > > "Riccardo" <riccardo.meldolesi@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.
> > > > >
> > > > > 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
>
> 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
>
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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
design pattern Kai 12 Nov, 2009 09:04:07
polymorphism Kai 12 Nov, 2009 09:04:07
factory Kai 12 Nov, 2009 09:04:07
oop per isakson 5 Aug, 2009 09:00:21
rssFeed for this Thread

Contact us at files@mathworks.com