简体   繁体   English

实现通用接口和继承

[英]Implementing Generic Interfaces and inheritance

Trying to implement a generic and inheritance classes in a Java Project. 试图在Java项目中实现泛型和继承类。 For that reason I created the "Base" classes and Interfaces that will inheritance all the specific classes and interfaces. 因此,我创建了将继承所有特定类和接口的“基础”类和接口。

I mean, I have a Base Interface 我的意思是,我有一个基本接口

public interface BaseServices<T> {
    public boolean validate(T item, int id) throws Exception;
}

And a Base Implementation for that Interface 以及该接口的基本实现

public class BaseServicesImpl implements BaseServices<BaseBO> {

@Autowired
BaseDao dao;
CategoryBO bo;

public BaseServicesImpl()
{
    this.bo = new CategoryBO();
}

    @Override
       public boolean validate(BaseBO item, int id) throws Exception {
       return dao.validate("name",item.toString(), id);
    } 
}

Where BaseBO is the business object that ALL the other objects will extend. 其中BaseBO是所有其他对象都将扩展的业务对象。

And then, for an specific object, I will have a particular Interface which extends the Base one. 然后,对于一个特定的对象,我将具有一个特定的接口,该接口扩展了Base接口。

public interface CategoryServices extends BaseServices {
    }

And its implementation: 及其实现:

public class CategoryServicesImpl extends BaseServicesImpl implements CategoryServices<CategoryBO> {

    @Autowired
    CategoryDao categoryDao;

    public CategoryServicesImpl()
    {
        this.dao = categoryDao;
    }

    @Override
    public boolean validate(CategoryBO item, int id) throws Exception {
            return dao.validate("name",item.getName(), id);
    }
}

Where some object can implement the generic "Validate" method (which validates just the name) or extend it to what the class requiredpublic class CategoryServicesImpl extends BaseServicesImpl implements CategoryServices. 在某些对象可以实现通用“ Validate”方法(仅验证名称)的地方,或将其扩展到所需类的公共类CategoryServicesImpl扩展BaseServicesImpl实现CategoryServices。

How can I make this work? 我该如何进行这项工作? Is it possible? 可能吗? I'm trying to do the most I can to reuse my code. 我正在尽我所能来重用我的代码。 Let me know if there is another way. 让我知道是否还有另一种方法。

EDITED: What I'm trying to do, is to have a base class with all the common method for all the classes, let say, the basic for create, edit, delete, get, getAll, etc. And then in each class I also can have and override for those method because in some classes they will be different, but in most, they will just do the same so it will call the generic ones that were defined in Base class. 编辑:我想做的是拥有一个基类,其中包含所有类的所有通用方法,比如创建,编辑,删除,获取,getAll等的基本方法。然后在每个类中也可以具有和覆盖这些方法,因为在某些类中它们将有所不同,但是在大多数情况下,它们将执行相同的操作,因此它将调用在基类中定义的泛型类。

You can easily achieve what you want, just with a couple of changes: 只需进行一些更改,即可轻松实现所需的目标:

public interface BaseServices<T extends BaseBO> {

    boolean validate(T item, int id) throws Exception;
}

public interface CategoryServices 
    extends BaseServices<CategoryBO> {
}

public class BaseServicesImpl<T extends BaseBO> 
    implements BaseServices<T> {

    @Override
    public boolean validate(T item, int id) throws Exception {
        return true;
    }
}

public class CategoryServicesImpl 
    extends BaseServicesImpl<CategoryBO> 
    implements CategoryServices {

    @Override
    public boolean validate(CategoryBO item, int id) throws Exception {
        return true;
    }
}

The key is to let your generic T type be a descendant of BaseBO , and then adjust your BaseServicesImpl class to also be generic. 关键是让您的通用T类型成为BaseBO的后代,然后将BaseServicesImpl类也调整为通用。

Caveat see my comment on good design . 请注意 我对好的设计的评论 Simplicity is the key to good software. 简单是优质软件的关键。 Over-engineering is the road to madness. 过度设计是通往疯狂的道路。

If you have an interface and you need to share to the implementation of some methods between classes that implement that interface, you can use an Abstract Base Class to implement the methods you need to share. 如果您有一个接口,并且需要在实现该接口的类之间共享某些方法的实现,则可以使用Abstract Base Class来实现您需要共享的方法。

public interface Service<T> { 
    //note, interfaces are implictly public
    boolean validate(T item, int id) throws Exception;
    void foo();
    void bar();
}

public abstract class BaseService<T> {
    boolean validate(T item, int id) throws Exception {
        return false; // a default implementation
    }
    public abstract void foo();
    public abstract void bar();
}

public class FooService<T> extends BaseService<T>{
    public void foo() {
        println("foo"); 
    }
    public void bar() {
        println("bar"); 
    }
}

With Java 8, it's also possible to provide default implementations in an interface . 使用Java 8,还可以在interface中提供默认实现

public interface Service<T> { 
    boolean validate(T item, int id) throws Exception;
    void foo();
    void bar();
}

But beware, this is can lead to a sort of multiple inheritance which may make your code harder to reason about. 但是请注意,这可能导致某种多重继承,这可能会使您的代码难以推理。

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

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