본문 바로가기

Software Engineering

[디자인패턴] 커맨드 패턴 Order.java Command.java Commander.java Receiver.java Carrier.java

1
2
3
4
5
package command;
 
public interface Order {
    public void execute();
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package command;
 
import javax.servlet.http.HttpServletRequest;
 
import lombok.Data;
@Data
public class Command implements Order{
    protected HttpServletRequest request;
    protected String action,domain,page,view;
 
    @Override
    public void execute() {
        this.view = "/WEB-INF/view/"+domain+"/"+page+".jsp";
    }
}
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package command;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import enums.Action;
 
public class Commander {
    public static Command order(
            HttpServletRequest request,
            HttpServletResponse response){
        Command cmd = null;
        switch (Action.valueOf(
                request.getParameter("action")
                .toUpperCase())) {
        case MOVE:
            System.out.println("---무브진입---");
            cmd = new MoveCommand(request);
            break;
        case JOIN:
            System.out.println("---조인진입---");
            cmd = new CreateCommand(request);
            break;
        case LIST:
            cmd = new ListCommand(request);
            break;
        case SEARCH:
            cmd = new SearchCommand(request);
            break;
        case RETRIEVE:
            cmd = new RetrieveCommand(request);
            break;
        case COUNT:
            cmd = new CountCommand(request);
            break;
        case UPDATE:
            cmd = new UpdateCommand(request);
            break;
        case DELETE:
            System.out.println("=== DELETE CMD 1===");
            cmd = new DeleteCommand(request);
            System.out.println("=== DELETE CMD 2===");
            break;
        case LOGIN:
            cmd = new LoginCommand(request);
            break;
        default:
            break;
        }
        return cmd;
    }
}
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
package command;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class Receiver {
    public static Command cmd=new Command();
    public static void init(
            HttpServletRequest request,
            HttpServletResponse response){
        cmd = Commander.order(request,response);
    }
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package command;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class Carrier {
    public static void forward(HttpServletRequest request, 
            HttpServletResponse response) {
        try {
            System.out.println("Sentry.cmd.getView() is "
                    +Receiver.cmd.getView());
            request
            .getRequestDispatcher(Receiver.cmd.getView())
            .forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void redirect(
            HttpServletRequest request, 
            HttpServletResponse response,String url){
        try {
            response.sendRedirect(request.getContextPath()+url);
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
}
cs