Design Patterns Illustrated

53
Design Patterns illustrated Herman Peeren May 31 st 2010 (DP-illustrations: Nelleke Verhoeff)

description

Illustrations to show the essence of the 23 "classical" Gang Of Four design patterns. Plus some general info about object oriented progr

Transcript of Design Patterns Illustrated

Page 1: Design Patterns Illustrated

Design Patterns illustrated

Herman Peeren May 31st 2010(DP-illustrations: Nelleke Verhoeff)

Page 2: Design Patterns Illustrated

Design Patterns●● recipes against common (OO-) programming problems●● code reuse: no need to reinvent the wheel●● common language●● GOF: 23 “classical” patterns

classic, The Book

very nice!

Page 3: Design Patterns Illustrated

The one constant in software development:

Page 4: Design Patterns Illustrated

CHANGE!The one constant in software development:

Page 5: Design Patterns Illustrated

CHANGE!The one constant in software development:

I knew it ...

Page 6: Design Patterns Illustrated

Ideal: code as modular black boxes

Page 7: Design Patterns Illustrated

Wish list and OOP-principles●● loose coupling: 1 change = ceteris paribus

●● code reuse (is not the same as copy/paste)

●● open for extension, closed for modification

●● encapsulate what varies

●● single responsibility principle

●● program against an interface not against an imple-mentation. Dependency injection

●● prefer composition over inheritance

(in fact this is all the same with different words)

Page 8: Design Patterns Illustrated

Avoid: tight coupling!

Page 9: Design Patterns Illustrated

It might get you into trouble...

Page 10: Design Patterns Illustrated

Code smells!

Beware of:

Page 11: Design Patterns Illustrated

some code smells:1.● duplicate code: DRY2.● long method, huge class: SRP3.● combinatorial explosion (similar classes)4.● conditional complexity, switch statements5.● “exhibitionistic” classes

Page 12: Design Patterns Illustrated

some code smells:1.● duplicate code: DRY2.● long method, huge class: SRP3.● combinatorial explosion (similar classes)4.● conditional complexity, switch statements5.● “exhibitionistic” classes

Page 13: Design Patterns Illustrated

Classic pattern categories

creational, structural and behavioral patterns:

●● creational: object instantiation●● structural: larger structures of classes or objects ●● behavioral: interaction and distribution of responsibility

Page 14: Design Patterns Illustrated

Creational design patterns●● Factory Method: Allow subclasses to “decide”

which class to instantiate.●● Abstract Factory: Encapsulate a set of analo-

gous factories that produce families of objects.●● Builder: Encapsulate the construction of com-

plex objects from their representation; so, the same building process can create various repre- sentations by specifying only type and content. ●● Singleton: Ensure that only a single instance of

a class exists and provide a single method for gaining access to it. ●● Prototype: Create an initialized instance for

cloning or copying.

Page 15: Design Patterns Illustrated

Factory MethodProvide an interface for the creation of objects.Allow subclasses to “decide” which class to instantiate.

c

Page 16: Design Patterns Illustrated

Abstract FactoryPovide an interface for creating families of related or dependent objects. A factory for factories.

c

Page 17: Design Patterns Illustrated

BuilderSeperate the construction process (how) of a complex object from the concrete representations (what).

c

Page 18: Design Patterns Illustrated

SingletonEnsure a class only has one instance, and provide a global point of access to it.

Oh, I’m soloooooooonly

c

Page 19: Design Patterns Illustrated

Joomla!●● JFactory: a class with static methods to instantiate objects like JDatabase, JUser, JDocument, JTemplate, etc.

●● most of those methods are singletons

Nooku●● KFactory: any class can be instantiated●● get() = singleton, tmp() = any instantiation

Page 20: Design Patterns Illustrated

“Every advantage

has its disadvantages”

(free to Johan Cruyff, Dutch Football Pattern Designer and Ajax-fan...)

Page 21: Design Patterns Illustrated

PrototypeMake variations on copies of a basic-object.

COPY-SERVICE

c

Page 22: Design Patterns Illustrated

Structural design patterns●● Adapter: Adapt an interface to an expected

interface.●● Bridge: Decouple an interface from its

implementation.●● Composite: Create a tree structure for

part-whole hierarchies.●● Decorator: Extend functionality dynamically.●● Facade: Simplify usage by defining a high-level

interface.●● Flyweight: Support fine-grained objects

efficiently by sharing.●● Proxy: Represent an object with another object

for access control.

Page 23: Design Patterns Illustrated

Adapter (= Wrapper)Adapt an interface to an expected interface.

c

Page 24: Design Patterns Illustrated

Joomla! ●● new in 1.6: JAdapter and JAdapterInstance

●● JUpdateAdapter extends JAdapterInstance JUpdaterExtension & JUpdaterExtension extend JUpdateAdapter

wanted: documentation and examples!what does it adapt?

Page 25: Design Patterns Illustrated

BridgeDecouple an abstraction from its implementation.

c

COLA

COLA

COLA

COLA

1 LITER

1 LITER

COLA

MILK

MILK

1 LITER

1 LITER

MILK

Page 26: Design Patterns Illustrated

CompositeCreate a tree structure for part-whole hierarchies. A node is also a (part of a) tree. Recursive:

c

Page 27: Design Patterns Illustrated

DecoratorAdd extra functionallity (at runtime), while keeping the interface the same. Matroushka’s...

c

Page 28: Design Patterns Illustrated

Nooku●● KPatternDecorator: a general decorator●● e.g. extended by KDecoratorJoomlaApplication

Decorator

Page 29: Design Patterns Illustrated

FacadeProvide a general (simpler) interface for a set of interfaces.

lookssimple

c

Page 30: Design Patterns Illustrated

FlyweightUse one instance of a class to provide many “virtual” instances.

c

Page 31: Design Patterns Illustrated

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

c

Page 32: Design Patterns Illustrated

Behavioral design patterns●● Chain of Responsibility: Define a method of passing a

request among a chain of objects.●● Command: Encapsulate a command request in an object.●● Interpreter: Allow inclusion of language elements in an appli-

cation.●● Iterator: Enable sequential access to collection elements.●● Mediator: Define simplified communication between classes.●● Memento: Save and restore the internal state of an object.●● Observer: Define a scheme for notifying objects of changes to

another object.●● State: Alter the behavior of an object when its state changes.●● Strategy: Encapsulate an algorithm inside a class.●● Template Method: Allow subclasses to redefine the steps of

an algorithm.●● Visitor: Define a new operation on a class without changing it.

Page 33: Design Patterns Illustrated

CommandEncapsulate a command request in an object.

c

YOU,DO YOURTASK!

LIGHTOFF

LIGHTON

TASKTASK

Page 34: Design Patterns Illustrated

Chain of ResponsibilityDefine a method of passing a request among a chain of objects.

c

Page 35: Design Patterns Illustrated

Nooku ●● KCommandChain+ KCommandContext, KCommandEvent, KCommandHandler and KCommandInterface

●● N.B.: executes a series of commands instead of passing a command to a series of handlers (like in Tapestry e.g.) ...correct me if I’m wrong....

Page 36: Design Patterns Illustrated

InterpreterDomain -> (little) language -> grammar -> objects(DSL)

he means:do this, do that,

and after finishing it, go there!

HÉ!HÉ!

c

Page 37: Design Patterns Illustrated

IteratorEnable sequential access to collection elements, without showing the underlying data-structures (array, list, records, etc)

nextnext

next

c

Page 38: Design Patterns Illustrated

MediatorLayer in between: communication via one object.

c

Page 39: Design Patterns Illustrated

MementoSave and restore the internal state of an object.

ME

c

Page 40: Design Patterns Illustrated

ObserverNotify “subscribers” of changes.

WHO?

ME

ME

MENO

c

Page 41: Design Patterns Illustrated

Joomla!●● JObserver and JObservable●● JObserver extended by JEvent and that by JPlugin

Nooku●● KPatternObserver and KPatternObservable

Page 42: Design Patterns Illustrated

StateLet an object show other methods after a change of internal state (as if it changes it’s class).

in a.....hick......different state, ....hick....I behave differently....hick.....

c

Page 43: Design Patterns Illustrated

StrategyWhen something can be done in several ways, make those ways interchangeable.

POSSI-BILITIES

c

Page 44: Design Patterns Illustrated
Page 45: Design Patterns Illustrated

Template MethodThe skeleton of an algorithm is fixed, but parts can be filled in differently.

c

Page 46: Design Patterns Illustrated

VisitorMake a kind of plugin-possibility for methods: in that way me-thods can be added in runtime.

printservice!

c

Page 47: Design Patterns Illustrated

extra: Mixin●● Used in Nooku (and a.o. in Ruby and Python)●● a kind of abstract class that is mixed into another object●● all methods of the mixin are now part of the object●● handle with care: beware of tight coupling....

Page 48: Design Patterns Illustrated

Golden OO-principles

●● encapsulate what varies, OCP

●● 1class, 1 responsibility, SRP

●● program against an interface, not against an imple-mentation, DIP

●● prefer composition over inheritance

●● loose coupling, modular black boxes

Page 49: Design Patterns Illustrated

Some booksGOF: 23 “classical” patterns:

classic, The Book

very nice!

handy examples

Page 50: Design Patterns Illustrated

good start

Fowler: extended e.g. with patterns for web

Fowler: also known from refactoring

combi: re-factoring & patterns

Page 51: Design Patterns Illustrated

Resign Patterns: Ailments of Unsuitable Project-Disoriented Software

Page 52: Design Patterns Illustrated

Joomla!Could be improved by studying design patterns and applying them.

Loose coupling...

Page 53: Design Patterns Illustrated

Questions?

Contact info:Herman [email protected]

© YeprThe artwork in this presentation is provided under the Creative Commons Public License for noncommercial use. http://creativecommons.org/licenses/by-nc/3.0/legalcode