简体   繁体   English

如何定义类和接口层次结构?

[英]How define class and interface hierarchy?

I am confused about how to define classes and interfaces hierarchy for below scenario. 我对如何为以下情况定义类和接口层次结构感到困惑。

Below are the interfaces 以下是界面

public interface Save {
    public void save(List<Object> pojoList);
    public void save(String query);
} 

public interface Update {
    public void update(List<Object> pojoList, List<String> conditionList);
    public void update(String query);
} 

public interface Delete {
    public void delete(String query);
} 

And here are the classes : 这是类:

Class DbOperations {
}

class SaveOperation extends DbOperations implements Save {
}

class UpdateOperation extends DbOperations implements Update {
} 

So my concerns are: 所以我担心的是:

  1. I want call SaveOperation, DeleteOpration class methods using instance of DbOperations (base class) 我想使用DbOperations实例(基类)调用SaveOperation,DeleteOpration类方法

  2. can you tell me which class should be which interface? 你能告诉我哪个类应该是哪个接口吗?

  3. any modification for above hierarchy?? 对以上层次有任何修改吗?

Thanks in advance 提前致谢

So regarding your concern, 因此,关于您的关注,

I want call SaveOperation, DeleteOpration class methods using instance of DbOperations (base class) 我想使用DbOperations实例(基类)调用SaveOperation,DeleteOpration类方法

I think, it will go something like this: 我认为它会像这样:

DbOperations op = new SaveOperation(/*Params*/);

// Check type to cast
if(op instanceof Save)
{
    // Cast to Save and call method
    ((Save)op).save(/*Params*/);
}

// For delete
if(op instanceof Delete)
{
    // Cast to Save and call method
    ((Delete)op).delete(/*Params*/);
}

So, you don't need any modification. 因此,您不需要任何修改。

What you get by having an interface implemented by multiple classes is that you can define a method that takes the interface as parameter and calls one of its methods, then the result of that call would depend on the actual type of that interface at runtime. 通过由多个类实现接口所获得的结果是,您可以定义一个将接口作为参数并调用其方法之一的方法,然后调用的结果将取决于运行时该接口的实际类型。

That said, I don't see any advantage in defining an interface DbOperation that doesn't define any method its classes will inherit. 也就是说,定义接口DbOperation并没有定义其类将继承的任何方法时,我看不出任何优势。

If you have reasons to do so (it's possible, if the code you wrote is just a simplification of your scenario), from a semantical point of view I would find more meaningful having DbOperations as the root interface of the hierarchy, and Save , Update and Delete as abstract classes (or interfaces) between the actual classes and the root: 如果您有这样做的理由(有可能,如果您编写的代码只是简化方案),从语义的角度来看,将DbOperations作为层次结构的根接口以及SaveUpdate会更有意义。并Delete实际类和根之间的抽象类(或接口):

public interface DbOperation {
    public String thisOperation();
}

public abstract class Save implements DbOperation {
    public String thisOperation(){
        return "Save";
    }

    public void save(List<Object> pojoList);
    public void save(String query);
} 

public abstract class Update implements DbOperation{
    public String thisOperation(){
        return "Update";
    }

    public void update(List<Object> pojoList, List<String> conditionList);
    public void update(String query);
} 

public abstract class Delete implements DbOperation {
    public String thisOperation(){
        return "Delete";
    }

    public void delete(String query);
} 

class SaveOperation implements Save {
}

class UpdateOperation implements Update {
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM