2015-08-14 71 views
1

我有枚舉類如何使用設計模式來代替開關盒

public enum TaskName { 
    LOGIN,REGISTER,MESSAGE 
} 

我用這個枚舉與開關的情況下

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控制器= new TaskController()

這樣對象只創建一次

現在我打算使用命令模式來代替開關情況

但我堅持瞭如何使用我的UserDaoJpaImpl,FriendDaoJpaImpl,GroupDaoJpaImpl,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(); 

} 

我想移動整個日誌ic從TaskController.doIt()轉換爲其他將執行命令接口的命令類。

我正在使用Websocket。我不想用彈簧這個問題

在此先感謝

+0

命令模式很好。但現在更像是你需要一個工廠模式來創建實例。是什麼讓你覺得你需要命令模式?這是要求還是你在這方面發現了挑戰?但是所有的Impl類都應該實現這個接口,並根據它來調用create方法。 –

+0

對不起,我已經粘貼了舊代碼,我已經實現了界面。這不是要求some1建議我。 – abhishek221192

+0

你的控制器可能做得太多了(至少有一種方法)。忘記命令模式,否則你將會遇到另一個問題。 – atamanroman

回答

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

保持這個問題有點模糊和主觀讓我們自由想象:)。這是我對結構的看法。

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,當然他們預計會改變。

0

與@epicPandaForce相同的想法,但在java 8中,它可以有點不同。通過傳遞一個lambda到構造函數。

public enum TaskName { 
    LOGIN((taskController) -> { 
      taskController.getUserDao().create(/*something*/); 
      //... 
    }), 
    REGISTER((taskController) -> { 
      //Implementation 
    }), 
    MESSAGE((taskController) -> { 
      //... 
    }); 

    private final Consumer<TaskController> doIt; 

    private TaskName(Consumer<TaskController> doIt) { 
     this.doIt = doIt; 
    } 

    public final void service(...) { 
     doIt(...); 
    } 
1
public interface Command { 
public void Execute(); 
} 

public class Invoker { 
    private Map<TaskName,Command> commandMap = new EnumMap<TaskName,Command>(TaskName.class); 

    public void AddCommand(TaskName task,Command command){ 
     commandMap.put(task, command); 
    } 
    public void HandleCommand(TaskName task){ 
     Command command=commandMap.get(task); 
     command.Execute(); 
    } 
} 


public class ConcreteMessageCommand implements Command { 
    private MessageDao messageDaoJpaImpl; 
    public ConcreteMessageCommand(MessageDao md){ 
     messageDaoJpaImpl=md; 
    } 
    @Override 
    public void Execute() { 
     // TODO Auto-generated method stub 
     messageDaoJpaImpl.create(); 
    } 

} 



public class ConcreteUserCommand implements Command { 
    private UserDao userDaoJpaImpl; 

    public ConcreteUserCommand(UserDao ud){ 
     userDaoJpaImpl=ud; 
    } 

    @Override 
    public void Execute() { 
     userDaoJpaImpl.create(); 
    } 

} 



public class TaskController { 

    Invoker invoker; 
    public TaskController() { 
     EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("Eclipselink_JPA"); 
     invoker=new Invoker(); 
     ConcreteUserCommand cuc=new ConcreteUserCommand(new UserDaoJpaImpl(emfactory)); 
     invoker.AddCommand(TaskName.LOGIN, cuc); 

     ConcreteMessageCommand cmc=new ConcreteMessageCommand(new MessageDaoJpaImpl(emfactory)); 
     invoker.AddCommand(TaskName.MESSAGE, cmc); 


    } 

    public void doIt(TaskName taskName) 
    { 
     invoker.HandleCommand(taskName); 
    } 
} 

public class Main { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     TaskController task=new TaskController(); 
     task.doIt(TaskName.LOGIN); 
    } 

} 

可以使用命令模式,以消除開關情況下的這樣的,GroupDaoJpaImpl- MessageDaoJpaImpl可以被認爲是接收機objects.You可以改變其中被創建的對象,以優化代碼。