我正在开发一个泽西岛的应用程序,我有很多资源.虽然这些资源的主要功能各不相同,但它们共享许多常用方法(如列表,读取,更新等).该应用程序在Google App Engine上运行,并使用Guice进行依赖注入.
我的第一种方法是拥有一个包含所有通用逻辑的通用AbstactResource,它分别由添加所需自定义方法的所有其他资源进行扩展.
public class AbstractResource<T> {
@GET
public ListPage<T> list(@QueryParam("limit") Integer limit,
@QueryParam("start") Integer start) {
// ... implementation
}
@GET
@Path("/{id}")
public T get(@PathParam("id") Long id) {
// ... implementation
}
Run Code Online (Sandbox Code Playgroud)
示例资源看起来像:
public class TenantResource extends AbstractResource<Tenant> {
// custom resource related methods here
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下一切正常.当我添加一个更多抽象级别时会出现问题.假设我只想为某些资源存储历史记录和更改日志.我创建了另一个扩展AbstractResource的抽象类,名为AudiatableResource,它添加了所需的功能.
public abstract class AuditableResource<T extends AuditableModel>
extends AbstractResource {
// here I override update and create methods to save changelogs
}
Run Code Online (Sandbox Code Playgroud)
如您所见,此案例中的type参数已更改(现在它扩展了AuditableModel).
新的具体资源将如下所示:
public class PropertyResource extends AuditableResource<Tenant> {
// custom …Run Code Online (Sandbox Code Playgroud)