Java设计/实现问题

发布于 2024-10-06 20:32:43 字数 491 浏览 3 评论 0原文

我正在创建一个小项目,它将操纵项目的一些内部组件(大项目)。

现在每个组件都以自己的方式做一些事情,但基本上是相同的事情。例如: 每个组件都可以删除存储在其中的临时文件。但是每个临时文件都是不同的类型,比如在组件1中,临时文件的类型为Object1,而在另一个组件Objectx等中。

所以我想创建一个名为Manager的类,其中将包含诸如delete之类的方法,并且会有componentManager扩展 Manager 类并提供内部方法的实现。

我应该让 Manager 抽象吗?小问题,假设 Manager 有这两个方法。

public void delete(Object1 obj){
}

public void delete(Objectx obj){
}

Component1 将使用第一个删除,其他一些组件将使用其他删除。

或者我应该将它们单独实现而不必扩展同一个类?

实现这个的好方法是什么?谢谢

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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆兒滾 2024-10-13 20:32:43

我认为您应该使用该方法创建一个可删除接口,

public void delete()

并且您的对象 Object1, ..., Objectx 应该实现它。您的经理可以采用以下方法:

public void delete(Deleteable obj) {
    obj.delete();
}

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();
}
瑾兮 2024-10-13 20:32:43

我不确定你的意思是不是我理解你的意思。
例如,Component1 仅获得 Object1 文件,而 Component2 仅获得 Object2 文件?

我不确定你的解决方案,如果你的意思像我想的那样,那么泛型呢?

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) {
    }   
}

请注意,只有当每个组件都应绑定到一个具体的“文件”(如您所命名的)时,这种方法才有用。例如 Component1 仅具有 Object1 类型的文件,依此类推。

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文