-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandTest.java
More file actions
77 lines (59 loc) · 1.67 KB
/
CommandTest.java
File metadata and controls
77 lines (59 loc) · 1.67 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
A Behavioral Pattern.
The Command pattern is intended to encapsulate a request as an object.
For example, consider a window application that needs to make requests of
objects responsible for the user interface. The client responds to input
from the user by creating a command object and the receiver. It then passes
this object to an Invoker object, which then takes the appropriate action.
This allows the client to make requests without having to know anything about
what action will take place. In addition, you can change that action without
affecting the client.
*/
public class CommandTest {
public static void main(String[] args){
System.out.println("-------------- COMMAND ---------------");
FileMenu fMenu = new FileMenu();
FileOpener opener = new FileOpener();
Command cmd = new NewFileOpenCommand(opener);
fMenu.setCommand(cmd);
fMenu.click();
}
}
// Command
abstract class Command {
FileOpener opener;
public abstract void execute();
};
// Concrete Command
class NewFileOpenCommand extends Command {
FileOpener opener;
NewFileOpenCommand(FileOpener o){
opener = o;
}
public void execute(){
System.out.println("New File Open command is being executed... ");
opener.openFile();
}
};
// Invoker
class FileMenu {
Command command;
public void setCommand(Command cmd){
command = cmd;
}
public Command getCommand(){
return command;
}
public void click(){
System.out.println("Clicked on File menu.");
command.execute();
}
};
//Receiver
class FileOpener {
FileOpener (){
}
public void openFile(){
System.out.println("Opening a new file.");
}
};