Đang chuẩn bị liên kết để tải về tài liệu:
Design Patterns FOR Dummies phần 9
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Nhưng bạn có thực sự cần Invoker? Tất cả những gì bạn đã làm là gọi phương thức chạy của Invoker, được gọi là các lệnh thực thi phương pháp, bạn có thể có được gọi là lệnh thực hiện phương pháp cho mình. Nhưng có một cái nhìn vào định nghĩa GOF cho mô hình này một lần nữa: | Chapter 10 Coordinating Your Objects with the Command and Mediator Patterns 245 Why invoke the invoker But did you really need the invoker All you did was call the invoker s run method which called the command s execute method you could have called the command s execute method yourself. But take a look at the GoF definition for this pattern again Encapsulate a request as an object thereby letting you parameterize clients with different requests queue or log requests and support undoable operations. What about that parameterize clients with different requests What s that all about Say you had a dedicated set of invokers each with different names for example one might be called panicbutton. When there s a problem you don t have to think about what you re doing you just hit the panicbutton invoker s run method. As the code enters different states the command loaded into the panic button invoker may differ but you don t have to think about that if there s a problem you just hit the panicbutton invoker s run method. That s one reason to use invokers. Another reason comes from the rest of the GoF definition . . . queue or log requests and support undoable operations. Invokers can keep track of entire queues of commands which is useful if you want to start undoing sequences of commands. That s coming up next. And when you undo a reboot command you would shut down the server. public class RebootCommand implements Command Receiver receiver public RebootCommand Receiver r receiver r public void execute receiver.connect receiver.reboot receiver.disconnect System.out.println public void undo System.out.printlnCUndoing receiver.connect receiver.shutdown receiver.disconnect System.out.println 246 Part II Becoming an OOP Master__ On the other hand you can t really undo a run diagnostics command once you ve run the diagnostics you can t undo them. public class RunDiagnosticsCommand implements Command Receiver receiver public RunDiagnosticsCommand .