Design Patterns: Object-Oriented Programming II - University of Maryland, College Park, Study notes of Computer Science

An overview of various design patterns including marker interface, observer, state, and visitor. Each pattern definition, use cases, benefits, and examples are discussed. The marker interface pattern allows identification of attributes of objects without assuming they are instances of any particular class. The observer pattern updates all dependents of an object automatically once its state changes. The state pattern represents change in an object's behavior using its member classes. The visitor pattern defines operations on elements of data structures without changing their classes.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-3ca
koofers-user-3ca 🇺🇸

9 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
CMSC 132:
Object-Oriented Programming II
Design Patterns II
Department of Computer Science
University of Maryland, College Park
2
More Design Patterns
Marker interface
Label semantic attributes of a class
Observer
A way of notifying change to a number of classes
State
Alter an object's behavior when its state changes
Visitor
Defines a new operation to a class without changing
class
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Design Patterns: Object-Oriented Programming II - University of Maryland, College Park and more Study notes Computer Science in PDF only on Docsity!

1

CMSC 132:

Object-Oriented Programming II

Design Patterns II

Department of Computer Science

University of Maryland, College Park

2

More Design Patterns

Marker interface

Label semantic attributes of a class

Observer

A way of notifying change to a number of classes

State

Alter an object's behavior when its state changes

Visitor

Defines a new operation to a class without changing class

3

Marker Interface Pattern

Definition

Label semantic attributes of a class

Where to use & benefits

Need to indicate attribute(s) of a class Allows identification of attributes of objects without assuming they are instances of any particular class

4

Marker Interface Pattern

Example

Classes with desirable property GoodProperty Original Store flag for GoodProperty in each class Using pattern Label class using GoodProperty interface

Examples from Java

Cloneable Serializable

7

Observer Pattern

Example

Multiple windows (views) for single document Original Each window checks document Window updates image if document changes Using pattern Each window registers as observer for document Document notifies all of its observers when it changes

Doc

Window

Doc

Window

Window

Window

8

Observer Example

public interface Observer { public void update(Observable o, Object a) // called when observed object o changes }

public class Observable { protected void setChanged() // changed protected void clearChanged() // no change boolean hasChanged() // return changed?

void addObserver(Observer o) // track observers void notifyObservers() // notify if changed, void notifyObservers(Object a) // then clear change }

9

Observer Example

public class MyWindow implements Observer { public openDoc(Observable doc) { doc.addObservers(this); // add window to list } public void update(Observable doc, Object arg) { redraw(doc); // display updated document } } public class MyDoc extends Observable { public void edit() { … // edit document setChanged(); // mark change notifyObservers(arg); // invokes update() } }

10

State Pattern

Definition

Represent change in an object’s behavior using its member classes

Where to use & benefits

Control states without many if-else statements Represent states using classes Every state has to act in a similar manner Simplify and clarify the program

13

State Example

public interface State { boolean isHatOn(); String requestFruit(); }

public class WearingHat implements State; public class NotWearingHat implements State;

14

State Example

public class FickleFruitVendor { State wearingHat = new WearingHat(); State notWearingHat = new NotWearingHat();

// explicitly track current state of Vendor State currentState = wearingHat;

// behavior of Vendor depends on current state public boolean isHatOn() { return currentState.isHatOn(); } public String requestFruit() { return currentState.requestFruit(); }

15

State Example

class WearingHat implements State { boolean isHatOn() { return true; } String requestFruit() { currentState = notWearingHat; // change state return "Banana"; } } class NotWearingHat implements State { boolean isHatOn() { return false; } String requestFruit() { currentState = wearingHat; // change state return "Apple"; } } } // end class

Wearing Hat

Not Wearing Hat Banana

Apple

16

Visitor Pattern

Definition

Define operations on elements of data structures without changing their classes

Where to use & benefits

Add operations to classes with different interfaces Can modify operations on data structure easily Encapsulate operations on elements of data structure Decouples classes for data structure and algorithms Crossing class hierarchies may break encapsulation

19

Visitor Example

public interface Visitor { public void visit(VisitableString s); public void visit(VisitableFloat f); }

public interface Visitable { public void accept(Visitor v); }

20

Visitor Example

public class VisitableString implements Visitable { private String value; public VisitableString(String s) { value = s; } public String toString( ) { return value.toString( ); } public void accept(Visitor v) { v.visit(this); } } public class VisitableFloat implements Visitable { private Float value; public VisitableFloat(Float f) { value = f; } public String toString( ) { return value.toString( ); } public void accept(Visitor v) { v.visit(this); } } Double dispatch

21

Visitor Example

public class PrintVisitor implements Visitor { public void visitCollection(Collection c) { for (Object o : c) { if (o instanceof Visitable) ((Visitable) o).accept(this); else System.out.println(o.toString()); } } public void visit(VisitableString s) { System.out.println(“{"+s.toString( )+“}"); } public void visit (VisitableFloat f) { System.out.println(f.toString( )+"f"); } }

22

UML Class Diagram of Abstract Visitor