Skip to Content.
Sympa Menu

gang-of-4-patterns - [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

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


Chronological Thread 
  • 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
  • List-archive: <http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

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






Archive powered by MHonArc 2.6.16.

Top of Page