Which design pattern to choose in this situation?

G

Guch Wu

I want to design an image processing class as follow:

class Image
{
Image Data;
General Image Operations;
read(filename, File_Type);
write(filename, File_Type);
};

The Image Data is initialized by the read function according to the
File_Type, and save to a file by write function according to the File
Type.

The problem is:
Whenever adding a file type, I have to add two functions(read and
write) to the class Image.
I want to know if there is a design pattern which bring less
modification to the class Image when adding a file type.

Any suggestion will be appreciated!
 
T

Tomás

class Image
{
Image Data;
General Image Operations;
read(filename, File_Type);
write(filename, File_Type);
};


No offence intended, but you don't know what you're doing. I suggest you
read up on some good examples of writing classes.


-Tomás
 
V

Victor Bazarov

Guch said:
I want to design an image processing class as follow:

class Image
{
Image Data;
General Image Operations;
read(filename, File_Type);
write(filename, File_Type);
};

The Image Data is initialized by the read function according to the
File_Type, and save to a file by write function according to the File
Type.

The problem is:
Whenever adding a file type, I have to add two functions(read and
write) to the class Image.
I want to know if there is a design pattern which bring less
modification to the class Image when adding a file type.

Any suggestion will be appreciated!

I think more generic approach would be to design a special ImageReader
class and derive all particular image readers from it. Then, any time
you need to read an image, you instantiate a particular reader and give
its pointer/reference to the image. The interaction between the image
and the generic reader is the difficult part to design. But it would
be best for this particular situation, I think.

V
 
D

Daniel T.

"Guch Wu said:
I want to design an image processing class as follow:

class Image
{
Image Data;
General Image Operations;
read(filename, File_Type);
write(filename, File_Type);
};

The Image Data is initialized by the read function according to the
File_Type, and save to a file by write function according to the File
Type.

The problem is:
Whenever adding a file type, I have to add two functions(read and
write) to the class Image.
I want to know if there is a design pattern which bring less
modification to the class Image when adding a file type.

Any suggestion will be appreciated!

I don't think there is any way to do what you want. There are things
that you can do to limit the size of the read and write functions by
reusing like structures within them, but the only way to eliminate
needing them is to make some read only or write only (depending on what
you are writing, this may be a good idea anyway.)

I suggest, however, that you read up on the Strategy pattern.
 
I

Ian Collins

Guch said:
I want to design an image processing class as follow:

class Image
{
Image Data;
General Image Operations;
read(filename, File_Type);
write(filename, File_Type);
};

The Image Data is initialized by the read function according to the
File_Type, and save to a file by write function according to the File
Type.

The problem is:
Whenever adding a file type, I have to add two functions(read and
write) to the class Image.
I want to know if there is a design pattern which bring less
modification to the class Image when adding a file type.

Any suggestion will be appreciated!
I agree with Victor, keep the read/write operations out of the class.

There are many ways to do this, one approach would be to use some for of
raw image object which only has read and write members. You could pass
a reference to one of these to your Image constructor, or make your
Image a template, or provide streaming operators, or....

If you follow Test Driven Development, you design will find the most
appropriate design pattern, don't tie your self to a pattern too early.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top