Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 210 Introduction to Design Patterns September 28 th, 2006.

Similar presentations


Presentation on theme: "CS 210 Introduction to Design Patterns September 28 th, 2006."— Presentation transcript:

1 CS 210 Introduction to Design Patterns September 28 th, 2006

2 Head First Design Patterns Chapter 6 Command Pattern Encapsulating Invocation

3 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

4 Introducing the command pattern Create Command Object execute()setCommandexecute() action1 action2 creatCommandObject() setCommand() execute() action_X()

5 Command Pattern for home automation action() execute(){ receiver.action() } execute() An encapsulated Request Invoker

6 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.

7 Command Pattern Class Diagram ClientInvoker setCommand() > Command execute() undo() Receiver action() ConcreteCommand execute() undo()

8 Command Pattern Class Diagram for Home automation RemoteLoader RemoteControl onCommands offCommands setCommand() onButtonPushed() offButtonPushed() > Command execute() undo() Light on() off() LightOnCommand execute() undo() LightOffCommand execute() undo()

9 Command pattern – Undo operation Eclipse code review

10 Macro Commands – Party mode public 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(); }

11 Macro Command – Party mode Eclipse code review

12 Page 228 – Head First Design Patterns

13

14 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.

15 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.


Download ppt "CS 210 Introduction to Design Patterns September 28 th, 2006."

Similar presentations


Ads by Google