Re: Class Design Question



Cool Guy <coolguy@xxxxxxx> wrote in message news:<qu1othydy4or.dlg@xxxxxxxxxxxxxxxx>...
> I have a class which holds key-value pairs where the keys are common
> Windows file extensions and the values are associated file content
> descriptions (for each extension).
>
> This class has a method which gets a value given a key.
>
> e.g.:
>
> class FileDescriptionTable {
> public void Get(string fileExtension) {
> return GetDescription(fileExtension);
> }
> }
>
> I was considering changing it so that, instead of taking a key (a file
> extension), it takes a full file name, and determines the extension itself
> (by making a routine call):
>
> class FileDescriptionTable {
> public void Get(string fileName) {
> return GetDescription(FileUtility.GetExtension(fileName));
> }
> }
>
> Is this a good idea, or would it give FileDescriptionTable too much
> responsibility? I can't quite decide.


Hej!

By naming your class, "FileDescriptionTable," you are nominally
defining it as connected with files, so I think passing it the full
file name and letting it get the extension seems reasonable. This
class is defined by its handling of files, so adding some more
file-handling functionality looks fine.

If, however, your application has another entity whose name
necessarily defines an associated description, for example a GUI
button and a tooltip, or a database field header and a description of
the entries that are allowed under that header, then you could
probably re-use some of the above code, in which case you'd change the
class name from, "FileDescriptionTable," to something like,
"KeyDescriptionTable." If you do this, then it may no longer be
appropriate for such a generic class to access something as specific
as a FileUtility, and so passing just the file extension might be a
better solution.

(Note that if you do take the latter approach, you can have the best
of both worlds: instead of get(String fileName), you could pass in an
abstract class get(Key key), and Key would have method getName() to
return a string; thus, if a subclass FileNameKey could be
parameterised by a file name and deduce the extention via getName(),
and FileExtentionKey could be parameterised by just the file extention
and return this unaltered via getName().)

..ed

www.EdmundKirwan.com - Home of The Fractal Class Composition.
.


Quantcast