Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.Command Pattern là một trong những Pattern thuộc nhóm hành vi (Behavior Pattern). Nó cho phép chuyển yêu cầu thành đối tượng độc lập, có thể được sử dụng để tham số hóa các đối tượng với các yêu cầu khác nhau như log, queue (undo/redo), transtraction. Nói cho dễ hiểu, Command Pattern cho phép tất cả những Request gửi đến object được lưu trữ trong chính object đó dưới dạng một object Command. Khái niệm Command Object giống như một class trung gian được tạo ra để lưu trữ các câu lệnh và trạng thái của object tại một thời điểm nào đó. Command dịch ra nghĩa là ra lệnh. Commander nghĩa là chỉ huy, người này không làm mà chỉ ra lệnh cho người khác làm. Như vậy, phải có người nhận lệnh và thi hành lệnh. Người ra lệnh cần cung cấp một class đóng gói những mệnh lệnh. Người nhận mệnh lệnh cần phân biệt những interface nào để thực hiện đúng mệnh lệnh. Command Pattern còn được biết đến như là Action hoặc Transaction.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
package com.gpcoder.patterns.behavioral.command.bank; public class Account { private String name; public Account(String name) { this .name = name; } public void open() { System.out.println( "Account [" + name + "] Opened\n" ); } public void close() { System.out.println( "Account [" + name + "] Closed\n" ); } } |
1
2
3
4
5
6 |
package com.gpcoder.patterns.behavioral.command.bank; public interface Command { void execute(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
package com.gpcoder.patterns.behavioral.command.bank; public class OpenAccount implements Command { private Account account; public OpenAccount(Account account) { this .account = account; } @Override public void execute() { account.open(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
package com.gpcoder.patterns.behavioral.command.bank; public class CloseAccount implements Command { private Account account; public CloseAccount(Account account) { this .account = account; } @Override public void execute() { account.close(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
package com.gpcoder.patterns.behavioral.command.bank; public class BankApp { private Command openAccount; private Command closeAccount; public BankApp(Command openAccount, Command closeAccount) { this .openAccount = openAccount; this .closeAccount = closeAccount; } public void clickOpenAccount() { System.out.println( "User click open an account" ); openAccount.execute(); } public void clickCloseAccount() { System.out.println( "User click close an account" ); closeAccount.execute(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
package com.gpcoder.patterns.behavioral.command.bank; public class Client { public static void main(String[] args) { Account account = new Account( "aaa" ); Command open = new OpenAccount(account); Command close = new CloseAccount(account); BankApp bankApp = new BankApp(open, close); bankApp.clickOpenAccount(); bankApp.clickCloseAccount(); } } |
1
2
3
4
5 |
User click open an account Account [aaa] Opened User click close an account Account [aaa] Closed |
Stack (ngăn xếp) là một cấu trúc dữ liệu trừu tượng hoạt động theo nguyên lý “vào sau ra trước” (Last In First Out (LIFO).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
package com.gpcoder.patterns.behavioral.command.document; import java.util.Stack; public class Document { private Stack<String> lines = new Stack<>(); public void write(String text) { lines.push(text); } public void eraseLast() { if (!lines.isEmpty()) { lines.pop(); } } public void readDocument() { for (String line : lines) { System.out.println(line); } } } |
1
2
3
4
5
6
7 |
package com.gpcoder.patterns.behavioral.command.document; public interface Command { void undo(); void redo(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
package com.gpcoder.patterns.behavioral.command.document; public class DocumentEditorCommand implements Command { private Document document; private String text; public DocumentEditorCommand(Document document, String text) { this .document = document; this .text = text; this .document.write(text); } public void undo() { document.eraseLast(); } public void redo() { document.write(text); } } |
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 |
package com.gpcoder.patterns.behavioral.command.document; import java.util.Stack; public class DocumentInvoker { private Stack<Command> undoCommands = new Stack<>(); private Stack<Command> redoCommands = new Stack<>(); private Document document = new Document(); public void undo() { if (!undoCommands.isEmpty()) { Command cmd = undoCommands.pop(); cmd.undo(); redoCommands.push(cmd); } else { System.out.println( "Nothing to undo" ); } } public void redo() { if (!redoCommands.isEmpty()) { Command cmd = redoCommands.pop(); cmd.redo(); undoCommands.push(cmd); } else { System.out.println( "Nothing to redo" ); } } public void write(String text) { Command cmd = new DocumentEditorCommand(document, text); undoCommands.push(cmd); redoCommands.clear(); } public void read() { document.readDocument(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
package com.gpcoder.patterns.behavioral.command.document; public class Client { public static void main(String[] args) { DocumentInvoker instance = new DocumentInvoker(); instance.write( "The 1st text. " ); instance.undo(); instance.read(); // EMPTY instance.redo(); instance.read(); // The 1st text. instance.write( "The 2nd text. " ); instance.write( "The 3rd text. " ); instance.read(); // The 1st text. The 2nd text. The 3rd text. instance.undo(); // The 1st text. The 2nd text. instance.undo(); // The 1st text. instance.undo(); // EMPTY instance.undo(); // Nothing to undo } } |
1
2
3
4
5 |
The 1st text. The 1st text. The 2nd text. The 3rd text. Nothing to undo |
Cùng nhau học tập, khám phá các kiến thức nền tảng về Lập trình web, mobile, database nhé.
Nền tảng kiến thức - Hành trang tới tương lai hân hạnh phục vụ Quý khách!
Khám phá, trải nghiệm ngay
Vui lòng đăng nhập để gởi bình luận!
Đăng nhậpChưa có bình luận nào!