2/12/15 Template Method Design Patterns Problem: Some classes - - PDF document

2 12 15
SMART_READER_LITE
LIVE PREVIEW

2/12/15 Template Method Design Patterns Problem: Some classes - - PDF document

2/12/15 Template Method Design Patterns Problem: Some classes have a similar algorithm, but it is a little different for each class. Solution: Define the skeleton of the algorithm as a method in a superclass, deferring some steps to


slide-1
SLIDE 1

2/12/15 1

1

Design Patterns

Template Method, Façade, Adapter, Observer, Command

CS361 427-22 2

Template Method

❚ Problem: Some classes have a similar algorithm, but it is a little different for each class. ❚ Solution: Define the skeleton of the algorithm as a method in a superclass, deferring some steps to subclasses.

427-22 3

Template Method

❚ A template method calls abstract methods. ❚ Subclasses provide concrete implementations

  • f abstract methods.

❚ Template method in superclass calls methods in subclass ❚ Template Method separates the invariant part

  • f an algorithm from the parts that vary with

each subclass

427-22 4

In Swing’s JComponent

public void paint(Graphics g) { Graphics cg = getComponentGraphics(g); Graphics co = SwingGraphics.createSwingGraphics(cg); paintComponent(co); paintBorder(co); paintChildren(co); }

427-22 5

Where do template methods come from?

❚ Two classes, each with a paint() method. ❚ All getComponentGraphics and then createSwingGraphics, but do different things with them. ❚ Some have border, some don’t. Some have children, some don’t. paintComponent(co); paintBorder(co); paintChildren(co);

427-22 6

Making Template Methods

❚ Given two methods with same name in different classes

❙ Mark places that are DIFFERENT ❙ Extract those places into separate methods in same classes ❙ Methods are now the same - move to superclass

slide-2
SLIDE 2

2/12/15 2

427-22 7

Façade Design Pattern

Provide a unified interface to a set of interfaces in a

  • subsystem. Define a higher-level interface that makes

the subsystem easier to use.

Facade

427-22 8

Why Façade?

❚ Hide complexity of subsystem ❚ Make a single API instead of a set of classes ❚ Allow subsystem to change without affecting clients

427-22 9

Adapter Design Pattern

Intent: Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. An adapter object will make the adaptee act like it has the target interface.

427-22 10

Adapter Pattern

Target Adapter Client Adaptee

11

Observer Design Pattern

❚ Intent: when one object changes state, all the dependent objects are notified and updated. ❚ one-to-many dependency between objects ❙ Allows for consistency between related

  • bjects without tightly coupling classes

❘ i.e., “reduces coupling between objects”

Example

CS361 1-12

slide-3
SLIDE 3

2/12/15 3 Key Players

❚ Subject

❙ Knows its observers – provides interface for adding/ removing observers ❙ Defines an interface for notifying the subjects of changes to the object

❚ Observer

❙ Defines an updating interface

❚ ConcreteSubject

❙ Sends notification to observers when state changes

❚ ConcreteObserver

❙ Implements Observer interface to keep state consistent with subject

CS361 13 CS361 14

Observer

Observer Update Subject AddObserver RemoveObserver Notify() Observer PieChart Update

  • bserver/dependent

DataObject Observable * BarChart Update

CS361 15

Using Observer

❚ Decide whether object is Subject, or Observer ❚ Subjects must call notify() when they change state ❚ Observers must define update() ❚ Observers must register with Subjects

CS361 16

Dependence

❚ If package A depends on package B then you can’t run the tests for A unless you also have B ❚ “A depends on B” means you can’t use A unless you have B

CS361 17

Cycles of dependence

If package A depends on package B then package B should NOT depend on package A. If classes C and D both depend on each

  • ther (what?)

put them in the same package.

CS361 18

Eliminating dependence

❚ The Observer pattern eliminates dependence. ❚ Example:

❙ Suppose that “FBIAgent” is an observer of

  • Mobster. Mobster is a subclass of Subject,

so it can have observers. Class FBIAgent probably depends on class Mobster, but Mobster does not depend on FBIAgent.

slide-4
SLIDE 4

2/12/15 4

CS361 19

Observer Pattern

Observer Update() Subject addDependent: removeDependent: notify() Mobster robBank driveCar FBIAgent Update()

*

CS361 20

Observer example from Eclipse: Tracking Java Element Changes Subject - JavaCore

addElementChangedListener()

Observer - ElementChangedListener()

elementChanged(ElementChangedEvent)

ElementChangedEvent has a set of JavaElementDeltas

CS361 21

Tracking in SWT

Just like Swing Subject - Button

addSelectionListener()

Observer - SelectionListener()

widgetSelected(SelectionEvent) widgetDefaultSelected(SelectionEvent)

Code Example

public interface Subject { public void addObserver( Observer o );
 public void removeObserver( Observer o ); public void notify(); } public interface Observer { public void update( Subject o ); }

CS361 22

Activity: implement code for Observer pattern

class IntegerSubject class HexObserver, OctObserver, BinaryObserver

implement update

CS361 23

  • 1 Subject: the user inputs an int number (read from keyboard)
  • 3 Observers: one displays (e.g., prints to console) the number in

Hex format, another in Octal format, another in Binary format implement addObserver,removeObserver,notify

Activity

class IntegerSubject implements Subject{ List observers; public void addObserver(Observer ob){

  • bservers.add(ob);

} public void setState( int in ) { state = in; notify(); } private void notify() { for (int i=0; i < totalObs; i++) {

  • bservers.get(i).update(this);

} }

CS361 24

slide-5
SLIDE 5

2/12/15 5 Activity

class HexObserver implements Observer {

public void update(Subject subj) { System.out.print(“”+ Integer.toHexString(subj.getState()) ); } }

CS361 25 CS361 26

Command Pattern

❚ Intent: encapsulate a request as an object command

❙ you can pass the "command" around until you can find which object can handle it, ❙ you can undo it, ❙ you can put it on a queue

❚ Command will have an execute() and an undo() method ❚ Each command is a separate subclass of Command

CS361 27

Command

ConcreteCommand Execute() Undo() Command Execute() Undo() Invoker Creator

Waiter Order Execute() Hamburger Execute() Hot Dogs Execute() Fries Execute() Chef Make Food()

CS361 28 29

When to use Command

❚ The Command object can be used when you need to tell the program to execute the command later.

❙ In such cases, you are saving commands as objects to be executed later

❚ Sending command objects over a network

  • r saving them in a collection class

CS361

When to use Command

❚ Support undo

❘ Commands can store additional state so that they can reverse the effect of a previous execute operation

CS361 30

slide-6
SLIDE 6

2/12/15 6 Examples of Command pattern in Eclipse

❙ Every Refactoring is a command (performChange returns the undo change) ❙ Copy/Paste/Cut and most of the menu actions that change the source code

CS361 1-31