Java design/implementation question
I'm creating a small project which will manipulate some internal components of the project(big one).
Now every components does something in its own way but its basically a same thing. For ex: Each component can delete temp files stored inside it. But each temp files are of a different type, like in component 1, temp files are type Object1 and in another component Objectx etc.
So I开发者_C百科 though of creating a class called Manager, which will contain methods like delete inside, and there will be componentManager extending the Manager class and providing the implementation for methods inside.
Should I make Manager abstract?Small problem, lets say Manager has these two methods.
public void delete(Object1 obj){
}
public void delete(Objectx obj){
}
Component1 will use first delete and some other component will use other delete.
Or should I implement them seperately all together without having to extend the same class?
What would be nice way to implement this? thank you
I think you should create an Interface Deleteable with the method
public void delete()
and your objects Object1, ..., Objectx should implement it. Your Manager could have the method:
public void delete(Deleteable obj) {
obj.delete();
}
I'm not sure if you meant it this way I understood you. E.g. Component1 got only Object1 files while Component2 got onyl Object2 files?
I'm not sure with your solution, if you meant it like i thought, what about generics?
interface Component<T extends YourObjectsAncestor> {
void delete(T t);
}
class Component1 implements Component<Object1> {
public void delete(Object1 t) {
}
}
class Component2 implements Component<Object2> {
public void delete(Object2 t) {
}
}
Note that this approach will be useful only if each component should be bound to one concnrete "file" (as you named it). e.g. Component1 has only files of type Object1 and so on..
精彩评论