Question:
Class design question?
anonymous
2008-12-20 10:42:55 UTC
How do you deal with a class that is nicely designed, but you want to add a new function to it, which doesn't really fit in the context of the original class? I suppose the options are to modify the original class, or start a new class that inherits from it. But even still, it seems wrong for me to start a new class. Here's an example. What if I made a class that describes a video file. Let's call it Class Video. Methods are getNumberOfFrames, getFrame, getResolution, etc. Now suppose I want to add a new method called convertToMPEG. I don't know if it really fits into the context of the Video class, and if I inherited the Video class, what would I call the new class? I feel like adding that method ruins the "purity" of the original class.
Three answers:
stephene
2008-12-20 11:36:22 UTC
This sounds like a good job for a static Utility class.

Instead of modifying the first class or extending it into another class, you create a second, independent class called... VideoUtils.



Then you can add static methods on it that might be convenient to use from numerous classes. Those methods might be

VideoUtils.convertToMPEG();

VideoUtils.getPhotoStills(int secondsInterval). Etc..



This is done in a few places in the Java API like the static methods of the Collections and Array classes, etc.



Cheers.

-----

Edit: Just wanted to say that I think you have good instincts. It's good to keep the focus of classes to a good level of logical conciseness. This may lead to you having more classes in your program, but it will make it easier to understand for other developers. For instance, if you wanted to code some IO functions for reading and saving your Video class, it might make sense for you to write a class called VideoManager. That knows how to make Video objects play nice together. I've read a bunch of programming-snobs who kick OO around for the large number of classes you have to create, but I've found that it leads to less bugs and to me, it's worth the tradeoff.
el e-phant
2008-12-20 11:40:49 UTC
if all of your derived classes are going to (eventually) support convertToMPEG, modify your class and make it a virtual function and override it when appropriate in the derived class,

if only some of your derived (think of them as 'specializations' - vehicle - car - coupe...) classes are going to support this function then maybe they ought to be a level lower?



base class - vehicle

derived class - boat

function - hasRudder()... that'd only from boat down but

getNumberOfSeats() - would go in vehicle



I guess it depends, what happens if your video already is an mpeg? and convert implies you're changing the type of the instance that would present problems



is the class closed for modification but open for extension?

you need to think about how many people will whine if you alter the base class and how many people will whine if you create a new derived class if you don't :)



----

yeah, stephene's answer's better, that makes sense :)
no1home2day
2008-12-20 10:50:32 UTC
You create a new class and use the "Inherits" clause and then your new class has all the attributes, functions, etc, of the original, and then you add your own functions, attributes, etc.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...