Creational Design Patterns Structural Design Patterns Behavioral Design Patterns Applying Design Patterns

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 157

Chapter 6 Design Patterns

Creational Design Patterns Structural Design Patterns Behavioral Design Patterns Applying Design Patterns

Object Oriented Analysis and Design

GoF Design Patterns

Object Oriented Analysis and Design

GoF Design Pattern Categories


Purpose Creational Structural Behavioral Scope Class Factory Method Adapter Interpreter Template Method Object Abstract Factory Adapter Chain of Responsibility Builder Bridge Command Prototype Composite Iterator Singleton Decorator Mediator Facade Memento Proxy Observer Flyweight State Strategy Visitor
Object Oriented Analysis and Design
3

GoF Design pattern relationships

Object Oriented Analysis and Design

6.1 Creational Design Patterns


Factory Method Abstract Factory Builder Prototype Singleton

Object Oriented Analysis and Design

Creational patterns
Creational design patterns abstract the instantiation process. There are two recurring themes in these patterns.
First, they all encapsulate knowledge about which concrete classes the system uses. Second, they hide how instances of these classes are created and put together.

Object Oriented Analysis and Design

Abstract Factory - Motivation


Abstract Factory Motivation 1) Implement a user interface toolkit that supports multiple looks and feel standards such as Motif, Windows 95 or the finder in MacOS. How can you write a single user interface and make it portable across the different look and feel standards for these window managers? 2) Implement a facility management system for an intelligent house that supports different control systems such as Siemens Instabus, Johnson & Control Metasys or Zumtobes proprietary standard. How can you write a single control system that is independent from the manufacturer?
Object Oriented Analysis and Design
7

Abstract Factory
The Client remains blissfully unaware of the various concrete classes in this example. Client code deals with the simpler, abstract, general case.

Object Oriented Analysis and Design

Abstract Factory - Example


We have a class named SomeApp that depends on the interface Shape. Shape uses instances of Shape solely through the Shape interface. Problem: SomeApp also creates instances of Square and Circle and thus has to depend on the concrete classes.

SomeApp

<<Interface>>

Shape

Square
<<creates>>

Circle

Object Oriented Analysis and Design

Abstract Factory - Example


Solution: ShapeFactory interface.

Object Oriented Analysis and Design

10

Abstract Factory - Example


Problem: Every time we add a new Shape derivative, we have to add a method to the ShapeFactory. Solution:
public interface ShapeFactory { public Shape make (String shapeName) throws Exception } public class ShapeFactoryImplementation implements ShapeFactory { public Shape make(String shapeName) throws Exception { if (shapeName.equals("Circle")) return new Circle(); else if (shapeName.equals("Square")) return new Square(); else throw new Exception("ShapeFactory cannot create " + shapeName); } } private ShapeFactory factory; factory = new ShapeFactoryImplementation(); Shape s = factory.make("Circle");

Object Oriented Analysis and Design

11

Abstract Factory
Intent:
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Applicability - Use the Abstract Factory pattern when


1) Independence from Initialization or Represenation: The system should be independent of how its products are created, composed or represented 2) Manufacturer Independence: A system should be configured with one of multiple family of products You want to provide a class library for a customer (facility management library), but you dont want to reveal what particular product you are using. 3) Constraints on related products A family of related products is designed to be used together and you need to enforce this constraint 4) Cope with upcoming change: You use one particular product family, but you expect that the underlying technology is changing very soon, and new products will appear on the market.
Object Oriented Analysis and Design
12

Abstract Factory - Structure

Object Oriented Analysis and Design

13

Factory Method - example


For example, a framework for a windowing application has a class Application which must create an object of class Document But the actual applications and documents are not written yet! Solution: Let subclasses decide which objects to instantiate Application subclasses redefine an abstract CreateDocument operation on Application to return the appropriate Document subclass. Once an Application subclass is instantiated, it can then instantiate application-specific Documents without knowing their class. We call CreateDocument a factory method because it's responsible for "manufacturing" an object.
Object Oriented Analysis and Design
14

Factory Method - example


Separate creation into a method

Object Oriented Analysis and Design

the factory method in the MyApplication class: public Document CreateDocument() { return new MyDocument();} client code: public Application app1; app1 = new MyApplication(); app1.CreateDocument();
15

Factory Method
Intent:
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Structure

Object Oriented Analysis and Design

16

Builder Motivation (GoF)


Problem:
A reader for the RTF (Rich Text Format) document exchange format should be able to convert RTF to many text formats. The problem is that the number of possible conversions is open-ended. So it should be easy to add a new conversion without modifying the reader.

Solution:
to configure the RTFReader class with a TextConverter object that converts RTF to another textual representation. Subclasses of TextConverter specialize in different conversions and formats.

Object Oriented Analysis and Design

17

Builder - Example

Object Oriented Analysis and Design

18

Builder
Intent: Separate the construction of a complex object from its representation so that the same construction process can create different representations. Structure

Object Oriented Analysis and Design

19

Builder
Collaborations :
The client creates the Director object and configures it with the desired Builder object. Director notifies the builder whenever a part of the product should be built. Builder handles requests from the director and adds parts to the product. The client retrieves the product from the builder.

Object Oriented Analysis and Design

20

Builder - Example (GoF)


Client Code: Maze* maze; MazeGame game; StandardMazeBuilder builder;

game.CreateMaze(builder);

maze = builder.GetMaze();

Bulider Code: Director Code: class MazeBuilder { Maze* MazeGame::CreateMaze (MazeBuilder& public: builder) { virtual void BuildMaze() { } builder.BuildMaze(); virtual void BuildRoom(int room) { } builder.BuildRoom(1); virtual void BuildDoor(int roomFrom, int roomTo) builder.BuildRoom(2); {} builder.BuildDoor(1, 2); virtual Maze* GetMaze() { return 0; } return builder.GetMaze(); protected: } MazeBuilder(); }; ConcreteBuilder Code: class StandardMazeBuilder : public MazeBuilder { public: StandardMazeBuilder(); virtual void BuildMaze(); virtual void BuildRoom(int); virtual void BuildDoor(int, int); virtual Maze* GetMaze(); private: Direction CommonWall(Room*, Room*); Maze* _currentMaze; }; Object Oriented Analysis and Design 21

Prototype Motivation (GoF)


Problem:
The classes for notes and staves are specific to our application, but the GraphicTool class belongs to the framework. GraphicTool doesn't know how to create instances of our music classes to add to the score. We could subclass GraphicTool for each kind of music object, but that would produce lots of subclasses that differ only in the kind of music object they instantiate.

Solution:
making GraphicTool create a new Graphic by copying or "cloning" an instance of a Graphic subclass. We call this instance a prototype.

Object Oriented Analysis and Design

22

Prototype
Intent: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

Structure
A client asks a prototype to clone itself

Object Oriented Analysis and Design

23

Prototype
Discussion:
Declare an abstract base class that specifies a pure virtual "clone" method, and, maintains a dictionary of all "cloneable" concrete derived classes. Any class that needs a "polymorphic constructor" capability: derives itself from the abstract base class, registers its prototypical instance, and implements the clone() operation. The client then, instead of writing code that invokes the "new" operator on a hard-wired class name, calls a "clone" operation on the abstract base class, supplying a string or enumerated data type that designates the particular concrete derived class desired.

Object Oriented Analysis and Design

24

Prototype Example in Java


In SwimInfo.java sxdata = (SwimData)sdata.clone(); or: sxdata = (SwimData)sdata.deepClone(); In SwimData.java
public class SwimData implements Cloneable , Serializable {
public Object clone() { try{ return super.clone(); } catch(Exception e) {System.out.println(e.getMessage()); return null;} } public Object deepClone(){ try{ ByteArrayOutputStream b = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(b); out.writeObject(this); ByteArrayInputStream bIn = new ByteArrayInputStream(b.toByteArray()); ObjectInputStream oi = new ObjectInputStream(bIn); return (oi.readObject()); } catch (Exception e) { System.out.println("exception:"+e.getMessage()); e.printStackTrace(); return null; } }
Object Oriented Analysis and Design

25

Singleton
Intent: Ensure a class only has one instance, and provide a global point of access to it.

Structure

Object Oriented Analysis and Design

26

Singleton - example
public abstract class ForumFactory { private static Object initLock = new Object(); private static String className = "com.abc.forum.db.DbForumFactory"; private static ForumFactory factory = null; public static ForumFactory getInstance(Authorization authorization) { if (authorization == null) {return null;} if (factory == null) {// Singleton pattern synchronized(initLock) { if (factory == null) { ...... try { Class c = Class.forName(className); factory = (ForumFactory)c.newInstance(); catch (Exception e) {return null;} } } } ..... }
Object Oriented Analysis and Design
27

6.2 Structural patterns


Adapter Bridge Composite Faade Decorator Proxy Flyweight

Object Oriented Analysis and Design

28

Adapter Pattern Switch example


Light

Switch
Simple table lamp

+turnOn +turnOff

What dont we like about this design?


The violation of Dependency-Inversion Principle (DIP: Abstractions should not depend upon details. Details should depend upon abstractions):

The dependency from switch to light is a dependency upon a concrete class


The violation of Open-Closed Principle (OCP: Software entities should be open for extension, but closed for modification):

Switch cannot be easily extended to control objects other than Light

Object Oriented Analysis and Design

29

Adapter Pattern Switch example


Switch

Light
+turnOn +turnOff

Fan FanSwitch
+turnOn +turnOff

A bad way to extended Switch

It also violates the DIP


FanSwitch still inherits the dependency upon Light.

Object Oriented Analysis and Design

30

Adapter Pattern Switch example


<<Interface>>

Switch

Switchable
+turnOn +turnOff

Light
+turnOn +turnOff

ABSTRACT SERVER solution to the Table Lamp problem It satisfies both the DIP and the OCP But there is a potential violation of the Single-Responsibility Principle (SRP: A class should only one reason to change) We have bound together two things, Light and Switchable, that may
not change for the same reasons. What if we purchased Light from a third party?
Object Oriented Analysis and Design
31

Adapter Pattern Switch example


<<Interface>>

Switch

Switchable
+turnOn +turnOff

Light Adapter
+turnOn +turnOff
<<delegates>>

Light
+turnOn +turnOff

Solving the Table Lamp problem with the object form ADAPTER Note:
Adapter dont come cheap. You dont want to use adapters all the time The ABSTRACT SERVER solution is quite appropriate for most situations. In fact, even the simple solution is pretty good unless you happen to know that there are other objects for switch to control.
Object Oriented Analysis and Design
32

Adapter Pattern Switch example


<<Interface>>

Switch

Switchable
+turnOn +turnOff

Light Adapter
+turnOn +turnOff

Light
+turnOn +turnOff

Solving the Table Lamp problem with the class form ADAPTER

Object Oriented Analysis and Design

33

Adapter Pattern
Adapter Pattern 1) Convert the interface of a class into another interface expected by the client. Adapter lets classes work together that couldnt otherwise because of incompatible interfaces 2) Used to provide a new interface to existing legacy components (Interface engineering, reengineering). 3) Also known as a wrapper 4) Two adapter patterns: Class adapter: - Uses multiple inheritance to adapt one interface to another Object adapter: - Uses single inheritance and delegation 5) We will mostly use object adapters and call them simply adapters
Object Oriented Analysis and Design
34

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

Structure

Object Oriented Analysis and Design

35

Class Adapter Pattern


Class Adapter Pattern (based on Multiple Inheritance)

Object Oriented Analysis and Design

36

Adapter pattern uses delegation and inheritance


Adapter pattern uses delegation and inheritance

Delegation is used to bind an Adapter and an Adaptee 1) Interface inheritance is use to specify the interface of the Adapter class. 2) Adaptee, usually called legacy system, pre-exists the Adapter. 3) Target may be realized as an interface in Java.
Object Oriented Analysis and Design
37

Adapter - Example
Client Code:

Adaptee a = new Adaptee(); Target t = new Adapter(a); public void test() { t.request(); }
Target Code: Adaptee Code:

class Target { public void request() {} }

class Adaptee { public void specificRequest() {

System.out.println("Adaptee: SpecificRequest");
} }

Adapter Code:

class Adapter extends Target { private Adaptee adaptee; public Adapter(Adaptee a) { adaptee = a;} public void request() { adaptee.specificRequest();} }
Object Oriented Analysis and Design
38

Object Adapter - example


Adapter pattern example

Client

<<Interface>>

Enumeration

ServicesEnumeration

RegiteredServices

hasMoreElements() nextElement()
Object Oriented Analysis and Design

hasMoreElements() nextElement()

-adaptee

numServices() getService()

39

Object Adapter - example


public class ServicesEnumeration implements Enumeration { public boolean hasMoreElements() { return this.currentServiceIdx <= adaptee.numServices(); } public Object nextElement() { if (!this.hasMoreElements()) { throw new NoSuchElementException(); } return adaptee.getService(this.currentSerrviceIdx++); } }

Object Oriented Analysis and Design

40

Adapter Pattern Modem example


Hayes Modem Robotics Modem
<<Interface>>

Modem
+dial +hangup +send +receive

Modem Clients

Ernies Modem

Modem Problem

Problem:

Suppose that there were hundreds of modem clients all making happy use of the Modem interface. Now suppose that customer have given us a new kind of modem that dont dial - dedicated modem. There are several new applications (Ded Users) that use these dedicated modems and dont bother to dial. All the current modem clients to be able to use these dedicated modems and neednt to modify their applications.
Object Oriented Analysis and Design
41

Adapter Pattern Modem example


Hayes Modem Robotics Modem
<<Interface>>

Dialler
+dial +hangup
<<Interface>>

Ernies Modem

Modem Clients

Modem
+send +receive

Ded Users

Ideal solution to the Modem Problem

Problem:

Dedicated Modem

Unfortunately this requires us to make changes to all the


modem clients something that our customer forbade.

Object Oriented Analysis and Design

42

Adapter Pattern Modem example


Hayes Modem Robotics Modem
<<Interface>>

Modem
+dial +hangup +send +receive

Modem Clients

Ernies Modem

Dedicated Modem Adapter

Dedicated Modem +send <<delegates>>


+receive

Ded Users

Dial and Hangup are implemented to simulate connection state. Send and Receive are delegated to DedicatedModem

Solving the Modem Problem with ADAPTER

Object Oriented Analysis and Design

43

Bridge Pattern Modem example


Another way to look at the modem problem
Solving the Modem Problem by merging type hierarchies
Modem DialModem
Hayes Dial Modem Ernies Dial Modem

DedicatedModem
Hayes Dedicated Modem USR Dedicated Modem Ernies Dedicated Modem

USR Dial Modem

Object Oriented Analysis and Design

44

Bridge Pattern Modem example


Split the modem hierarchy into two hierarchies:
One represents the connection mothod The other represents the hardware
Modem Clients
<<Interface>> Modem +dial +hangup +send +receive <<Interface>> Modem +send +receive Dial and Hangup are implemented to simulate connection state. Send and Receive delegate to their respective imps.
Object Oriented Analysis and Design

ModemConnectionCo ntroller #dialImp #hangupImp #sendImp #receiveImp +dial +hangup +send +receive <<delegates>>

<<interface>> Modem Implementation +dial +hangup +send +receive

Ded Users

Hayes Modem DialModem Controller +dial +hangup +send +receive


45

USR Modem Ernies Modem

DedModem Controller +dial +hangup +send +receive

All mothods delegate to their respective imps.

Design Patterns - Bridge Pattern Example

How can we simplify this design?


Car

Ford

Toyota

Sporty

Truck

SportyFord

ToyotaTruck

FordTruck

SportyToyota

Object Oriented Analysis and Design

46

Design Patterns - Bridge Pattern Example


Apply the Bridge Design Pattern
Intent: Decouple a class abstraction from its implementation. - You might use Bridge when you might otherwise be

tempted to use multiple inheritance...

Car

CarManufacturer

Sporty
Object Oriented Analysis and Design

Truck
47

Ford

Toyota

Design Patterns - Bridge Pattern


Intent:
Decouple an abstraction from its implementation so that the two can vary independently. Solution: Abstraction forwards client requests to its Implementor object

Object Oriented Analysis and Design

48

Bridge Pattern - example

Object Oriented Analysis and Design

49

Bridge Pattern - example


Client Code: public void test1() { ClientService1 cs1 = new ClientService1(new Implementation1()); cs1.serviceA(); cs1.serviceB(); } public void test2() { ClientService1 cs1 = new ClientService1(new Implementation2()); cs1.serviceA(); cs1.serviceB(); } public void test3() { ClientService2 cs2 = new ClientService2(new Implementation1()); cs2.serviceC(); cs2.serviceD(); cs2.serviceE();} } Abstraction Code: class Abstraction { private Implementation implementation; public Abstraction(Implementation imp) { implementation = imp;} public void service1() {implementation.facility1(); implementation.facility2(); } public void service2() {implementation.facility2(); implementation.facility3(); } public void service3() {implementation.facility1(); implementation.facility2(); implementation.facility4();} protected Implementation getImplementation() {return implementation;} } class ClientService1 extends Abstraction { public ClientService1(Implementation imp) { super(imp); } public void serviceA() {service1(); service2();} public void serviceB() {service3();}} class ClientService2 extends Abstraction { public ClientService2(Implementation imp) { super(imp); } public void serviceC() {service2(); service3();} public void serviceD() {service1(); service3();} public void serviceE() {getImplementation().facility3();}}
50

Object Oriented Analysis and Design

Bridge Pattern - example


Implementation Code:
interface Implementation { void facility1(); void facility2(); void facility3(); void facility4();} class Library1 { public void method1() {System.out.println("Library1.method1()");} public void method2() {System.out.println("Library1.method2()"); }} class Library2 {public void operation1() {System.out.println("Library2.operation1()");} public void operation2() {System.out.println("Library2.operation2()");} public void operation3() {System.out.println("Library2.operation3()");}} class Implementation1 implements Implementation { private Library1 delegate = new Library1(); public void facility1() {System.out.println("Implementation1.facility1"); delegate.method1(); } public void facility2() {System.out.println("Implementation1.facility2"); delegate.method2(); } public void facility3() {System.out.println("Implementation1.facility3"); delegate.method2(); delegate.method1();} public void facility4() {System.out.println("Implementation1.facility4"); delegate.method1();} } class Implementation2 implements Implementation { private Library2 delegate = new Library2(); public void facility1() {System.out.println("Implementation2.facility1");delegate.operation1();} public void facility2() {System.out.println("Implementation2.facility2");delegate.operation2();} public void facility3() {System.out.println("Implementation2.facility3");delegate.operation3();} public void facility4() {System.out.println("Implementation2.facility4");delegate.operation1();} }
Object Oriented Analysis and Design
51

Design Patterns - Composite Pattern Example


Problem:
: Assembly
Contains Contains

: Part
Contains

: Assembly
Contains

: Part

: Part

: CatalogueEntry name = screw

: CatalogueEntry name = strut

Fig. A hierarchical assembly


Object Oriented Analysis and Design
52

Design Patterns - Composite Pattern Example


Solution:
Component + cost() : double n

Part

Assembly

0..1

+ cost() : double

+ cost() : double

Object Oriented Analysis and Design

53

Design Patterns - Composite Pattern Example


public abstract class Component { public abstract double cost () ; } public class Part extends Component { public double cost () { return entry.getCost(); } } public class Assembly extends Component { private Vector components = new Vector(); public double cost() { double total = 0.0; Enumeration enum = components. elements(); while (enum.hasMoreElements()) { total += ((Component) enum.nextElement()).cost();} return total; } }
Object Oriented Analysis and Design
54

Design Patterns - Composite Pattern


Intent:
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. Solution:

Object Oriented Analysis and Design

55

Design Patterns - Facade Pattern


Facade Pattern 1) Provides a unified interface to a set of objects in a subsystem. A facade defines a higher-level interface that makes the subsystem easier to use (i.e. it abstracts out the gory details) 2) Facades allow us to provide a closed architecture

Object Oriented Analysis and Design

56

Design Patterns - Open vs Closed Architecture


Open vs Closed Architecture 1) Open architecture: Any dealer management system can call any component or class operation of the PAID databases. 2) Why is this good? Efficiency 3) Why is this bad? Cant expect the client to understand how the subsystem works or any of the complex relationships that may exist within the subsystem. We can (pretty much) be assured that the subsystem will be misused, leading to non-portable code
Object Oriented Analysis and Design
57

Realizing a Closed Architecture with a Facade


Realizing a Closed Architecture with a Facade 1) The subsystem decides exactly how it is accessed. 2) No need to worry about misuse by clients 3) If a faade is used the subsystem can be used in an early integration We need to write only a driver

Object Oriented Analysis and Design

58

Decorator Pattern - Motivation


Widget Example

Suppose you have a user interface toolkit and you wish to make a border or scrolling feature available to clients without defining new subclasses of all existing classes. The client "attaches" the border or scrolling responsibility to only those objects requiring these capabilities. Widget* aWidget = new BorderDecorator(

new ScrollDecorator(new TextView), 1); aWidget->draw();

Stream Example
cascading responsibilities on to an output stream
Stream* aStream = new CompressingStream( new ASCII7Stream( new FileStream( "fileName.dat" ))); aStream->putString( "Hello world" );

Object Oriented Analysis and Design

59

Decorator Pattern - Motivation


VisualComponent draw() TextView draw() ScrollDecorator scrollPosition draw() scrollto() Decorator
draw()

+component component.draw()

BorderDecorator borderWidth draw() drawBorder()

super.draw() drawBorder()

Decorator subclasses are free to add operations for specific functionality. For example, ScrollDecorator's ScrollTo operation lets other objects scroll the interface if they know there happens to be a ScrollDecorator object in the interface.
Object Oriented Analysis and Design
60

Decorator Pattern - Motivation


Painting Example
Although paintings can be hung on a wall with or without frames, frames are often added, and it is the frame which is actually hung on the wall. Prior to hanging, the paintings may be matted and framed, with the painting, matting, and frame forming a single visual

Object Oriented Analysis and Design

61

Decorator Pattern - Structure


Intent
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

Structure

Structure

Object Oriented Analysis and Design

62

Decorator Pattern Example (TestEoF.java)


import java.io.*; public class TestEOF { public static void main(String[] args) throws IOException { DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("TestEof.java")));

while(in.available() != 0)
System.out.print((char)in.readByte()); }

}
Object Oriented Analysis and Design
63

Decorator Pattern Example(TestEoF.java)

Object Oriented Analysis and Design

64

Decorator Pattern Example (java.io.*)


java.io.FilterInputStream Code: public class FilterInputStream extends InputStream { protected InputStream in; protected FilterInputStream(InputStream in) {this.in = in;} public int read() throws IOException {return in.read();} } java.io. BufferedInputStream Code: public class BufferedInputStream extends FilterInputStream { protected byte buf[]; protected int count; protected int pos; protected int markpos = -1; public BufferedInputStream(InputStream in) {this(in, defaultBufferSize); } public synchronized int read() throws IOException { ensureOpen(); if (pos >= count) {fill(); if (pos >= count) return -1;} return buf[pos++] & 0xff;} private void ensureOpen() throws IOException {if (in == null) throw new IOException("Stream closed");} private void fill() throws IOException {if (markpos < 0) pos = 0; else if (pos >= buf.length) if (markpos > 0) { int sz = pos - markpos; System.arraycopy(buf, markpos, buf, 0, sz); pos = sz; markpos = 0;} else if (buf.length >= marklimit) {markpos = -1; pos = 0; } else {int nsz = pos * 2;if (nsz > marklimit) nsz = marklimit; byte nbuf[] = new byte[nsz]; System.arraycopy(buf, 0, nbuf, 0, pos); buf = nbuf;} count = pos; int n = in.read(buf, pos, buf.length - pos); if (n > 0) count = n + pos;} . }
Object Oriented Analysis and Design
65

Decorator Pattern - Example (java.io.*)


java.io. DataInputStream Code:
public class DataInputStream extends FilterInputStream implements DataInput { public DataInputStream(InputStream in) {super(in); } public final byte readByte() throws IOException { int ch = in.read(); if (ch < 0) throw new EOFException(); return (byte)(ch); } . }

Object Oriented Analysis and Design

66

Proxy Pattern - Motivation


What is expensive?
Object Creation Object Initialization

Defer creation and initialization to the time you need the object Reduce the cost of access to objects
Use another object (the proxy) that acts as a stand-in for the real object The proxy creates the real object only if the user asks for it

Object Oriented Analysis and Design

67

Proxy Pattern - Motivation


Example
The Proxy provides a surrogate or place holder to provide access to an object. A check or bank draft is a proxy for funds in an account. A check can be used in place of cash for making purchases and ultimately controls access to cash in the issuer's account.

Object Oriented Analysis and Design

68

Proxy Pattern - Structure


Intent
Provide a surrogate or placeholder for another object to control access to it.

Problem
You need to support resource-hungry objects, and you do not want to instantiate such objects unless and until they are actually requested by the client.

Structure

Object Oriented Analysis and Design

69

Proxy Pattern Simple Example (ProxyDemo.java)


Client Code: Proxy p = new Proxy(); public void test() { p.f(); p.g(); p.h(); }

Proxy and ProxyBase Code: interface ProxyBase { void f(); void g(); void h();} class Proxy implements ProxyBase { private ProxyBase implementation; public Proxy() { implementation = new Implementation(); } public void f() {implementation.f();} public void g() {implementation.g();} public void h() {implementation.h();} }
class Implementation implements ProxyBase { public void f() {System.out.println("Implementation.f()");} public void g() {System.out.println("Implementation.g()");} public void h() {System.out.println("Implementation.h()");} }
Object Oriented Analysis and Design
70

Proxy Example (ConnectionPoolProxyDemo.java)


Client Code:
public class ConnectionPoolProxyDemo extends TestCase { static {ConnectionPool.addConnections(5);} public void test() { Connection c = ConnectionPool.getConnection(); c.set(new Object()); c.get(); c.release(); } public void testDisable() { Connection c = ConnectionPool.getConnection(); String s = null; c.set(new Object()); c.get(); c.release(); try {c.get();} catch(Exception e) {s = e.getMessage();System.out.println(s);} assertEquals(s, "Tried to use reference after it was released");} public static void main(String args[]) { junit.textui.TestRunner.run(ConnectionPoolProxyDemo.class); } }

Object Oriented Analysis and Design

71

Proxy Example (ConnectionPoolProxyDemo.java)


Connection, ConnectionImplementation and ConnectionPool Code:
interface Connection { Object get(); void set(Object x); void release();} class ConnectionImplementation implements Connection { public Object get() { return null; } public void set(Object s) {} public void release() {}} class ConnectionPool { // A singleton private static PoolManager pool = new PoolManager(); private ConnectionPool() {} // Prevent synthesized constructor public static void addConnections(int number) { for(int i = 0; i < number; i++) pool.add(new ConnectionImplementation());} public static Connection getConnection() {
PoolManager.ReleasableReference rr = (PoolManager.ReleasableReference)pool.get();

if(rr == null) return null; return new ConnectionProxy(rr);} private static class ConnectionProxy implements Connection { private PoolManager.ReleasableReference implementation;
public ConnectionProxy(PoolManager.ReleasableReference rr) {implementation = rr;}

public Object get() {return ((Connection)implementation.getReference()).get();} public void set(Object x) {((Connection)implementation.getReference()).set(x);} public void release() { implementation.release(); }

}
}
Object Oriented Analysis and Design
72

Proxy Example (PoolManager.java)


PoolManager Code:
public class PoolManager { private static class PoolItem { boolean inUse = false; Object item; PoolItem(Object item) { this.item = item; } } public class ReleasableReference { // Used to build the proxy private PoolItem reference; private boolean released = false; public ReleasableReference(PoolItem reference) {this.reference = reference;} public Object getReference() { if(released) throw new RuntimeException("Tried to use reference after it was released"); return reference.item;} public void release() {released = true; reference.inUse = false;} } private ArrayList items = new ArrayList(); public void add(Object item) {items.add(new PoolItem(item));} public static class EmptyPoolItem {} public ReleasableReference get() { for(int i = 0; i < items.size(); i++) { PoolItem pitem = (PoolItem)items.get(i); if(pitem.inUse == false) {pitem.inUse = true; return new ReleasableReference(pitem);} } return null; // temporary } } Oriented Analysis and Design Object 73

Flyweight Pattern - Motivation


How can a document editor use objects to represent characters? The drawback of such a design is its cost. Even moderate-sized documents may require hundreds of thousands of character objects, which will consume lots of memory and may incur unacceptable run-time overhead. The Flyweight pattern describes how to share objects to allow their use at fine granularities without prohibitive cost. A flyweight is a shared object that can be used in multiple contexts simultaneously.
74

Object Oriented Analysis and Design

Flyweight Pattern Example


consider a DataPoint object

Suppose you need to create a million of these objects

class DataPoint { private static int count = 0; private int id = count++; private int i; private float f; public int getI() { return i; } public void setI(int i) { this.i = i; } public float getF() { return f; } public void setF(float f) { this.f = f; } public String toString() {return "id: " + id + ", i = " + i + ", f = " + f;} } This program may result in excessive slowness or running out of memory. public class ManyObjects { static final int size = 1000000; public static void main(String[] args) { DataPoint[] array = new DataPoint[size]; for(int i = 0; i < array.length; i++) array[i] = new DataPoint(); for(int i = 0; i < array.length; i++) { DataPoint dp = array[i]; dp.setI(dp.getI() + 1); dp.setF(47.0f); } System.out.println(array[size -1]); } Object Oriented Analysis and Design 75 }

Flyweight Pattern Example


To solve the problem the DataPoint can be reduced from a million objects to one object by externalizing the data held in the DataPoint
class ExternalizedData { static final int size = 5000000; static int[] id = new int[size]; static int[] i = new int[size]; static float[] f = new float[size]; static { for(int i = 0; i < size; i++) id[i] = i;} } public class FlyWeightObjects { public static void main(String[] args) { for(int i = 0; i < ExternalizedData.size; i++) { FlyPoint.setI(i, FlyPoint.getI(i) + 1); FlyPoint.setF(i, 47.0f); } System.out.println( FlyPoint.str(ExternalizedData.size -1)); } } class FlyPoint { private FlyPoint() {} public static int getI(int obnum) { return ExternalizedData.i[obnum]; } public static void setI(int obnum, int i) { ExternalizedData.i[obnum] = i; } public static float getF(int obnum) { return ExternalizedData.f[obnum]; } public static void setF(int obnum, float f) { ExternalizedData.f[obnum] = f; } public static String str(int obnum) { return "id: " + ExternalizedData.id[obnum] + ", i = + ExternalizedData.i[obnum] + ", f = " + ExternalizedData.f[obnum]; }

Now all the data is in ExternalizedData each call to a FlyPoint method must include the index into ExternalizedData
Object Oriented Analysis and Design
76

Flyweight Pattern - Structure


Intent
Use sharing to support large numbers of fine-grained objects efficiently.

Structure

Object Oriented Analysis and Design

77

Flyweight Pattern Example


The Flyweight uses sharing to support large numbers of objects efficiently Example: The public switched telephone network
There are several resources such as dial tone generators, ringing generators, and digit receivers that must be shared between all subscribers. A subscriber is unaware of how many resources are in the pool when he or she lifts the handset to make a call. All that matters to subscribers is that a dial tone is provided, digits are received, and the call is completed.

Object Oriented Analysis and Design

78

6.3 Behavioral design patterns


Observer pattern Command Pattern Chain of Responsibility Visitor Template Method Strategy pattern State pattern Iterator Memento Mediator
79

Object Oriented Analysis and Design

Observer pattern
Also known as: Publish / Subscribe, Model / View, and Source / Listener. Motivation: Two File Managers are both observing the same Directory; the user deletes a subdirectory using File Manager A; we want File Manager B to immediately and automatically get updated, reflecting the change... Applicability: When there are multiple views of a model (subject) that need to stay in sync. No view should know about any other. When an object needs to communicate to other objects of unknown type (but known Observer interface) it can notify them. Pros: Promotes loose coupling between objects. Excellent communication protocol. Avoids polling Cons: None. Knowledge of this pattern is essential.

Object Oriented Analysis and Design

80

Observer pattern
Intent: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Object Oriented Analysis and Design

81

Observer pattern Example

Object Oriented Analysis and Design

82

Java Support for Observer


l The java.util package provides an Observable class and an Observer interface:

Object Oriented Analysis and Design

83

Example: A GUI Observes a Person

Object Oriented Analysis and Design

84

Example: A GUI Observes a Person

Object Oriented Analysis and Design

85

Example: Java AWT 1.1 Event Model

Object Oriented Analysis and Design

86

Example: event-driven programming

Object Oriented Analysis and Design

87

Example: event-driven programming


import javax.swing.*; import java.awt.event.*; import java.awt.*;

import com.bruceeckel.swing.*;

public class Button2 extends JApplet { JButton b1 = new JButton("Button 1"), b2 = new JButton("Button 2"); JTextField txt = new JTextField(10);

Event Sources

class BL implements ActionListener {

Events

public void actionPerformed(ActionEvent e){ String name = ((JButton)e.getSource()).getText(); txt.setText(name);} } BL al = new BL(); public void init() { b1.addActionListener(al); b2.addActionListener(al); cp.add(b1); cp.add(b2); cp.add(txt); } public static void main(String[] args) {Console.run(new Button2(), 200, 75);} }Object Oriented Analysis and Design
88

Event handle r

Register event handler to event source

Container cp = getContentPane(); cp.setLayout(new FlowLayout());

Example: event-driven programming


In the case of a JButton, this event of interest is that the button is pressed. To register your interest in when a button is pressed, you call the JButtons addActionListener( ) method. This method expects an argument that is an object that implements the ActionListener interface, which contains a single method called actionPerformed( ). So all you have to do to attach code to a JButton is to implement the ActionListener interface in a class, and register an object of that class with the JButton via addActionListener( ). The method will be called when the button is pressed (this is normally referred to as a callback). <<Interface>> Event
Button2 init() : void JButton
(from swing)

Event Sources

EventListener
(from util)

handle r

EventObject
(from util)

getSource() : Object <<Interface>> ActionListener

AbstractButton
(from swing)

(from event)

actionPerformed(arg0 : ActionEvent) : void

AWTEvent
(from awt)

addActionListener(arg0 : ActionListener) : void

Events
BL
(from Button2)

Register event handler to event source


Object Oriented Analysis and Design

ActionEvent
(from event)

actionPerformed(arg0 : ActionEvent) : void


89

Example: Sequence diagram for scenario: Change filename to foo


Sequence diagram for scenario: Change filename to foo

Object Oriented Analysis and Design

90

Command Pattern - Motivation


You want to make the user interface reusable across many applications
You cannot hardcode the meanings of the menus for the various applications The applications only know what has to be done when a menu is selected.

You want to support Undo and Redo

Object Oriented Analysis and Design

91

Command Pattern - Example


The "check" at a diner
The waiter or waitress takes an order or command from a customer and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of "checks" used by each waiter is not dependent on the menu, and therefore they can support commands to cook many different items.

Object Oriented Analysis and Design

92

Command Pattern Example


Client Code: Macro macro = new Macro(); macro.add(new Hello()); macro.add(new World()); macro.add(new IAm()); macro.run();

// An object that holds commands


class Macro { private List commands = new ArrayList(); public void add(Command c) { commands.add(c); } public void run() { Iterator it = commands.iterator(); while(it.hasNext()) ((Command)it.next()).execute(); } }

Command object:
class Hello implements Command {public void execute() {System.out.print("Hello ");}} class World implements Command {public void execute() {System.out.print("World! ");}} class IAm implements Command { public void execute() {System.out.print("I'm the command pattern!");} }

Command interface: interface Command { void execute();}

Object Oriented Analysis and Design

93

Command pattern
Intent
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Structure

The Invoker offers a variety of commands

Client instantiates the ConcreteCommands and sets its Receiver.

ConcreteCommand implements execute() by calling corresponding operation(s) in Receiver.

Receiver knows how to perform the operation.


Object Oriented Analysis and Design
94

Example: Application independent Menus

Object Oriented Analysis and Design

95

Command Pattern Example in Wmvc


Client Code in WmvcController.java: public class WmvcController implements ActionListener, ItemListener{ private WmvcExecutor wmvcExecutor; public WmvcController(JComponent comp,String tip, WmvcExecutor wExec) { wmvcExecutor = wExec; } public void actionPerformed(ActionEvent event) { if (wmvcExecutor != null) wmvcExecutor.execute(event);} public void itemStateChanged(ItemEvent event) { if (wmvcExecutor != null) wmvcExecutor.execute(event);} } Concrete command (anonymous WmvcExecutor classes) in MainView.java: WmvcMenuItemCtl fileOpen = new WmvcMenuItemCtl ( fileMenu, "Open","images/open-16.gif", 'O', null, new WmvcExecutor() { public void execute(ActionEvent event){ }} ); Command interface: public class WmvcExecutor{ public void execute(ActionEvent event){} public void execute(ItemEvent event){} public void execute(ChangeEvent event){} }
Object Oriented Analysis and Design
96

Chain of Resposibility Pattern - Motivation


Consider a context-sensitive help facility for a GUI
The user can obtain help information on any part of the interface just by clicking on it. The help that's provided depends on the part of the interface that's selected and its context.

Problem
How to decouple the button that initiates the help request from the objects that might provide help information?

Solution
to decouple senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. The first object in the chain receives the request and either handles it or forwards it to the next candidate on the chain, which does likewise. The object that made the request has no explicit knowledge of who will handle it
Object Oriented Analysis and Design
97

Chain of Resposibility Pattern - Motivation

Object Oriented Analysis and Design

98

Chain of Resposibility Sample Code


Client Code:
Application* application = new Application(APPLICATION_TOPIC); Dialog* dialog = new Dialog(application, PRINT_TOPIC); Button* button = new Button(dialog, PAPER_ORIENTATION_TOPIC); button->HandleHelp();

class HelpHandler { private: HelpHandler* _successor; public: HelpHandler ( HelpHandler* h, Topic t ) : _successor(h), _topic(t) { } virtual void HandleHelp() { if (_successor != 0) { _successor->HandleHelp(); } } .};
class Widget : public HelpHandler {}; class Button : public Widget { public: virtual void HandleHelp() { if (HasHelp()) { // offer help on the button } else { HelpHandler::HandleHelp(); //the request gets forwarded to the successor using the HandleHelp operation in HelpHandler. }} ; class Dialog : public Widget { public: virtual void HandleHelp() { if (HasHelp()) { // offer help on the button } else { HelpHandler::HandleHelp();}} ; class Application : public HelpHandler { public: Application(Topic t) : HelpHandler(0, t) { } virtual void HandleHelp() { // show a list of help topics }};
Object Oriented Analysis and Design
99

Chain of Resposibility Pattern - Example


Mechanical coin sorting banks
Rather than having a separate slot for each coin denomination coupled with a receptacle for the denomination, a single slot is used. When the coin is dropped, the coin is routed to the appropriate receptacle by the mechanical mechanisms within the bank.

Object Oriented Analysis and Design

100

Chain of Resposibility Pattern


Intent Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Structure

Object Oriented Analysis and Design

101

Visitor Pattern Modem example


How can we configure these modems for UNIX without putting the ConfigureForUnix method in the Mdem interface?
<<interface>> Modem +dial +hangup +send +receive

Hayes

Zoom

Ernie

Object Oriented Analysis and Design

102

Visitor Pattern Modem example


How can we configure these modems for UNIX without putting the ConfigureForUnix method in the Mdem interface?
<<Interface>> ModemVisitor <<interface>> Modem +dial +hangup +send +receive +visit(Hayes) +visit(Zoom) +visit(Ernie)

Hayes

Zoom

Ernie

UnixModemConfigura tor

Public void accept (ModemVisitor v) { v.visit(this) }


Object Oriented Analysis and Design
103

Visitor Pattern Modem example


Client Code: v = new UnixModemConfigurator(); h = new HayesModem(); h.accept(v); z = new ZoomModem(); z.accept(v); e = new ErnieModem(); e.accept(); public interface Modem { public void dial(String pno); public void hangup(); public void send(char c); public char recv(); public void accept (ModemVisitor v); }; public interface ModemVisitor { public void visit (HayesModem modem); public void visit (ZoomModem modem); public void visit (ErnieModem modem); }; public class HayesModem implements Modem { public void dial(String pno){}; public void hangup(){}; public void send(char c){}; public char recv(){return 0}; public void accept (ModemVisit v) {v.visit(this); String configurationString =null; }; public class ZoomModem implements Modem { public void dial(String pno){}; public void hangup(){}; public void send(char c){}; public char recv(){return 0}; public void accept (ModemVisit v) {v.visit(this); int configurationValue = 0; };
Object Oriented Analysis and Design
104

Visitor Pattern Modem example


public class ErnieModem implements Modem { public void dial(String pno){}; public void hangup(){}; public void send(char c){}; public char recv(){return 0}; public void accept (ModemVisit v) {v.visit(this); String internalPattern = null; };
public UnixModemConfigurator implements ModemVisitor { public void visit (HayesModem m) {m.configrationString = $s1=4$D=3 }; public void visit (ZoomModem m) {m.configrationValue = 42 }; public void visit (ErnieModem m) {m.internalPattern =c is too slow }} };

Object Oriented Analysis and Design

105

Visitor Pattern A cyclic Visitor Modem example


There is a cycle of dependencies that ties all the visited derivatives (all the Modems) together.
public interface ModemVisitor {} Public void accept (ModemVisitor v){ try { HayesVisitor hv = (HayesVisitor) v; hv.visit(this); } catch (ClassCastException) {} }

<<interface>> Modem +dial +hangup +send +receive

<<degenerate>> ModemVisitor

UnixModemConfigura tor

Hayes <<interface>> HayesVisitor


+visit(Hayes)
Object Oriented Analysis and Design

Zoom

Ernie

<<interface>> ZoomVisitor
+visit(Zoom)
106

<<interface>> ErnieVisitor
+visit(Ernie)

Visitor Pattern - Motivation


abstract syntax trees
Consider a compiler that represents programs as abstract syntax trees. It will need to perform operations on abstract syntax trees for "static semantic" analyses like checking that all variables are defined. It will also need to generate code. Problem distributing all these operations across the various node classes leads to a system that's hard to understand, maintain, and change. It would be better if each new operation could be added separately, and the node classes were independent of the operations that apply to them. Solution packaging related operations from each class in a separate object, called a visitor, and passing it to elements of the abstract syntax tree as it's traversed. When an element "accepts" the visitor, it sends a request to the visitor that encodes the element's class. It also includes the element as an argument. The visitor will then execute the operation for that elementthe operation that used to be in the class of the element.
107

Object Oriented Analysis and Design

Visitor Pattern - Motivation

Object Oriented Analysis and Design

108

Visitor Pattern - Example


Taxi Company
When a person calls a taxi company (accepting a visitor), the company dispatches a cab to the customer. Upon entering the taxi the customer, or Visitor, is no longer in control of his or her own transportation, the taxi (driver) is.

Object Oriented Analysis and Design

109

Visitor Pattern - Example


Iterator iterator = collection.iterator() while (iterator.hasNext()) { Object o = iterator.next(); if (o instanceof Collection) messyPrintCollection((Collection)o); else if (o instanceof String)
System.out.println("'"+o.toString()+"'"); else if (o instanceof Float) System.out.println(o.toString()+"f"); else System.out.println(o.toString()); }
Object Oriented Analysis and Design
110

Visitor Pattern - Example


Visitor pattern

Object Oriented Analysis and Design

111

Visitor Pattern
Intent
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Structure
A client that uses the Visitor pattern must create a ConcreteVisitor object and then traverse the object structure, visiting each element with the visitor. When an element is visited, it calls the Visitor operation that corresponds to its class. The element supplies itself as an argument to this operation to let the visitor access its state, if necessary.

Object Oriented Analysis and Design

112

Visitor Pattern - Structure

Object Oriented Analysis and Design

113

Visitor Pattern - Example


public interface Element { public void accept(Visitor visitor); }
public class StringElement implements Element { private String value;
public StringElement(String string) {value = string;} public String getValue(){return value;}

public void accept(Visitor visitor) { visitor.visitString(this); } }

Object Oriented Analysis and Design

114

Visitor Pattern - Example


public class FloatElement implements Element { private Float value; public FloatElement(Float value) { this.value = value; } public Float getValue(){ return value; } public void accept(Visitor visitor) { visitor.visitFloat(this); } }

Object Oriented Analysis and Design

115

Visitor Pattern - Example


public interface Visitor { public void visitString(StringElement stringE); public void visitFloat(FloatElement floatE); public void visitCollection(Collection collection); }

Object Oriented Analysis and Design

116

Visitor Pattern - Example


public class ConcreteVisitor implements Visitor { public void visitCollection(Collection collection) { Iterator iterator = collection.iterator() while (iterator.hasNext()) { Object o = iterator.next(); if (o instanceof Element) ((Element)o).accept(this); }

Object Oriented Analysis and Design

117

Visitor Pattern - Example


public void visitString(StringElement stringE) { System.out.println("'"+stringE.getValue()+"'"); } public void visitFloat(FloatElement floatE){
System.out.println(floatE.getValue().toString()+"f");

Object Oriented Analysis and Design

118

Visitor Pattern - Example


Visitor visitor = new ConcreteVisitor();
StringElement stringE = new StringElement("I am a String"); visitor.visitString(stringE); Collection list = new ArrayList(); list.add(new StringElement("I am a String1")); list.add(new StringElement("I am a String2")); list.add(new FloatElement(new Float(12))); list.add(new StringElement("I am a String3")); visitor.visitCollection(list);

Object Oriented Analysis and Design

119

Template Method Pattern Motivation


Consider an application framework that provides Application and Document classes.
Applications built with the framework can subclass Application and Document to suit specific needs. The abstract Application class defines the algorithm for opening and reading a document in its OpenDocument operation:

Object Oriented Analysis and Design

120

Template Method Pattern Motivation


The abstract Application class defines the algorithm for opening and reading a document in its OpenDocument operation:
void Application::OpenDocument (const char* name) { if (!CanOpenDocument(name)) { // cannot handle this document return; } Document* doc = DoCreateDocument(); if (doc) { _docs->AddDocument(doc); AboutToOpenDocument(doc); doc->Open(); doc->DoRead(); } }

OpenDocument defines each step for opening a document. We call OpenDocument a template method. A template method defines an algorithm in terms of abstract operations that subclasses override to provide concrete behavior.
Object Oriented Analysis and Design
121

Template Method Pattern Example


Consider following main-loop structure
Initialize(); While (!done()) { //main loop Idle(); //do something useful } Cleanup();

Ftocraw.java is a example program


public class ftocraw { public static void main(String[] args) throws Exception { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); boolean done = false; while (!done) { String fahrString = br.readLine(); if (fahrString == null || fahrString.length() == 0) done = true; else { double fahr = Double.parseDouble(fahrString); double celcius = 5.0/9.0*(fahr-32); System.out.println("F=" + fahr + ", C=" + celcius); } } System.out.println("ftoc exit"); } Object Oriented 122 } Analysis and Design

Template Method Pattern Example


We can separate this main-loop structure from ftoc program by employing the Template Method pattern
public abstract class Application { private boolean isDone = false; protected abstract void init(); protected abstract void idle(); protected abstract void cleanup(); protected void setDone() {isDone = true;} protected boolean done() {return isDone;} public void run() { init(); while (!done()) idle(); cleanup(); } }
Object Oriented Analysis and Design
123

Template Method Pattern Example

public class ftocTemplateMethod extends Application { private InputStreamReader isr; private BufferedReader br;

We can rewrite the ftoc class by inheriting from Application and just filling in the abstract methods

public static void main(String[] args) throws Exception { (new ftocTemplateMethod()).run(); } protected void init() {isr = new InputStreamReader(System.in); br = new BufferedReader(isr); } protected void idle() { String fahrString = readLineAndReturnNullIfError(); if (fahrString == null || fahrString.length() == 0) setDone(); else {double fahr = Double.parseDouble(fahrString); double celcius = 5.0/9.0*(fahr-32); System.out.println("F=" + fahr + ", C=" + celcius);} }

protected void cleanup() { System.out.println("ftoc exit"); }


private String readLineAndReturnNullIfError() { String s; try { s = br.readLine(); } catch(IOException e) { s = null; } return s; }
Object Oriented Analysis and Design

124

Template Method Pattern


Intent Define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. Structure ConcreteClass relies on AbstractClass to implement the invariant steps of the algorithm.

Object Oriented Analysis and Design

125

Template Method Pattern - example


The Template Method defines a skeleton of an algorithm in an operation, and defers some steps to subclasses. Home builders use the Template Method when developing a new subdivision.
A typical subdivision consists of a limited number of floor plans with different variations available for each. Within a floor plan, the foundation, framing, plumbing, and wiring will be identical for each house. Variation is introduced in the later stages of construction to produce a wider variety of models.

Object Oriented Analysis and Design

126

Strategy Pattern Motivation


Many algorithms exist for breaking a stream of text into lines. Hard-wiring all such algorithms into the classes that require them isn't desirable for several reasons:
Clients that need linebreaking get more complex if they include the linebreaking code. That makes clients bigger and harder to maintain, especially if they support multiple linebreaking algorithms. Different algorithms will be appropriate at different times. We don't want to support multiple linebreaking algorithms if we don't use them all. It's difficult to add new algorithms and vary existing ones when linebreaking is an integral part of a client.

We can avoid these problems by defining classes that encapsulate different linebreaking algorithms.

An algorithm that's encapsulated in this way is called a strategy.

Object Oriented Analysis and Design

127

Strategy Pattern Example


Consider following main-loop structure
Initialize(); While (!done()) { //main loop Idle(); //do something useful } Cleanup();

Ftocraw.java is a example program


public class ftocraw { public static void main(String[] args) throws Exception { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); boolean done = false; while (!done) { String fahrString = br.readLine(); if (fahrString == null || fahrString.length() == 0) done = true; else { double fahr = Double.parseDouble(fahrString); double celcius = 5.0/9.0*(fahr-32); System.out.println("F=" + fahr + ", C=" + celcius); } } System.out.println("ftoc exit"); } }
Object Oriented Analysis and Design
128

Strategy Pattern Example


We place the generic application algorithm into a concrete class named ApplicationRunner
public interface Application { public void init(); public void idle(); public void cleanup(); public boolean done(); }
<<interface>>

ApplicationR unner
+run

Application
+init +idle +cleanup +done : boolean

public class ApplicationRunner { private Application itsApplication = null; public ApplicationRunner(Application app){ itsApplication = app; } public void run() { itsApplication.init(); while (!itsApplication.done()) itsApplication.idle(); itsApplication.cleanup(); } Object Oriented Analysis and Design 129 }

focStrategy

Strategy Pattern Example


public class ftocStrategy implements Application { private InputStreamReader isr; private BufferedReader br; private boolean isDone = false; public static void main(String[] args) throws Exception { (new ApplicationRunner(new ftocStrategy())).run(); } public void init() { isr = new InputStreamReader(System.in); br = new BufferedReader(isr); } public void idle() { String fahrString = readLineAndReturnNullIfError(); if (fahrString == null || fahrString.length() == 0) isDone = true; else { double fahr = Double.parseDouble(fahrString); double celcius = 5.0/9.0*(fahr-32); System.out.println("F=" + fahr + ", C=" + celcius); } } public void cleanup() {System.out.println("ftoc exit"); } public boolean done() { return isDone; } private String readLineAndReturnNullIfError() { String s; try {s = br.readLine(); } catch(IOException e) { s = null; } return s; }
Object Oriented Analysis and Design

130

Strategy Pattern - Structure


Intent Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it. Structure

Object Oriented Analysis and Design

131

Strategy Pattern - example


A Strategy defines a set of algorithms that can be used interchangeably. Modes of transportation to an airport is an example of a Strategy.
Several options exist such as driving one's own car, taking a taxi, an airport shuttle, a city bus, or a limousine service. For some airports, subways and helicopters are also available as a mode of transportation to the airport. Any of these modes of transportation will get a traveler to the airport, and they can be used interchangeably. The traveler must chose the Strategy based on tradeoffs between cost, convenience, and tlme.

Object Oriented Analysis and Design

132

Strategy Pattern Example


In this inflexible example, all the NumberCruncher code is in one big class Why is this bad?

Strategy is similar to Bridge; same basic structure; very different intent. The Strategy pattern is also similar to State, which allows a class to be configured with different behaviors from which it can select whenever it makes an interesting state transition.
Object Oriented Analysis and Design
133

Strategy Pattern Example


What if there were not a CrunchAlgorithm interface suppose instead that NumberCruncher had two subclasses, CorrectButSlowNumberCruncher, and FastButSloppyNumberCruncher? Why is this bad?

Object Oriented Analysis and Design

134

Strategy Pattern Example


Heres another correct design... But there can be no polymorphism in the chooseAlgorithm() or implCode() methods, leading to maintenance difficulties. Adding a NewAndImprovedCrunch would require adding if-then-else logic everywhere that the different Crunches are used. If the Strategy pattern were applied instead, the only place where the concrete CrunchImpls would get referred to specifically is the one place that they get instantiated.

Object Oriented Analysis and Design

135

Strategy Pattern Example


Intent: Allows multiple implementation strategies to be interchangeable, so that they can easily be swapped at run-time, and so that new strategies can be easily added.

In this example, notice that clients of NumberCruncher do not know about the different crunch algorithms. The NumberCruncher.crunch() method is free to decide which CrunchImpl to use at any time; new algorithms can be easily added.
Object Oriented Analysis and Design
136

Applying a Strategy Pattern in a Database Application

Object Oriented Analysis and Design

137

Applicability of Strategy Pattern


Applicability of Strategy Pattern 1) Many related classes differ only in their behavior. Strategy allows to configure a single class with one of many behaviors 2) Different variants of an algorithm are needed that trade-off space against time. All these variants can be implemented as a class hierarchy of algorithms

Object Oriented Analysis and Design

138

State Pattern Motivation


Consider a class TCPConnection that represents a network connection.
A TCPConnection object can be in one of several different states: Established, Listening, Closed. When a TCPConnection object receives requests from other objects, it responds differently depending on its current state. The State pattern describes how TCPConnection can exhibit different behavior in each state.

The key idea is to introduce an abstract class called TCPState to represent the states of the network connection.
The TCPState class declares an interface common to all classes that represent different operational states. Subclasses of TCPState implement state-specific behavior.

Object Oriented Analysis and Design

139

State Pattern Example


Consider the Finite State Machine of Turnstile
pass / alarm Locked

coin / unlock pass / lock Unlocked coin / thankyou

Ftocraw.java is a example program

Object Oriented Analysis and Design

140

State Pattern Example


The structure of the solution
Turnstile
+coin() +pass() #lock() #unlock() #thankyou() #alarm()

<<interface>>

TurnstileState
+coin (Turnstile) +pass (Turnstile)

interface TurnstyleState { void coin(Turnstyle t); void pass(Turnstyle t); }


class LockedTurnstyleState implements TurnstyleState { public void coin(Turnstyle t) { t.setUnlocked(); t.unlock(); } public void pass(Turnstyle t) { t.alarm(); } }

Turnstile LockedSt ate

TurnstileU nlockedSt ate

The State Pattern to the Turnstile

Object Oriented Analysis and Design

141

class UnlockedTurnstyleState implements TurnstyleState { public void coin(Turnstyle t) { t.thankyou(); } public void pass(Turnstyle t) { t.setLocked(); t.lock(); } }

State Pattern Example


public class Turnstyle { private static TurnstyleState lockedState = new LockedTurnstyleState(); private static TurnstyleState unlockedState = new UnlockedTurnstyleState(); private TurnstyleController turnstyleController; private TurnstyleState state = lockedState; public Turnstyle(TurnstyleController action) { turnstyleController = action;} public void coin() { state.coin(this);} public void pass() {state.pass(this);} public void setLocked() {state = lockedState;} public void setUnlocked() {state = unlockedState; } public boolean isLocked() {return state == lockedState; } public boolean isUnlocked() { return state == unlockedState; } void thankyou() { turnstyleController.thankyou(); } void alarm() {turnstyleController.alarm();} void lock() { turnstyleController.lock();} void unlock() { turnstyleController.unlock(); } } public interface TurnstyleController { public void lock(); public void unlock(); public void thankyou(); public void alarm(); }
Object Oriented Analysis and Design
142

State Pattern - Structure


Intent Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

Structure

Object Oriented Analysis and Design

143

State Pattern - example


The State pattern allows an object to change its behavior when its internal state changes. This pattern can be observed in a vending machine.
Vending machines have states based on the inventory, amount of currency deposited, the ability to make change, the item selected, etc. When currency is deposited and a selection is made, a vending machine will either deliver a product and no change, deliver a product and change, deliver no product due to insufficient currency on deposit, or deliver no product due to inventory depletion.

Object Oriented Analysis and Design

144

State Pattern Example


Intent: Allow an object to alter its behavior when its internal state changes. The object will appear to change its class, from the point of view of the client.

Object Oriented Analysis and Design

145

State Pattern Example


How does a ConcreteState know what state to go to on a transition? Each class can have its own table or switch statement, or a hash table of transitions keyed by their triggers. Consider using Event and Transition classes. Note: The Event class might be implemented using the Command pattern.

Object Oriented Analysis and Design

146

Iterator Pattern Motivation


An aggregate object such as a list should give you a way to access its elements without exposing its internal structure. The key idea is to take the responsibility for access and traversal out of the list object and put it into an iterator object.

Object Oriented Analysis and Design

147

Iterator Pattern Example


Consider a type-safe container
package java.util; public interface Iterator { boolean hasNext(); Object next(); void remove(); }

import java.util.*; public class TypedIterator implements Iterator { private Iterator imp; private Class type; public TypedIterator(Iterator it, Class type) { imp = it; this.type = type;} public boolean hasNext(){return imp.hasNext();} public void remove() { imp.remove(); } public Object next() { Object obj = imp.next(); if(!type.isInstance(obj)) throw new ClassCastException( "TypedIterator for type " + type + " encountered type: " + obj.getClass()); return obj; } }

Object Oriented Analysis and Design

148

Iterator Pattern - Structure


Intent
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Structure

Object Oriented Analysis and Design

149

Memento Pattern Motivation


Sometimes it's necessary to record the internal state of an object.
This is required when implementing checkpoints and undo mechanisms

Exposing this state would violate encapsulation

A memento is an object that stores a snapshot of the internal state of another objectthe memento's originator.
The undo mechanism will request a memento from the originator when it needs to checkpoint the originator's state. The originator initializes the memento with information that characterizes its current state. Only the originator can store and retrieve information from the mementothe memento is "opaque" to other objects.

Object Oriented Analysis and Design

150

Memento Pattern - Structure


Intent Without violating encapsulation, capture and externalize an object's internal state so that the object can be returned to this state later. Structure

Object Oriented Analysis and Design

151

Memento Pattern Example


public class Originator { private int number; private File file = null; public Originator(){} public Memento getMemento() { return new Memento(this);} public void setMemento(Memento m){number = m.number; file = m.file;} }

private class Memento implements java.io.Serializable{ private int number; private File file = null; public Memento( Originator o){ number = o.number; file = o.file;} }

Object Oriented Analysis and Design

152

Memento
Intent: Save an objects state without violating the principle of encapsulation. Applicability: The state of an object must be saved (by a client) so that it can be restored later. The Memento object contains all the necessary state information. l This is another way to implement undo. l Example: Java Beans save their state to a .ser file after being configured. l How is it possible, in Java & C++, for methods & data in the class Memento to be available to SomeClass, but not to Clients?

Object Oriented Analysis and Design

153

Mediator Pattern Motivation


How the objects cooperate to handle a change in a list box's selection Encapsulating collective behavior in a separate mediator object

Object Oriented Analysis and Design

154

Mediator Pattern Motivation

Object Oriented Analysis and Design

155

Mediator Pattern - Structure


Intent
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

Structure
Colleagues send and receive requests from a Mediator object. The mediator implements the cooperative behavior by routing requests between the appropriate colleague(s).

Object Oriented Analysis and Design

156

6.4 Applying Design Patterns


MVC pattern Wmvc Framework MovieCat Application using Wmvc Thermometer Application using Wmvc

Object Oriented Analysis and Design

157

You might also like