Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 62 trang
THÔNG TIN TÀI LIỆU
Cấu trúc
Behavioral design patterns
Slide 2
The Command Pattern
The Command Pattern Example
Motivation Example
A Pictorial View
Observations, anyone?
Enter – The Command Pattern!
Simple Remote Control: Only one button
Coding Our Command Object
Using the Command Object
Simple test to use the Remote Control
The Command Pattern Defined
Applicability
The Remote Control Example
Implementing the RemoteControl
Implementing the Commands
Testing the Code
The NoCommand?
Adding the Undo Feature
Updating the RemoteControl class
RemoteControl Class
Using State to Implement Undo
Adding Undo to Ceiling Fan Commands
Using the Macro Command
Macro Command
More Uses - Queuing Requests
More Uses: Logging Requests
Summary
Observer Pattern
Motivation Weather Station
The Weather-O-Rama!
The WeatherData class
A First Misguided Attempt at the Weather Station
What’s wrong with the first implementation?
The Observer Pattern
Motivation for Observer Pattern
Structure Observer Pattern
Designing the Weather Station
Implementing the Weather Station
Implementing the Subject Interface
The Display Elements
Using Java’s Built-in Observer Pattern
Slide 44
Consequences
Implementation Issues: Updates
Update Protocols: Push or Pull
Slide 48
State Pattern
Intent and Structure
Slide 51
Example
Strategy Pattern
Slide 54
Slide 55
Visitor Pattern
Intent and Applicability
Structure
Slide 59
Chain of Responsibility Pattern
Slide 61
Slide 62
Nội dung
1 Behavioral design patterns 2 Behavioral design patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor 3 The Command Pattern Encapsulating Invocation 4 The Command Pattern Example 5 Motivation Example Program a Remote Control that can be used to control the devices in the house Requirements: Remote control features seven programmable slots Each slot can be assigned to a different household device Each slot has a corresponding on/off button Remote has a global undo button that undoes the last button pressed. A set of vendors classes are provided to give you an idea of the interfaces of the objects that need to be controlled from the remote. 6 A Pictorial View on Off Seven slots to program Each slot contains a different device and is controlled via the buttons. On and off buttons for each of the seven slots These two control device 1 Global “undo” button CeilingLight on ( ) off ( ) dim ( ) TV on ( ) off ( ) setInputChannel ( ) setVolume ( ) FaucetControl openValue ( ) closeValue ( ) Thermostat setTemperature ( ) Hottub circulate ( ) jetsOn ( ) jetsOff ( ) setTemperature ( ) Vendor Classes 7 Observations, anyone? Expectation: see a bunch of classes with on() and off() methods > common interface Instead: each vendor class has its own API: dim(), setTemperature(), setVolume(), setDirection() Observation: Separation of concerns: remote should know how to interpret button presses and make requests, but it really should not know anything about home automation or how to turn on a hot tub. Any issues? If the remote is dumb, how do we design a remote so that it can invoke an action - turn on a light? Requirement: There will be more vendor classes in the future - design must accommodate for that 8 Enter – The Command Pattern! Command Pattern allows you to decouple the requester of an action from the object that actually performs the action Requester == remote control, Object == vendor classes How does it work? Introduce "command objects" into your design. A command object encapsulates a request to do something (like turn on a light) on a specific object (say, the living room light). Every time a remote button is pressed, its corresponding command object goes to work! Remote does not have any idea what the work is, it just has a command object that knows how to talk to the right object to get the work done. Here the remote is decoupled from the object! 9 Simple Remote Control: Only one button 10 Coding Our Command Object Command Interface: public interface Command { public void execute (); } Implementing a Command to turn the light on public class LightOnCommand implements Command { Light light; public LightOnCommand(Light light) { this.light = light; } public void execute() { light.on(); } } Simple. All we need is one method called execute ( ). The constructor is passed the specific light that this command is going to control. When execute() gets called, this light object is the Receiver of the request. The execute method calls the on() method on the receiving object, which is the light that we are controlling. [...]... interface to talk to the display elements… they all have an update ( ) method that takes temp, humidity and pressure values 35 The Observer Pattern OO Design Principle Strive for loosely coupled designs between objects that interact Loosely coupled designs allow us to build flexible OO systems that can handle changes because they minimize the interdependency between objects The Observer Pattern . 1 Behavioral design patterns 2 Behavioral design patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template. the remote is dumb, how do we design a remote so that it can invoke an action - turn on a light? Requirement: There will be more vendor classes in the future - design must accommodate for that . Object == vendor classes How does it work? Introduce "command objects" into your design. A command object encapsulates a request to do something (like turn on a light) on a specific