本文介绍了国家模式:国家作为休眠单身实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

我已经实现了一个状态模式。现在我的应用程序中有十几个国家,所有的都是Spring的bean,如:

What I have:
I've implemnted a `State Pattern. Now I have a dozen of States in my app, all of them are Spring's beans, like:

@Service(value = "FinishedState")
@Scope("singleton")
public class FinishedState extends AbstractState {
  private final String stateName = "Finished";
  private final String badgeColor = "#459852";

  //implementation of state API
}

我想要的:

可以更改 stateName badgeColor 而无需重新编译我的应用程序。或者换句话说,我想将我的州存储在数据库中。

What I want:
Be able to change stateName and badgeColor without recompiling my application. Or in other words I want to store my states in DB.

有什么问题:

What a question:


  1. 将所有国家列为Singleton实体是个好主意?

  2. >我可能遇到什么困难?

  1. Is it good idea to make all States as Singleton entities?
  2. What diffculties may I face?

意味着我将手动设置每个州的ID,如 id = this.getClass()。getSimpleName(),并将所有状态兄弟对象映射到一个表中。

Means that I will manually set an id to each State , like id = this.getClass().getSimpleName(), and map all state-siblings into one table.

编辑(对于@Adam Arold):

Edit (for @Adam Arold):

而且它的价值说我需要其他实体可以引用Singleton的状态

And also its worth to say that I need that other entities can reference that Singleton's states.

@Service(value = "FinishedState ")
@Scope("singleton")
public class FinishedState extends AbstractState {
  private final String stateName = "Finished";
  private final String badgeColor = "#459852";

  @Override
  public void someMethod1(Session session, String newState, MyEntity entity) {
       entity.setStatus(newState);
       writeHistory(entity);
  }

  @Override
  protected boolean someMethod2(Session session, AbstractState newState, MyEntity entity)
  {
      return true;
  }
}


推荐答案

结束于:

@MappedSuperclass
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@Table(name = "state")
public class StateBase {

    /**
     * Entity API
     */
    @Id
    @Column(name = "id")
    private String id = this.getClass().getSimpleName(); //prevent from creating two instances of state

    @Column(name = "name")
    private String name;

    @Column(name = "badgeColor")
    private String badgeColor;

    //only Getters! also hashCode and Equals
    //.....................

    /**
     * ======== State API ========
     * Use TEMPLATE METHOD and hooks
     */
    final public void changeState(Session session, State newState, StateEntity entity) { //StateEntity is interface that is implemented by all entities that have a state
        if(newState.equals(this)) return;

        if (canMove(session, newState, entity)) {
            hook(session, newState, entity);
            beforeTransition(session, newState, entity);
            makeTransition(session, newState, entity);
            afterTransition(session, newState, entity);
        } else {
            throw new TransitionDeniedException("Transition from " + getName() + " to " + newState.getName() + " is denied.");
        }
    }

    //Defauls implementation for all (or almost all hooks)
    protected void beforeTransition(Session session, State newState, StateEntity entity) {
        entity.setState(newState);
    }

    protected void afterTransition(Session session, State newState, StateEntity entity) {
        writeHistory(entity);
       session.merge(entity);
    }
    //..........................
}

而不是:

@Entity
public class SpecificState extends StateBase {

    //Override hook
    @Override
    public void makeTransition(Session session, State newState, StateEntity entity) {
        MyEntity myEntity = (MyEntity ) entity;
        String commentText = "some comment";
        String author = "[autogenerated]";
        addProfileComment(session, myEntity, commentText, author);
    }
}

由于我的实施,所有的州都是迷信,但是他们只有没有Setters的私有String字段。所以他们没有可变态。因此,如果存在一个或多个特定状态的实体(在内存中),这不是真实的事情。在DB中,我可以一次只存储一个特定状态类的实体。

Due to my implementation, all states are entites, but they have only private String field without Setters. So they have no muttable state. So it isn't realy matter if there can exist one or more entities of specific state (in memory). In DB I can store only one entity of specific state class at once.

优点


  1. 让我忘记关于状态同步的麻烦以及与使用Hibernate实体替换Spring单例bean相关的另一个可能的困难。

  2. 它还允许用户更改名称 badgeColor 通过管理工具,而无需重新编译整个应用程序。

  1. It's allow me to forget about troubles of state synchronization and another possible difficulties linked with replacement of Spring singleton beans with Hibernate entities.
  2. It's also allows user to change name and badgeColor through admin tool without recompiling the whole application.

>缺点:

Disadvantages:

未遵守。

这篇关于国家模式:国家作为休眠单身实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:05