class="java" name="code"> //状态机元数据描述 @StateMachine protected static interface CustomerLifecycleMeta { @StateSet static interface States { @Initial @Function(transition = CustomerLifecycleMeta.Transitions.Activate.class, value = { Active.class }) static interface Draft {} @Functions({ @Function(transition = CustomerLifecycleMeta.Transitions.Suspend.class, value = Suspended.class), @Function(transition = CustomerLifecycleMeta.Transitions.Cancel.class, value = Canceled.class) }) static interface Active {} @Function(transition = CustomerLifecycleMeta.Transitions.Resume.class, value = Active.class) static interface Suspended {} @End static interface Canceled {} } @TransitionSet static interface Transitions { static interface Activate {} static interface Suspend {} static interface Resume {} static interface Cancel {} } }
?
public abstract static class ReactiveObject { @StateIndicator private String state = null; protected void initialState(String stateName) { if ( null == state ) { this.state = stateName; } else { throw new IllegalStateException("Cannot call initialState method after state had been intialized."); } } public String getState() { return state; } }
?
? ?// 标记生命周期元数据引用的业务对象(反应型对象)
@LifecycleMeta(CustomerLifecycleMeta.class) public static class Customer extends ReactiveObject { protected Customer() { initialState(Draft.class.getSimpleName()); } @Transition public void activate() {} @Transition public void suspend() {} @Transition public void resume() {} @Transition public void cancel() {} }
?
? ?// 测试用例
@Test public void test_standalone_object_without_relation_lifecycle() throws VerificationException { Customer customer = new Customer(); customer.activate(); assertEquals(CustomerLifecycleMeta.States.Active.class.getSimpleName(), customer.getState()); customer.suspend(); assertEquals(CustomerLifecycleMeta.States.Suspended.class.getSimpleName(), customer.getState()); customer.resume(); assertEquals(CustomerLifecycleMeta.States.Active.class.getSimpleName(), customer.getState()); customer.cancel(); assertEquals(CustomerLifecycleMeta.States.Canceled.class.getSimpleName(), customer.getState()); }
?