如何使用DI将转换代码转换为OO代码

时间:2013-02-27 15:53:24

标签: java cdi

我有一个使用CDI的项目。具有状态列表的容器管理服务类。

@ApplicationScoped
public class StateService {

    @Inject
    MailService mailService;

    //Inject some other stuff

    private Map<String, State> state = new HashMap<String, State>();

    public void makeStateTransition(StateChange stateChange) {
        State currentState = state.get(stateChange.getKey());
        switch (currentState.stateType) {
            case STATE1:
                handleState1Transition(stateChange);
            case STATE2:
                handleState2Transition(stateChange);
            case STATE3:
                handleState3Transition(stateChange);
        }
    }

    private void handleState1Transition(StateChange stateChange) {
        //Do some stuff to handle state and use injected mailsender to send some email
        mailService.sendEmail("Send some email");
        //if some criteria is right create a new state and set it in the map
        State newState = new State();
        //Set some variable
        state.put(stateChange.getKey(), newState);
    }

    private void handleState2Transition(StateChange stateChange) {
        //Do stuff similar to above, maybe use other injected fields
    }

    private void handleState3Transition(StateChange stateChange) {
        //Do stuff similar to above
    }
}

public class State {

    public enum StateType {
        STATE1, STATE2, STATE3
    }

    public StateType stateType;

    //Bunch of other properties


}

事实证明这是非常可行的。我重构它以试图让它更加OO。

@ApplicationScoped
public class StateService {

    @Inject
    MailService mailSerice;

    //Inject some other stuff

    private Map<String, State> state = new HashMap<String, State>();

    public void makeStateTransition(StateChange stateChange) {
        State currentState = state.get(stateChange.getKey());
        State newState = currentState.handleStateChange(stateChange, mailService);
        state.put(stateChange.getKey(), newState);
    }
}

现在StateService不需要知道所有状态转换

public abstract class State {
    //Some protected fields shared by all states

    public abstract State handleStateChange(StateChange stateChange, MailService mailService);
}

public class SomeState extends State {

    //Some properties specific to this state

    public State handleStateChange(StateChange stateChange, MailService mailSerice) {
        //Do some stuff to handle state and use injected mailsender to send some email
        mailSerice.sendEmail("Send some email");
        return new SomeOtherState();
    }
}

这会带来更好的代码,特别是随着可能状态数量的增加。 问题是让注入的mailsender进入需要它的正确状态。有些州可能不需要它,而其他州可能需要别的东西。因为状态不是由容器创建的,所以它们不能自己使用注入。 在状态的构造函数中传递注入的字段是可能的,但是注入的对象可能在非托管状态内“陈旧”?

我在方法调用中使用它作为参数发送,这样它应该总是“新鲜”。如果许多注射需要作为参数发送并且在某种程度上感觉不对,那么非常麻烦。

在这个例子中,您如何建议如何处理状态对象所需的注入资源?

请忽略课堂上的任何线程问题。

0 个答案:

没有答案
相关问题