I have a feeling that below code is great candidate for refactoring to generics but I struggle with doing that. The business background is simple. We are just after sending some data to an external system using external API. These can be CREATE_LETTER, CREATE_EVENT, CREATE_USER etc actions. We are now receiving status of the request of for example CREATE_LETTER action which can be (ACCEPT, REJECT, PROGRESS, UNKNOW, etc). We have to do something depending on the status ant type of action. Below are strategies implemented for this scenario.
So if anybody wants to progess action from anywhere he wants, he just call
ActionProcessorApi.processAction
Below is the whole code:
public class ActionProcessorImpl implements ActionProcessorApi {
private interface ProcessActionStrategy{
void process(ActionParameters parameters);
}
private ProcessActionStrategy strategyForCreateEvents = new ProcessActionStrategy() {
@Override
public void process(ActionParameters parameters) {
ActionParameters actionLogic = parameters.getProcessingLogic();
if (ActionLogicEnum.SENT.equals(actionLogic)) {
Event event parameters.getEvent();
//do something
}else if (ActionLogicEnum.REJECTED.equals(actionLogic)) {
//do something
} else if (ActionLogicEnum.UNKNOW.equals(actionLogic)) {
//do something
}
}
};
private ProcessActionStrategy strategyForSendLetters = new ProcessActionStrategy() {
@Override
public void process(ActionParameters parameters) {
ActionLogicEnum actionLogic = parameters.getProcessingLogic();
if (ActionLogicEnum.SENT.equals(actionLogic)) {
//do something
}else if (ActionLogicEnum.REJECTED.equals(actionLogic)) {
//do something
} else if (ActionLogicEnum.UNKNOW.equals(actionLogic)) {
//do something
}
}
};
private ProcessActionStrategy strategyForCreateUsers = new ProcessActionStrategy() {
@Override
public void process(ActionParameters parameters) {
ActionLogicEnum actionLogic = parameters.getProcessingLogic();
if (ActionLogicEnum.SENT.equals(actionLogic)) {
//do something
}else if (ActionLogicEnum.REJECTED.equals(actionLogic)) {
//do something
} else if (ActionLogicEnum.UNKNOW.equals(actionLogic)) {
//do something
}
}
};
public enum ActionType{
CREATE_USERS, SEND_LETTERS, CREATE_EVENTS
}
private ProcessActionStrategy chooseProperStrategy(ActionParameters parameters) {
//This could be a Map<ActionType, ProcessActionStrategy>
if (ActionType.CREATE_EVENTS.equals(parameters.getActionType())){
return strategyForCreateEvents;
}
if (ActionType.SEND_LETTERS.equals(parameters.getActionType())){
return strategyForSendLetters;
}
if (ActionType.CREATE_USERS.equals(parameters.getActionType())){
return strategyForCreateUsers;
}
throw new IllegalArgumentException("Cant find strategy");
}
public void processAction(ActionParameters parameters) {
ProcessActionStrategy processActionStrategy = chooseProperStrategy(parameters);
processActionStrategy.process(parameters);
}
public class ActionParameters{
private User user;
private Event event;
private Letter letter;
private ActionType actionType;
public ActionParameters(User user, Event event, Letter letter, ActionType actionType) {
this.user = user;
this.event = event;
this.letter = letter;
this.actionType = actionType;
}
public User getUser() {
return user;
}
public Event getEvent() {
return event;
}
public Letter getLetter() {
return letter;
}
public ActionType getActionType() {
return actionType;
}
}
}
Aucun commentaire:
Enregistrer un commentaire