生命周期组件框架:生命周期描述语言——简单状态机示例代码_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 生命周期组件框架:生命周期描述语言——简单状态机示例代码

生命周期组件框架:生命周期描述语言——简单状态机示例代码

 2013/11/24 12:29:53  barryzhong  程序员俱乐部  我要评论(0)
  • 摘要://状态机元数据描述@StateMachineprotectedstaticinterfaceCustomerLifecycleMeta{@StateSetstaticinterfaceStates{@Initial@Function(transition=CustomerLifecycleMeta.Transitions.Activate.class,value={Active.class})staticinterfaceDraft{}@Functions({@Function
  • 标签:代码
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());
    }

?

发表评论
用户名: 匿名