简体   繁体   中英

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)

  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)

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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