【问题标题】:Active objects @Implementation annotation recursion活动对象@Implementation注解递归
【发布时间】:2013-09-19 07:48:54
【问题描述】:

我在使用活动对象库 (https://java.net/projects/activeobjects/pages/Home) 时遇到问题。

假设我有一个像这样的实体:

@Implementation(PersonImpl.class)
interface Person extends Entity{

    public String getName();

    public String setName();
}

以及这个实体的实现类:

class PersonImpl {

    private Person person;

    public PersonImpl(Person person){
        this.person = person;
    }

    public String getName(){
       if( isTodayIsMonday() )
           return "I hate monday";
       else
           return person.getName();
    }
}

问题出在PersonImpl 类中。因为person.getName() 我得到了无限递归(impl 类总是被调用)。如何跳过调用实现(在PersonImpl 类中)并从数据库中获取名称?

【问题讨论】:

    标签: java active-objects


    【解决方案1】:

    根据http://www.javalobby.org/articles/activeobjects/,ActiveObjects 通过检查调用堆栈自动避免了这个问题:

    "我们可以用它来检查栈上一步定义的实现,如果我们发现它发起了方法调用,我们将跳过定义的实现的重新调用,并正常执行方法调用. 因此,从其定义的实现对实体的任何调用都将跳过任何实现逻辑,从而避免递归。”

    【讨论】:

      【解决方案2】:
      private String getMyName(boolean isMonday) {
        if (isMonday) {
          return "I hate Monday";
        }
        return person.getName();
      }
      

      【讨论】:

      • 这不好。首先,我将在表示层中使用这个方法(它没有任何用于检查 isMonday 的数据)。其次,我不想实现另一种获取名称的方法。但是感谢您的快速响应:)
      猜你喜欢
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多