CS 210 Introduction to Design Patterns September 28 th, 2006.
-
Upload
ambrose-adams -
Category
Documents
-
view
215 -
download
1
Transcript of CS 210 Introduction to Design Patterns September 28 th, 2006.
![Page 1: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/1.jpg)
CS 210
Introduction to Design Patterns
September 28th, 2006
![Page 2: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/2.jpg)
Head First Design Patterns
Chapter 6
Command Pattern
Encapsulating Invocation
![Page 3: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/3.jpg)
Motivating problem description
Build a remote that will control variety of home devices
Sample devices: lights, stereo, TV, ceiling light, thermostat, sprinkler, hot tub, garden light, ceiling fan, garage door
![Page 4: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/4.jpg)
Introducing the command pattern
CreateCommandObject
execute()
setCommand
execute()action1action2
creatCommandObject()
setCommand()
execute()
action_X()
![Page 5: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/5.jpg)
Command Pattern for home automation
action()
execute(){ receiver.action()}
execute()
execute()
execute()
An encapsulated Request
Invoker
![Page 6: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/6.jpg)
Command Pattern defined
The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.
![Page 7: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/7.jpg)
Command Pattern Class Diagram
Client Invoker
setCommand()
<<Interface>>Command
execute()undo()
Receiver
action()
ConcreteCommand
execute()undo()
![Page 8: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/8.jpg)
Command Pattern Class Diagram for Home automation
RemoteLoader
RemoteControl
onCommandsoffCommandssetCommand()onButtonPushed()offButtonPushed()
<<Interface>>Command
execute()undo()
Light
on()off()
LightOnCommand
execute()undo()
LightOffCommand
execute()undo()
![Page 9: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/9.jpg)
Command pattern – Undo operation
Eclipse code review
![Page 10: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/10.jpg)
Macro Commands – Party modepublic class MacroCommand implements Command {
Command[] commands; public MacroCommand(Command[] commands) { this.commands = commands;}
public void execute() {for (int i = 0; i < commands.length; i++) {
commands[i].execute();}
}
public void undo() {for (int i = 0; i < commands.length; i++) {
commands[i].undo();}
}}
![Page 11: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/11.jpg)
Macro Command – Party mode
Eclipse code review
![Page 12: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/12.jpg)
Page 228 – Head First Design Patterns
![Page 13: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/13.jpg)
![Page 14: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/14.jpg)
Summary so far.. OO Basics
• Abstraction• Encapsulation• Inheritance• Polymorphism
OO Principles• Encapsulate what varies• Favor composition over inheritance• Program to interfaces not to implementations• Strive for loosely coupled designs between objects that interact• Classes should be open for extension but closed for modification.• Depend on abstracts. Do not depend on concrete classes.
![Page 15: CS 210 Introduction to Design Patterns September 28 th, 2006.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e7c5503460f94b7d9ae/html5/thumbnails/15.jpg)
Summary so far… OO Patterns
• Strategy Pattern defines a family of algorithms, Encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
• Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.
• Decorator Pattern – attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative for sub-classing for extending functionality
• Abstractor Factory – Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
• Factory Method – Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to the subclasses.
• Command Pattern – Encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.