design patterns illustrated
DESCRIPTION
Illustrations to show the essence of the 23 "classical" Gang Of Four design patterns. Plus some general info about object oriented progrTRANSCRIPT
Design Patterns illustrated
Herman Peeren May 31st 2010(DP-illustrations: Nelleke Verhoeff)
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!
The one constant in software development:
CHANGE!The one constant in software development:
CHANGE!The one constant in software development:
I knew it ...
Ideal: code as modular black boxes
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)
Avoid: tight coupling!
It might get you into trouble...
Code smells!
Beware of:
some code smells:1.● duplicate code: DRY2.● long method, huge class: SRP3.● combinatorial explosion (similar classes)4.● conditional complexity, switch statements5.● “exhibitionistic” classes
some code smells:1.● duplicate code: DRY2.● long method, huge class: SRP3.● combinatorial explosion (similar classes)4.● conditional complexity, switch statements5.● “exhibitionistic” classes
Classic pattern categories
creational, structural and behavioral patterns:
●● creational: object instantiation●● structural: larger structures of classes or objects ●● behavioral: interaction and distribution of responsibility
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.
Factory MethodProvide an interface for the creation of objects.Allow subclasses to “decide” which class to instantiate.
c
Abstract FactoryPovide an interface for creating families of related or dependent objects. A factory for factories.
c
BuilderSeperate the construction process (how) of a complex object from the concrete representations (what).
c
SingletonEnsure a class only has one instance, and provide a global point of access to it.
Oh, I’m soloooooooonly
c
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
“Every advantage
has its disadvantages”
(free to Johan Cruyff, Dutch Football Pattern Designer and Ajax-fan...)
PrototypeMake variations on copies of a basic-object.
COPY-SERVICE
c
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.
Adapter (= Wrapper)Adapt an interface to an expected interface.
c
Joomla! ●● new in 1.6: JAdapter and JAdapterInstance
●● JUpdateAdapter extends JAdapterInstance JUpdaterExtension & JUpdaterExtension extend JUpdateAdapter
wanted: documentation and examples!what does it adapt?
BridgeDecouple an abstraction from its implementation.
c
COLA
COLA
COLA
COLA
1 LITER
1 LITER
COLA
MILK
MILK
1 LITER
1 LITER
MILK
CompositeCreate a tree structure for part-whole hierarchies. A node is also a (part of a) tree. Recursive:
c
DecoratorAdd extra functionallity (at runtime), while keeping the interface the same. Matroushka’s...
c
Nooku●● KPatternDecorator: a general decorator●● e.g. extended by KDecoratorJoomlaApplication
Decorator
FacadeProvide a general (simpler) interface for a set of interfaces.
lookssimple
c
FlyweightUse one instance of a class to provide many “virtual” instances.
c
ProxyProvide a surrogate or placeholder for another object to control access to it.
c
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.
CommandEncapsulate a command request in an object.
c
YOU,DO YOURTASK!
LIGHTOFF
LIGHTON
TASKTASK
Chain of ResponsibilityDefine a method of passing a request among a chain of objects.
c
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....
InterpreterDomain -> (little) language -> grammar -> objects(DSL)
he means:do this, do that,
and after finishing it, go there!
HÉ!HÉ!
c
IteratorEnable sequential access to collection elements, without showing the underlying data-structures (array, list, records, etc)
nextnext
next
c
MediatorLayer in between: communication via one object.
c
MementoSave and restore the internal state of an object.
ME
c
ObserverNotify “subscribers” of changes.
WHO?
ME
ME
MENO
c
Joomla!●● JObserver and JObservable●● JObserver extended by JEvent and that by JPlugin
Nooku●● KPatternObserver and KPatternObservable
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
StrategyWhen something can be done in several ways, make those ways interchangeable.
POSSI-BILITIES
c
Template MethodThe skeleton of an algorithm is fixed, but parts can be filled in differently.
c
VisitorMake a kind of plugin-possibility for methods: in that way me-thods can be added in runtime.
printservice!
c
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....
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
Some booksGOF: 23 “classical” patterns:
classic, The Book
very nice!
handy examples
good start
Fowler: extended e.g. with patterns for web
Fowler: also known from refactoring
combi: re-factoring & patterns
Resign Patterns: Ailments of Unsuitable Project-Disoriented Software
Joomla!Could be improved by studying design patterns and applying them.
Loose coupling...
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