Java匿名类唯一标识

标签 java oop design-patterns reflection

假设我想使用使用匿名类的命令模式。在调用程序中我想应用缓存。像这样的事情:

// command abstract class
public static abstract class ViewScopeCacheCommand<T> { 
    public abstract T invoke();
}

// innvoker
public static class ViewScopeCache<T> {
    // cache
    private Map<String,T> pageCache = null;

    public ViewScopeCache() {
        pageCache = new HashMap<String,T>();
    }
    // get from cache
    public T get(String key) {
        return pageCache.get(key);
    }
    // put to cache
    public void put(String key, T t) {
        pageCache.put(key, t);
    }
    // I want to say how to load the result of not in cache - given by command
    public T getLazy(String key, ViewScopeCacheCommand<T> command) {
        if(get(key) == null) {

            T t = command.invoke();
            pageCache.put(key, t);
        }
        return get(key);
    }
    public void invalidate() {
        pageCache.clear();
    }
    // I want to identify the cache by command
    /*
    public T getLazy(ViewScopeCacheCommand<T> command) {
        String magicKey = .. derive from command
        if(get(key) == null) {

            T t = command.invoke();
            pageCache.put(key, t);
        }
        return get(key);
    }
    */ 
}

假设我有 2 个地方使用这个:

..
Result getResult1() {
    viewScopeCache.get("key1", new ViewScopeCacheCommand<Result>() { 
       ..invoke(){ call DB SQLX }}); // does not need to be SQL, can be a service A call
}
..
Result getResult2() {
    viewScopeCache.get("key2", new ViewScopeCacheCommand<Result>() { 
       ..invoke(){ call DB SQLY}}); // does not need to be SQL, can be a service B call
}
..

好的,但是我可以识别匿名 ViewScopeCacheCommand 吗?这样用法就会简化为这样

..
Result getResult1() {
    viewScopeCache.get(new ViewScopeCacheCommand<Result>() { 
       ..invoke(){ call DB SQLX }}); // does not need to be SQL, can be a service A call
}
..
Result getResult2() {
    viewScopeCache.get(new ViewScopeCacheCommand<Result>() { 
       ..invoke(){ call DB SQLY}}); // does not need to be SQL, can be a service B call
}
..

如果我完成了普通的类扩展,我可以使用instanceof、反射等。但是是否有某种机制可以识别调用 SQLX 的命令 1 和调用 SQLY 的命令 2 之间的差异 它可以是基于反射的,也可以是代码行..

我脑子里唯一的想法是注释:(..但这很蹩脚..

一些更好的想法/建议?

问题是:我可以推断出在类级别上可能是唯一的 key 来识别特定的命令..

我可以向 Command { abstract String getUniqueKey() {} } 添加第二个方法并强制定义它..但这很丑陋:)

最佳答案

抽象类可以具有匿名类可以调用的构造函数:

public static abstract class ViewScopeCacheCommand<T> {
    private final String uniqueKey;

    public ViewScopeCacheCommand(String uniqueKey) {
        this.uniqueKey= uniqueKey;
    }

    public String getUniqueKey() {
        return uniqueKey;
    }

    public abstract T invoke();
}

用法是这样的:

..
Result getResult1() {
    viewScopeCache.get(new ViewScopeCacheCommand<Result>("key1") { 
        ..invoke(){ call DB SQLX }}); // does not need to be SQL, can be a service A call
}
..
Result getResult2() {
    viewScopeCache.get(new ViewScopeCacheCommand<Result>("key2") { 
        ..invoke(){ call DB SQLY}}); // does not need to be SQL, can be a service B call
}
..

关于Java匿名类唯一标识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26277986/

相关文章:

java - 是否可以用 Java 编写宏?

c++ - 用 C++ 实现一副纸牌

java - 什么是 Java EE 负载提取器模式?

Java - 如何利用动态 jar 加载?

java - 如何使用 Log4j 将日志消息计数发送到 Graphite?

java - 比较文件中的值在 java 中不起作用

javascript - 在 JavaScript 中创建函数原型(prototype)?

javascript - 如何使用 JavaScript 对象文字进行继承?

php - 使用 CodeIgniters Active Record 库来操作 MySQL 数据库是个好主意还是我应该只使用 SQL?

c# - 在方法中访问属性时使用私有(private)或公共(public)属性成员?