designpatternsjoomla-100603035605-phpapp01
designpatternsjoomla-100603035605-phpapp01
illustrated
classic,
The Book
very nice!
The one constant in software development:
The one constant in software development:
CHANGE!
The one constant in software development:
CHANGE!
I knew it ...
Ideal: code as modular black boxes
Wish list and OOP-principles
●● loose coupling: 1 change = ceteris paribus
Code
smells!
some code smells:
1. duplicate code: DRY
2. long method, huge class: SRP
3. combinatorial explosion (similar classes)
4. conditional complexity, switch statements
5. “exhibitionistic” classes
some code smells:
1. duplicate code: DRY
2. long method, huge class: SRP
3. combinatorial explosion (similar classes)
4. conditional complexity, switch statements
5. “exhibitionistic” classes
Classic pattern categories
creational, structural and behavioral patterns:
Oh, I’m so
loooooooonly
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...)
Prototype
Make 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)
c Adapt an interface to an expected interface.
Joomla!
●● new in 1.6: JAdapter and JAdapterInstance
COLA
1 LITER 1 LITER
COLA
1 LITER 1 LITER
COLA
MILK
COLA
MILK COLA
MILK
c
Composite
Create a tree structure for part-whole hierarchies. A node is also a
(part of a) tree. Recursive:
c
Decorator
Add extra functionallity (at runtime),
while keeping the interface the same.
Matroushka’s...
c
Decorator
Nooku
●● KPatternDecorator: a general decorator
●● e.g. extended by KDecoratorJoomlaApplication
Facade
Provide a general (simpler) interface for a set of interfaces.
looks
simple
c
Flyweight
Use one instance of a class to provide many
“virtual” instances.
c
Proxy
Provide 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.
Command
Encapsulate a command request in an object.
YOU,DO YOUR
TASK!
TASK TASK
LIGHT LIGHT
ON OFF
c
Chain of Responsibility
c
Define a method of passing a request among a chain of objects.
Nooku
●● KCommandChain
+ KCommandContext, KCommandEvent, KCommandHandler and
KCommandInterface
HÉ! he means:
do this, do that,
and after finishing it,
go there!
c
Iterator
Enable sequential access to collection elements, without showing
the underlying data-structures (array, list, records, etc)
next
next
c
Mediator
c Layer in between: communication via one object.
Memento
Save and restore the internal state of an object.
ME
c
Observer
Notify “subscribers” of changes.
ME
NO ME
ME
WHO?
c
Joomla!
●● JObserver and JObservable
●● JObserver extended by JEvent and that by JPlugin
Nooku
●● KPatternObserver and KPatternObservable
State
Let 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
Strategy
When something can be done in several ways, make those
ways interchangeable.
POSSI-
BILITIES
c
Template Method
The skeleton of an algorithm is fixed, but parts can be filled in
differently.
c
Visitor
Make 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
very nice!
classic,
The Book
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 Peeren
[email protected]
© Yepr
The artwork in this presentation is provided under the
Creative Commons Public License for noncommercial use.
http://creativecommons.org/licenses/by-nc/3.0/legalcode