469,086 Members | 1,273 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,086 developers. It's quick & easy.

Which design pattern to choose in this situation?

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!

Apr 8 '06 #1
4 2129
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
Apr 8 '06 #2
Guch Wu wrote:
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
--
Please remove capital As from my address when replying by mail
Apr 8 '06 #3
In article <11**********************@g10g2000cwb.googlegroups .com>,
"Guch Wu" <gu****@gmail.com> wrote:
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.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 8 '06 #4
Guch Wu wrote:
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.

--
Ian Collins.
Apr 8 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by MasterBlaster | last post: by
3 posts views Thread by Steve | last post: by
2 posts views Thread by Matthew Hood | last post: by
6 posts views Thread by pitachu | last post: by
22 posts views Thread by Krivenok Dmitry | last post: by
2 posts views Thread by LarryTheSoftwareGuy | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.