






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Label semantic attributes of a class
A way of notifying change to a number of classes
Alter an object's behavior when its state changes
Defines a new operation to a class without changing class
Marker Interface Pattern
Label semantic attributes of a class
Need to indicate attribute(s) of a class Allows identification of attributes of objects without assuming they are instances of any particular class
Marker Interface Pattern
Classes with desirable property GoodProperty Original Store flag for GoodProperty in each class Using pattern Label class using GoodProperty interface
Cloneable Serializable
Observer Pattern
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
Observer Example
public interface Observer { public void update(Observable o, Object a) // called when observed object o changes }
public class Observable extends Object { 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 }
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() } }
State Pattern
Represent change in an object’s behavior using its member classes
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
State Example
public interface State { boolean isHatOn(); String requestFruit(); }
public class WearingHat implements State; public class NotWearingHat implements State;
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(); }
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
Visitor Pattern
Define operations on elements of data structures without changing their classes
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
Visitor Example
public interface Visitor { public void visitCollection(Collection c); public void visitString(String string); public void visitFloat(Float float); }
public interface Visitable { public void accept(Visitor v); }
Visitor Example
public class VisitableString implements Visitable { private String value; public VisitableString(String s) { value = s; } public void accept(Visitor v) { v.visitString(this); } }
public class VisitableFloat implements Visitable { private Float value; public VisitableFloat(Float f) { value = f; } public void accept(Visitor v) { v.visitFloat(this); } }
Double dispatch
Visitor Example
public class PrintVisitor implements Visitor { public void visitCollection(Collection c) { Iterator iterator = c.iterator() while (iterator.hasNext()) { Object o = iterator.next(); if (o instanceof Visitable) ((Visitable) o).accept(this); } public void visitString(String s) { System.out.println("'"+s+"'"); } public void visitFloat(Float f) { System.out.println(f.toString()+"f"); } }
UML Class Diagram of Abstract Visitor