Skip to Content.
Sympa Menu

gang-of-4-patterns - Re: [gang-of-4-patterns] Create object without know type of object

gang-of-4-patterns AT lists.cs.illinois.edu

Subject: Design Patterns discussion

List archive

Re: [gang-of-4-patterns] Create object without know type of object


Chronological Thread 
  • From: Jesús Alonso <kenchoweb AT hotmail.com>
  • To: russ AT pentad.com
  • Cc: gang-of-4-patterns AT cs.uiuc.edu
  • Subject: Re: [gang-of-4-patterns] Create object without know type of object
  • Date: Fri, 04 May 2007 02:04:13 +0200
  • List-archive: <http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

Thanks Russ, that is a very interesting reading.


From: Russ Rufer
<russ AT pentad.com>
To: Jesús Alonso
<kenchoweb AT hotmail.com>
CC:
patterns AT celticblues.com,

gang-of-4-patterns AT cs.uiuc.edu
Subject: Re: [gang-of-4-patterns] Create object without know type of object
Date: Thu, 03 May 2007 16:06:12 -0700

Hi Ed and Jesús,

The hints Jesús gave at the end of his message capture the essence of a pattern named Product Trader. You can find more details here:

Product Trader
by Dirk Baumer and Dirk Riehle
http://www.riehle.org/computer-science/research/1996/plop-1996-product-trader.pdf


- Russ

Jesús Alonso wrote:
Hi Ed,

The Factory Method is the way to go here in my opinion. You receive the filename and substract the extension to know the file type. Then you take the extension and use it as a filter to choose the right class to instance. It's probably the most lightweight and easy to implement solution, and it sure gives great results, so I suggest you to use it, yes :)

The abstract factory will only be useful in case you need to instance many other classes depending on the file name. I guess you only want to instantiate a FileWhatever class, so Abstract Factory would be meaningless here.

However, you expressed that you want to extend the supported file types in the future, so I think you should take an approach similar to the Abstract Factory, but simpler. On the application load, register a number of factories in the file reader, passing its discriminator as an extra parameter. Something like:
myFileReader->registerFileType ("jpg", new JpegFileTypeFactory ());
You would store the discriminator and the factory reference in a std::map<std::string,FileTypeFactory*>. That should allow you to instantiate the right file type doing:
BaseFile *newFile = factories[fileExtension]->load (filename);
Pretty straightforward and easy to read if you have some knowledge on the STL containers.

Hope that helped!


From:
patterns AT celticblues.com
To:
gang-of-4-patterns AT cs.uiuc.edu
Subject: [gang-of-4-patterns] Create object without know type of object
Date: Thu, 03 May 2007 12:58:15 -0400

Let's I am writing some code that will read two or more different file
types. Assume that I will have a BaseFile class, and two subclasses.
With the load of specific file contents performed in the Load method.

BaseFile
{
.
.
.
virtual bool Load() = 0;
.
.
.
};

FileTypeOne: public BaseFile
{
.
.
.
bool Load();
.
.
.

};

FileTypeTwo: public BaseFile
{
.
.
.
bool Load();
.
.
.

};


What I need, is a way to create a new file object type without
necessarily knowing which type of file is going to be read. For
example, if given the name of the file to be read, I pass it to some
function/object and it will return a ptr to a new correct type of
object.

BaseFile *newFile = CreateFile(filename)

Seems like this may be a job for a factory or factory method but I am
not sure. What pattern would best suit this functionality?

To make matters more confusing, this is for a real time simulation, so
it needs to be lean and mean, and preferrably simple to implement and
update as new file types are defined.

Thanks,
Ed


_______________________________________________
gang-of-4-patterns mailing list
gang-of-4-patterns AT cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/gang-of-4-patterns


_________________________________________________________________
Acepta el reto MSN Premium: Protección para tus hijos en internet. Descárgalo y pruébalo 2 meses gratis. http://join.msn.com?XAPID=1697&DI=1055&HL=Footer_mailsenviados_proteccioninfantil

_______________________________________________
gang-of-4-patterns mailing list
gang-of-4-patterns AT cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/gang-of-4-patterns





_________________________________________________________________
Dale rienda suelta a tu tiempo libre. Mil ideas para exprimir tu ocio con MSN Entretenimiento. http://entretenimiento.msn.es/





Archive powered by MHonArc 2.6.16.

Top of Page