如何使用设计模式代替开关盒

时间:2015-08-14 06:50:31

标签: java design-patterns enums switch-statement

我有枚举类

public enum TaskName {
     LOGIN,REGISTER,MESSAGE
}

我将此枚举用于switch-case

public class TaskController {


    private UserDao userDaoJpaImpl;
    private FriendDao friendDaoJpaImpl;
    private GroupDao groupDaoJpaImpl;
    private MessageDao messageDaoJpaImpl;

    public TaskController() {
        EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("Eclipselink_JPA");
        userDaoJpaImpl = new UserDaoJpaImpl(emfactory);
        friendDaoJpaImpl = new FriendDaoJpaImpl(emfactory);
        groupDaoJpaImpl = new GroupDaoJpaImpl(emfactory);
        messageDaoJpaImpl = new MessageDaoJpaImpl(emfactory);
    }

    public void doIt(String taskName)
    {
      switch (taskName) {
        case LOGIN:
            userDaoJpaImpl.create(/*Something*/);
            //Implementation
        break;
        case REGISTER:
            //Implementation
        break;
        case MESSAGE:
             messageDaoJpaImpl.create(/*Something*/);
            //Implementation
        break;
        }
    }
}

我将TaskController的对象创建为  静态TaskController控制器=新的TaskController()

使对象只创建一次

现在我打算用Command Pattern代替switch-case

但我被困在如何使用我的 UserDaoJpaImpl,FriendDaoJpaImpl,GroupDaoJpaImpl,Message classess中的MessageDaoJpaImpl

修改

命令类

public interface Command {

    void setSession(Session session); //Session is websocket connection session not HttpSession

    void setConnectedUser(Map<String, UserDTO> connectedUser);

    void setData(String data);        


    JSONObject execute();

}

我想将整个逻辑从TaskController.doIt()移动到其他将实现命令接口的命令类。

我正在使用Websocket。我不想用春天来解决这个问题

提前致谢

3 个答案:

答案 0 :(得分:3)

保持这个问题有点模糊和主观让我们想象自由:)。这是我对结构的看法。

public interface Command {
    public void doIt(EntityManagerFactory emFactory);
}

class LoginCommand implements Command {
    @Override public void doIt(EntityManagerFactory emFactory) {
        UserDaoJpaImpl userDaoJpaImpl = new UserDaoJpaImpl(emFactory);
        // Do something
    }
}

class RegisterCommand implements Command {
    @Override public void doIt(EntityManagerFactory emFactory) {
    }
}

class MessageCommand implements Command {
    @Override public void doIt(EntityManagerFactory emFactory) {
    }
}

enum TaskName {
    LOGIN(new LoginCommand()), REGISTER(new RegisterCommand()), MESSAGE(new MessageCommand());

    private Command command;

    TaskName(Command command) {
        this.command = command;
    }

    public void doIt(EntityManagerFactory emFactory) {
        command.doIt(emFactory);
    }
}

public class TaskController {
    private EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("Eclipselink_JPA");

    public void doIt(String taskName) {
        TaskName task = TaskName.valueOf(taskName);
        task.doIt(emFactory);
    }
}

所有方法都称为doIt,当然它们会发生变化。

答案 1 :(得分:2)

您正在寻找我对此问题的解决方案here

基本上,

public enum TaskName {
     LOGIN {
         @Override
         public void doIt(TaskController taskController) {
              taskController.getUserDao().create(/*something*/);
              //...
         }
     },
     REGISTER {
         @Override
         public void doIt(TaskController taskController) {
              //Implementation
         }
     },
     MESSAGE {
         @Override
         public void doIt(TaskController taskController) {
              taskController.getMessageDao().create(/*something*/);
              //...
         }
     };

     private TaskName() {
     }

     public abstract void doIt(TaskController taskController);

     public static TaskName getByTaskName(String taskName) {
         for(TaskName taskEnum : TaskName.values()) {
             if(taskEnum.name().equalsIgnoreCase(taskName)) {
                 return taskEnum;
             }
         }
         throw new IllegalArgumentException("The Task Name [" + taskName + "] is not a valid task name!");
     }
}

public class TaskController {


    private UserDao userDaoJpaImpl;
    private FriendDao friendDaoJpaImpl;
    private GroupDao groupDaoJpaImpl;
    private MessageDao messageDaoJpaImpl;

    public TaskController() {
        EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("Eclipselink_JPA");
        userDaoJpaImpl = new UserDaoJpaImpl(emfactory);
        friendDaoJpaImpl = new FriendDaoJpaImpl(emfactory);
        groupDaoJpaImpl = new GroupDaoJpaImpl(emfactory);
        messageDaoJpaImpl = new MessageDaoJpaImpl(emfactory);
    }

    public void doIt(String taskName) {
        TaskName.getByTaskName(taskName).doIt(this);
    }
}

除非我完全误解了你的问题,因为你从未真正展示过关于Command课程的任何内容。

您还应考虑为此特定问题使用框架,例如 Spring Framework

答案 2 :(得分:0)

与@epicPandaForce相同的想法,但在java 8中它的处理方式有点不同。通过将lambda传递给构造函数。

DataGridView