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

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

本文介绍了如何使用设计模式代替开关盒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有枚举类

public enum TaskName {
     LOGIN,REGISTER,MESSAGE
}

我将此枚举与切换用例一起使用

I use this enum with 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()

I create object of TaskController as static TaskController controller=new TaskController()

因此该对象仅创建一次

现在我打算使用命令模式代替开关盒

Now I am planning to use Command Pattern in place of switch-case

但是我被困在如何使用我的 UserDaoJpaImpl,FriendDaoJpaImpl,GroupDaoJpaImpl ,Command类中的MessageDaoJpaImpl

but i am stuck how to use my UserDaoJpaImpl,FriendDaoJpaImpl,GroupDaoJpaImpl,MessageDaoJpaImpl inside Command classess

编辑

命令类

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()中移出

I want to move whole logic from TaskController.doIt() to Other command class which will implement Command Interface.

我正在使用Websocket。我不想用spring解决这个问题

I am using Websocket. I don't want to use spring for this problem

预先感谢

推荐答案

您正在,正在寻找我的解决方案。

You're looking for my solution to this problem 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 类的任何内容。

Unless I totally misunderstood your question because you never really showed anything about your Command classes.

您还应该考虑针对此特定问题使用框架,例如 Spring框架

You should also consider using a framework for this particular problem, like the Spring Framework.

这篇关于如何使用设计模式代替开关盒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:40