Question

For the sake of an example I have a WAV class which represents WAV sound files. For convenience' sake, I am then required to add a class called WaveformAudioFile which has exactly the same functionality.

Regardless of possible design/style issues, what would be an appropriate way to integrate the new class ? By using an extends (with nothing added), by using a common interface or something else ?

Edit: Thinking of this further, if design was to be considered would there be an alternative to avoid redundant code ? I mean writing interfaces would be tiresome if there are several classes. Some sort of pattern that could be used to init. attributes ?

Was it helpful?

Solution

1. From design point of view, if you want your classes to play a specific role, then go for Interface.

2. If you have behaviors that keeps changing, then its better to encapsulate them in either Abstract class or Interface , then extend or implement it to specify certain functionality.

//////////////////////////////////////////Edited ://////////////////////////////

I think you are referring to the DRY principle (Dont Repeat Yourself), according to this principle, its better to keep your Information and Behaviors in Single Sensible Place.

But what if the Behaviors are different for different classes for the same functionality.

For eg:

Consider Painting as a Class with paint() Method.

Now

paint() method can have Stroking, gliding, shading etc styles of doing it.

Then its better to Encapsulate that method into an Abstract class or an Interface.

public interface Paint{

paintDoIt(String style);

}

Its Not about a tiresome work, but about flexibility and having the capability to make changes without touching too many classes and without breaking the code.

Only One thing is a Constant in Software Engineering and that is CHANGE.

So we need to adopt to it.

OTHER TIPS

Interface is the best solution from my point of view. Declare a common interface for classes who implement such functionality and use this interface instead of direct class type definition.

For instance:

ISoundFile mySoundFile = getSoundFile();

Where ISoundFile is your declared interface.

Use a common interface and implement it in both of the classes. One major benifit is your WAVE and all classes then can extend any other classes also .

Just adding this as it might interest someone. I've thought of another solution and that's to simply use a wrapper class :) .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top