Reputation: 4149
I am currently a student learning about Java and have not learned any OO design principles. I want to modify existing group of classes: MathEditor, OCREditor, DataEditor, ParameterEditor.
I have a grasp on inheritance and such. However, each of these classes inherit a different parent. For instance MathEditor inherits Math class and OCREditor inherits OCR class. I want to add a method called changeName
to all four classes. The code inside changeName
is the exact same. Should I make an interface and make these four classes implement that? Or should I just copy/paste changeName
and manually add it to each class (I feel this is not good).
I am confused and I don't want to modify the code too much and create too many classes since I am modifying an existing code base.
Upvotes: 1
Views: 103
Reputation: 14847
Interfaces no. You will need to implement the method four times and it will be very hard to edit. But if you target Java 8, you could create an Interface with the method and a default implementation. And, even with the interface, the interface need a logic... you create an interface for a method which could fit better in current scheme?
Anyway before go to make a class and make them extends it etc.
The class names feel like they all extends an Editor class, can you check? Maybe MathEditor extends Math which extends Editor (same for others) in this case you could edit the Editor class and put that method there.
If not, i think it would be a great abstraction to create a general Editor class with all methods they should implement to be good editors (like: save (), delete ())
You need to study a bit the code
Upvotes: 1
Reputation: 309008
I would argue that you have a tenuous grip on inheritance and no clue about composition.
It's object-oriented noobs who tend to overuse inheritance. They should be taught to prefer composition.
I think there's a common Editor interface there. Each of these classes could have an instance of Editor, like this:
public interface Editor {
void changeName(String newName);
}
public class EditorImpl implements Editor {
private String name;
public void changeName(String newName) { this.name = newName; }
}
public MathEditor extends Math {
// composition: MathEditor HAS-A Editor reference
private Editor editor;
public MathEditor(Editor editor) { this.editor = editor; }
}
Upvotes: 2
Reputation: 285440
If you can't have them inherit from the same parent, then enhance by composition. Create a class, perhaps called EditorAssistant that has a changeName method, have your classes all inherit the same interface that also has a changeName method, and call the assistant's method as a delegate.
For example:
interface Editable {
void changeName(String name);
String getName();
}
class EditorAssistant implements Editable {
private String name = "";
@Override
public void changeName(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
}
class SomethingEditor implements Editable {
private EditorAssistant assistant = new EditorAssistant();
@Override
public void changeName(String name) {
assistant.changeName(name);
}
@Override
public String getName() {
return assistant.getName();
}
}
class SomeOtherEditor implements Editable {
private EditorAssistant assistant = new EditorAssistant();
@Override
public void changeName(String name) {
assistant.changeName(name);
}
@Override
public String getName() {
return assistant.getName();
}
}
Admittedly this is a trivial example, but it can scale nicely.
Upvotes: 2
Reputation: 68715
You can add a single top level parent to all current parent classes. Add method to the newly added grand parent.
Upvotes: 1