English 中文(简体)
如何在静态背景下使用带有特定物体的通用类?
原标题:How to use generic class with specific objects in a static context?

我会尽力解释

我使用 Play Forum 2, 我会做很多 CRUD 动作。 其中一些动作将会是身份识别的, 所以我喜欢 KISS 和 DRY, 所以在一开始我想到一个抽象的分类, 包含 < code> list 、 < code > details 、 < code > create 、 < code> update 和 < code> < deletete 方法, 带有通用对象, 并且通过指定要使用的对象来扩展这个类别( model & amp; 表) :

public abstract class CrudController extends Controller {
    protected static Model.Finder<Long, Model> finder = null;
    protected static Form<Model> form = null;

    public static Result list() {
        // some code here
    }

    public static Result details(Long id) {
        // some code here
    }

    public static Result create() {
        // some code here
    }

    public static Result update(Long id) {
        // some code here
    }

    public static Result delete(Long id) {
        // some code here
    }
}

以及使用CROUD的阶级:

public class Cities extends CrudController {
    protected static Model.Finder<Long, City> finder = City.find;
    protected static Form<City> form = form(City.class);

    // I can override a method in order to change it s behavior :
    public static Result list() {
        // some different code here, like adding some where condition
    }
}

如果我不是在一成不变的情况下这样做,将会有效。

但既然如此,我该怎么做?

最佳回答

要做到这一点,可以使用代表团:界定一个包含CROUD行动逻辑的普通爪哇类:

public class Crud<T extends Model> {

    private final Model.Finder<Long, T> find;
    private final Form<T> form;

    public Crud(Model.Finder<Long, T> find, Form<T> form) {
        this.find = find;
        this.form = form;
    }

    public Result list() {
        return ok(Json.toJson(find.all()));
    }

    public Result create() {
        Form<T> createForm = form.bindFromRequest();
        if (createForm.hasErrors()) {
            return badRequest();
        } else {
            createForm.get().save();
            return ok();
        }
    }

    public Result read(Long id) {
        T t = find.byId(id);
        if (t == null) {
            return notFound();
        }
        return ok(Json.toJson(t));
    }

    // … same for update and delete
}

然后您可以定义一个 Play 控制器, 其静态字段包含 Crud<City> 实例 :

public class Cities extends Controller {
    public final static Crud<City> crud = new Crud<City>(City.find, form(City.class));
}

你几乎完成了:你只需要确定Crud行动的路线:

GET     /                     controllers.Cities.crud.list()
POST    /                     controllers.Cities.crud.create()
GET     /:id                  controllers.Cities.crud.read(id: Long)

注意: 此示例生成了 JSON 对 Brevety 的回复, 但可以生成 HTML 模板 。 但是, 由于 Play 2 模板是静态键入的, 您需要将所有模板都作为 < code> Crude 类的参数通过 。

问题回答

(否认:我对游戏框架没有经验。 )

下列想法可能有助于:

public interface IOpImplementation {
    public static Result list();
    public static Result details(Long id);
    public static Result create();
    public static Result update(Long id);
    public static Result delete(Long id);
}

public abstract class CrudController extends Controller {
    protected static Model.Finder<Long, Model> finder = null;
    protected static Form<Model> form = null;

    protected static IOpImplementation impl;

    public static Result list() {
        return impl.list();
    }

    public static Result details(Long id) {
        return impl.details(id);
    }
    // other operations defined the same way
}

public class Cities extends CrudController {

    public static Cities() {
        impl = new CitiesImpl();
    }

}

这样你就可以形成一个层次的 执行。

(这一定是一些花哨设计图案, 但我不知道ATM的名字。 )





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签