69
Thinking in Patterns with Java Bruce Eckel http://www.mindview.net/Books/TIPatterns/

Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Thinking in Patterns with JavaBruce Eckel

http://www.mindview.net/Books/TIPatterns/

Page 2: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Design Patterns Classification

• Creational Design Patterns

• Structural Design Patterns

• Behavioral Design Patterns

Page 3: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Creational Design Patterns

• Singleton

• Factory Method

• Factory Pattern

• Abstract Factory

• Builder

• Reusable Pool

• Prototype

Page 4: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Singleton (creational)

Class Singleton {

private static Singleton instance = null;

private Singleton() {}; //private constructor

public static Singleton getInstance() {

if (instance == null) {

instance = new Singleton();

}

return instance;

}

}

Page 5: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Factory method (creational)

Class Singleton {

private static Singleton instance = null;

private Singleton() {}; //private constructor

public static Singleton getInstance() {

if (instance == null) {

instance = new Singleton();

}

return instance;

}

}

Page 6: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Abstract Factory Pattern (creational)

Factory<<abstract nebo interface>>

Product<<abstract nebo interface>>

Client

ConcreteFactory

public Product createProduct()

ConcreteProduct

Specialised classjust to create an

instance ofProduct

createProduct() { return new ConcreteProduct();}

Page 7: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Abstract Factory Pattern (creational)

ClientClass

WidgetFactory<<abstract nebo interface>>

+ createWindow()

+ createScrollBar()

MotifFactory

+ createWindow()

+ createScrollBar()MetalFactory

+ createWindow()

+ createScrollBar()

Window<<abstract nebo interface>>

MetalWindowMotifWindow

ScrollBar<<abstract nebo interface>>

MetalScrollBarMotifScrollbar

Page 8: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Builder (creational)

AbstractBuilder

<<abstract or interface>>

+ buildStep1()+ buildStep2()+ buildStep3()+ getObject()

Director

+ construct()

construct() {builder.buidlStep1();builder.buildStep2();buidler.buildStep3();getObject();

}

+ builder

ConcreteBuilder1 ConcreteBuilder2

Page 9: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Prototype (creational)

AbstractPrototype

<<abstract or interface>>

+ clone()

clone() { // return copy of self}

use clone to instantiate

ConcretePrototype1 ConcretePrototype2

Client

clone() { // return copy of self}

Page 10: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Client

ReusablePool<<singleton>>

+ acquireReusable(): Reusable

+ releaseReusable(): Reusable

Reusable

use

s

1

0..*

+ pool

0..*11

*

Reusable Pool (creational)

Page 11: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Structural Design Patterns

• Adapter

• Bridge (sometimes considered to be a behavioral DP)

• Decorator

• Composite

• Facade

• Proxy

• and more

Page 12: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Adapter (structural)

Client classVendor

class

Page 13: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Adapter (structural)

Client classVendor

classAdapter

Page 14: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Adapter (structural)

AdapterClient classVendor

class

Page 15: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Adapter (structural)

Client

Target<<interface>>

request()

Adapter

request()

Adaptee

specificRequest()

Page 16: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

The Dependency Inversion Principle

• High level modules should not depend upon low level

modules; both should depend on abstractions.

• Abstractions should not depend upon details;details should depend upon abstractions.

KávaSMlékemKávaSMlékem

ACukremKávaSCukrem

KávovarAEG

Page 17: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Dependency inversion principlespecific form of decoupling where conventional dependency relationships established from high-levelare inverted (e.g. reversed) for the purpose of rendering high-level modules independent of the low-level module implementation details. The principle states:

• High-level modules should not depend on low-level modules. Both should depend on abstractions.

• Abstractions should not depend upon details. Details should depend upon abstractions.

(From Wikipedia, the free encyclopedia)

Page 18: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

The Dependency Inversion Principle

KávaSMlékemKávaSMlékem

ACukremKávaSCukrem

KávovarAEG

<<interface>>Kávovar

KávaSMlékemKávaSMlékem

ACukremKávaSCukrem

<<interface>>Káva

0..*

Page 19: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

The Dependency Inversion Principle

<<interface>>Kávovar

KávaSMlékemKávaSMlékem

ACukremKávaSCukrem

<<interface>>Káva

KávovarETA KávovarAEG KávovarBosh

0..*

Jednoduché rozšíření předešlého diagramu

� design Pattern Bridge

Page 20: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

BridgeImplementation

Abstraction refinement

• Máme více hledisek klasifikace.

• Pokud bychom je chtěli zohlednit v jedné

inheritanční hierarchii, došlo by k explozi počtu

tříd (pro různé kombinace).

• Řešení – oddělit klasifikační hlediska.

Page 21: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Bridge

Bridge

imp

+ drawLine(): void

Page 22: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Bridge

Bridge

imp

imp.devDrawLine();

imp.devDrawLine();

imp.devDrawLine();

imp.devDrawLine();

DrawRect();

DrawText(); XDrawLine();XDrawString();

+ drawLine(): void

Page 23: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Bridge (structural)

AbstractUser

<<abstract or interface>>

+ operation()

AbstractProvider

<<interface>>

+ oper1()+ oper2()

Provider1

+ oper1()+ oper2()

Provider2

+ oper1()+ oper2()

User1

+ operation()

User2

+ operation()

operation() {provider.oper2();

}

provider

operation() {provider.oper1();

}

Page 24: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Bridge (structural)

1. The Bridge pattern is intended to keep the interface to your client class constant while allowing you to change the actual kind of class you use.

2. You can extend the implementation class and the bridge class separately, and usually without much interaction with each other (avoiding permanent binding between abstraction and its implementation).

3. You can hide implementation details from the client class much more easily.

Page 25: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

DecoratorKávovar

KávaSMlékemKávaSMlékem

ACukremKávaSCukrem

<<abstract nebo interface>>Káva

Possible class explosion(hard to maintain)

Decorator patterns helps ...

Page 26: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

The Open-Close Design Principle

Software entities like classes, modules and functions should be

open for extension but closed for modifications.

Adding new functionality should involve minimal changes to

existing code.

=> adding new functionality will not break the oldfunctionality

=> old functionality need not be re-tested

Most changes will be handled as new methods and new classes. Designs following this principle would result in resilient code which does not break on addition of new functionality.

Page 27: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Decorator (structural)

Káva

Page 28: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Decorator (structural)

KávaS cukrem

Page 29: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Decorator (structural)

KávaS cukremS mlékem

Page 30: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

S cukrem

Decorator (structural)

KávaS cukremS mlékem

Page 31: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Decorator (structural)

Component<< abstract >>

methodA()methodB()

// other methods

ConcreteComponent

methodA()methodB()

// other methods

Decorator<< abstract >>

methodA()methodB()

// other methods

ConcreteDecoratorA

methodA()methodB()

// other methods

ConcreteDecoratorB

methodA()methodB()

// other methods

0..1

1ClientClass

Page 32: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Decorator (structural)

Component<< abstract >>

methodA()methodB()

// other methods

ConcreteComponent

methodA()methodB()

// other methods

Decorator<< abstract >>

methodA()methodB()

// other methods

ConcreteDecoratorA

methodA()methodB()

// other methods

ConcreteDecoratorB

methodA()methodB()

// other methods

0..1

1ClientClass

Káva

S cukrem

S mlékem

Page 33: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Decorator (structural)

Kava kava = new Kava();

Kava kavaSMlekem = new SMlekem(kava);

Kava kavaSCukremSMlekem = new SCukrem(kavaSMlekem);

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("fff"));

Page 34: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Decorator (structural)

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("fff"));

InputStream

FileInputStream BufferedInputStream

Page 35: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Decorator (structural)

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("fff"));

InputStream

FileInputStream

BufferedInputStream

Page 36: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Decorator (structural)

The Decorator Pattern attaches additional functionalities

to an object dynamically.

Decorators provide a flexible alternative to subclassingfor extending functionality.

Decorator prevents explosion of (sub)classes.

Page 37: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Composite (structural)

graphic

Abstract nebo interface

Vrací typ Graphic

Page 38: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Composite (structural)

graphic

Forall g in graphic

g.draw();

Add g to the graphic

list

Page 39: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Composite (structural)

Page 40: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Composite (structural)

Forwards the

operation to

all children

Default empty

implementation

Views all components

uniformly regardless

whether leaf or

composite

Page 41: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Façade (structural)

Client Facade

A

B

CD

EF

A complex subsystem

The façade provides a unified

interface that is easier to use

Page 42: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Façade (structural)

The Façade Pattern provides a unified interface to a set

of interfaces in a subsystem. Façade defines a higher-levelinterface that makes the system easier to use.

Design PrinciplePrinciple of least knowledge -talk only to your immediate

friends.

Client Facade

A

B

CD

EF

Page 43: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Proxy (structural)

The Proxy Pattern provides a surrogate or placeholder

for another object to access control to it.

RealSubject

Client Proxy

request()request()

Page 44: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Proxy (structural)

With Remote Proxy, the proxy act as a local

representative for an object that lives in a different JVM.

RealSubject

Client Proxy

request()request()

A remote object

Page 45: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Proxy (structural)

Virtual Proxy acts as a representative for an object that

may be expensive to create. The virtual proxy often defers creation of the object until it is needed. After that,

the virtual proxy delegates requests to the RealSubject.

RealSubject

Client Proxy

request()

request()

Page 46: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Proxy (structural)

<<interface>>Subject

request()

RealSubject

request()

Proxy

request()

subject

The Proxy ofteninstantiates or handles

creation of theRealSubject.

The RealSubject doesmost of the work. The

Proxy controls accessto it.

Page 47: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Flyweight (structural)

• Intrinsic state is stored in the ConcreteFlyweight object

• Extrinsic state is stored or computed by Client objects. Clients

pass this state to the flyweight when they invoke they operations

• Clients should not instantiate ConcreteFlyweights directly.

Clients must obtain ConcreteFlyweights from the

FlyweightFactory to ensure they are shared properly.

• not all Flyweight objects need to be shared. It is common for

UnsharedConcreteFlyweight objects to have

ConcreteFlyweight objects as children.

Page 48: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Flyweight (structural)

Pool of

flyweights

Page 49: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Flyweight (structural)

Flyweight

SharedFlyweight

UnsharedFlyweight UnsharedFlyweight

Page 50: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Behavioral Design Patterns

• Command

• Interpreter

• Iterator

• Mediator

• Observer

• State

• Strategy

• Visitor

Page 51: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Command (behavioral)

Command represents an abstract algorithm independent on:

1) The application of the command (Client)2) The particular implementation of the command (Receiver)

Page 52: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Command (behavioral)

Command encapsulates a request as an object,

thereby letting you parametrize other objects with different requests, queue or log requests and

support undoable operations.

Receiveraction()

Command

execute() {

receiver.action();

}

Page 53: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Command (behavioral)

Page 54: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Command (behavioral)

Page 55: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Command (behavioral)

• Command decouples the object that invokes the operation from

the one that knows how to perform it.

• You can assemble commands into a composite command

(composite commands are instances of Composite pattern)

• Easy to add new commands.

Page 56: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Behavioral Design Patterns

Behavioral Class Patterns

Behavioral Object Patterns

OtherBehavioral Patterns

Use inheritance to distribute behaviour between classes

• Template Method

• Interpreter

Use composition rather than inheritance to distribute behaviour between objects

• Mediator

• Chain of Responsibility

• Observer

• Strategy

• Command

• Visitor

• Iterator

Page 57: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Template Method

Page 58: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Strategy (behavioral)

<<interface>>Vehicle

MotionBehavior motion;EngineBehavior engine;

setMotion();setEngine();

<<interface>>MotionBehavior

move();

<<interface>>EngineBehavior

on();off()

Flying

move();

Riding

move();

ElectricDrive

on();off()

CombustionEngine

on();off()

JetPropulsion

on();off()

Page 59: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Strategy (behavioral)

The Strategy Pattern defines a family of algorithms,

encapsulates each one, and makes them interchangeable. Startegy lets the algorithm vary

independently from clients that use it.

Desing Principal

Favor composition over inheritance.

Page 60: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Observer (behavioral)

Subject

Observer1

Observer2

Observer3

notify()

notify()

notify()An object holding

a state and notifying

its observers about state’s

change.

The aim is to make subject independent on observers- loose coupling.

Page 61: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Observer (behavioral)

<<interface>>Observer

update()

<<interface>>Subject

registerObserver()removeObserver()notifyObserver()

ConcreteSubject

Object state;

registerObserver();removeObserver();notifyObserver();

getState();setState();

ConcreteObserver

update();

observers

subject

This association neededfor unsubscribing

(removeObserver())

Page 62: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Observer (behavioral)

Desing Principle

Loosely coupled designs allows us to build flexible OO

systems that can handle change because they minimize the interdependency between objects.

Page 63: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Mediator (behavioral)

• define an object that encapsulates how a set of objects

interacts

• mediator promotes loose coupling by keeping objects from

refering to each other explicitly

• it lets you vary their interactions independently

Mediator

Page 64: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Mediator (behavioral)

Page 65: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Mediator (behavioral)

Page 66: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

State (behavioral)

State Pattern allows an object to alter its behaviorwhen its internal state changes.

Context

state

request();

public void request() {state.handle();

}

<<interface>>State

handle();

StateB

handle();

StateA

handle();

Page 67: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Visitor (behavioral)

v.visitConcreteElementA

(this)

Page 68: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Visitor (behavioral)

Page 69: Thinking in Patterns with Java Bruce Eckelcw.felk.cvut.cz/.../a4m33nms/designpatternsen.pdf · Structural Design Patterns • Adapter • Bridge (sometimes considered to be a behavioral

Visitor (behavioral)

• Visitor makes adding new operations easy

simply by adding a new visitor

• A visitor gathers related operations and separates

unrelated ones

• Adding new ConcreteElement classes is hard

Is mostly likely to change the algorithm or the classes

of objects that make up the structure?

• Visitor can accumulate state as they visit each element