Design Patterns
How to include them in
existing code
/Studio
Architecture
• Wikipedia
– The software architecture of a program or
computing system is the structure or structures of
the system, which comprise software
components, the externally visible properties of
those components, and the relationships between
them.
Architecture defined - Decision
• Software architecture encompasses the set of significant
decisions about the organization of a software system
– Selection of the structural elements and their interfaces by
which a system is composed
– Behavior as specified in collaborations among those elements
– Composition of these structural and behavioral elements into
larger subsystems
– Architectural style that guides this organization
Booch, Kruchten, Reitman,
Bittner, and Shaw
Architecture defined - Irreversibility
• Architecture establishes the irreversible
context for design and implementation
architecture
CODE
implementation
design
Architectural
decisions are the
most fundamental
decisions; changing
them will have
significant ripple
effects because they
are hard to reverse
Architect defined - Irreversibility
• Architect’s most important tasks is to remove
architecture by finding ways to eliminate
irreversibility in software designs
Martin Fowler
Design challenges
• Design is non-deterministic
– Not a repeatable processes that are guaranteed to produce
predictable results
• Design techniques tend to be “heuristics”
– “rules of thumb”
– “things to try that sometimes work”
• Design is an iterative process
– Sloppy process
– About trade-offs and priorities
– Involves trial and error
Fundamentals heuristics
• Crisp abstractions
– Keep Your Design Modular
– Aim for Strong Cohesion
• Clear Separation of Concerns
– Stratification
• Accommodate human cognitive limitations
• Balanced distribution of responsibilities
– Find Behavior and Real-World Objects
• Keep Coupling Loose
– Hold connections among different parts to a minimum
– Formalize Class Contracts
– Encapsulate Implementation Details
Fundamentals heuristics
• Choose simple solution
– No extra parts (TDD enforce this)
– Avoid Failure
– Consider Using Brute Force
• Design for Test
• Identify Areas Likely to Change
– Extensibility, Plugins, IOC, DI
• Look for proven solution
– Patterns
Patterns defined
• A pattern is a proven solution to a common and
recurring problem
• A pattern codifies specific knowledge collected from
experience in a domain
• A pattern resolves forces in context
• All well-structured systems are full of patterns
•http://www.hillside.net
Patterns raise the level of abstraction
• For a developer the standard design elements are
objects
– Everything is an object
• For an architect the standard design elements are
patterns
Patterns creates a vocabulary
• “Everything is an object” is like an architect
summing up a house by saying “Everything is a
room”
– Big room with high voltage outlets and a sink to
cook
– Small room upstairs to sleep
• Room follow patterns and special names are
created to describe it
– Kitchen
– Bedroom
Patterns simplifies communication
• It enables packing a huge amount of information
into a short sentence
– Three-bedroom, two-bath house with an open-plan
kitchen
Patterns are proven solutions
• Not all combinations of design elements are
practical
• Bedroom and bathroom are separated
– Bathrooms have specialized and expensive
infrastructure requirements
– Bathroom require maximum privacy while
bedroom can be share
• Bathtub and toilet end up in the same room
– Both require the same infrastructure
Patterns avoid silly mistake
• Putting a toilet next to a refrigerator
• Putting dishwasher next to a bathtubs
Patterns and software design
• Starting in the 1990s software patterns were
applied in many ways with success
– Design Patterrns (Gang of four 1994)
– Enterprise architecture (Fowler 2002)
Layered Architecture
• Common technique to break apart
complicated software system into
partition in which each partition is at a
particular level of abstraction
• A layer is a cohesive and tightly
coupled partition that depends only
on the layers below
• The most simple but also the most
important enterprise application
patterns
• The hardest part of a layered
architecture is deciding what layers to
have and what the responsibilities of
each layer should be
Presentation
Infrastructure
Layers
Domain Logic
Patterns
• Use Factories to delegate the production of
objects.
• Use Decorators to avoid hierarchy explosion.
• Use Proxies to control access to objects.
• Use Strategies to pass algorithms to objects.
• Use Commands to pass requests to objects.
• Use Template Method to delegate parts of an
algorithm
• Use the Composite Pattern to treat objects and
collections uniformly.
Use Factories to delegate the production of objects.
Use Decorators to avoid hierarchy explosion
Use Proxies to control access to objects
Use Strategies to pass algorithms to objects
Use Commands to pass requests to objects
Use Template Method to delegate part of an algorithm
Use the Composite Pattern to treat objects and collections uniformly
Complexity
0
1
0 1 2 3 4 5 6
Local
Complexity
Abstraction level
Complexity
0
1
0 1 2 3 4 5 6
Local
Global
Complexity
Abstraction level
Confort zone
• Transform code to use patterns
DEMO
The end
• Remember
– Pattern for common naming
– Pattern to solve well known problem
– Watch out for complexity
• Eric De Carufel
– eric@decarufel.net
– http://blog.decarufel.net
– http://pyxis-tech.com
• Questions?
28

Refactoring to Design Patterns

  • 1.
    Design Patterns How toinclude them in existing code /Studio
  • 2.
    Architecture • Wikipedia – Thesoftware architecture of a program or computing system is the structure or structures of the system, which comprise software components, the externally visible properties of those components, and the relationships between them.
  • 3.
    Architecture defined -Decision • Software architecture encompasses the set of significant decisions about the organization of a software system – Selection of the structural elements and their interfaces by which a system is composed – Behavior as specified in collaborations among those elements – Composition of these structural and behavioral elements into larger subsystems – Architectural style that guides this organization Booch, Kruchten, Reitman, Bittner, and Shaw
  • 4.
    Architecture defined -Irreversibility • Architecture establishes the irreversible context for design and implementation architecture CODE implementation design Architectural decisions are the most fundamental decisions; changing them will have significant ripple effects because they are hard to reverse
  • 5.
    Architect defined -Irreversibility • Architect’s most important tasks is to remove architecture by finding ways to eliminate irreversibility in software designs Martin Fowler
  • 6.
    Design challenges • Designis non-deterministic – Not a repeatable processes that are guaranteed to produce predictable results • Design techniques tend to be “heuristics” – “rules of thumb” – “things to try that sometimes work” • Design is an iterative process – Sloppy process – About trade-offs and priorities – Involves trial and error
  • 7.
    Fundamentals heuristics • Crispabstractions – Keep Your Design Modular – Aim for Strong Cohesion • Clear Separation of Concerns – Stratification • Accommodate human cognitive limitations • Balanced distribution of responsibilities – Find Behavior and Real-World Objects • Keep Coupling Loose – Hold connections among different parts to a minimum – Formalize Class Contracts – Encapsulate Implementation Details
  • 8.
    Fundamentals heuristics • Choosesimple solution – No extra parts (TDD enforce this) – Avoid Failure – Consider Using Brute Force • Design for Test • Identify Areas Likely to Change – Extensibility, Plugins, IOC, DI • Look for proven solution – Patterns
  • 9.
    Patterns defined • Apattern is a proven solution to a common and recurring problem • A pattern codifies specific knowledge collected from experience in a domain • A pattern resolves forces in context • All well-structured systems are full of patterns •http://www.hillside.net
  • 10.
    Patterns raise thelevel of abstraction • For a developer the standard design elements are objects – Everything is an object • For an architect the standard design elements are patterns
  • 11.
    Patterns creates avocabulary • “Everything is an object” is like an architect summing up a house by saying “Everything is a room” – Big room with high voltage outlets and a sink to cook – Small room upstairs to sleep • Room follow patterns and special names are created to describe it – Kitchen – Bedroom
  • 12.
    Patterns simplifies communication •It enables packing a huge amount of information into a short sentence – Three-bedroom, two-bath house with an open-plan kitchen
  • 13.
    Patterns are provensolutions • Not all combinations of design elements are practical • Bedroom and bathroom are separated – Bathrooms have specialized and expensive infrastructure requirements – Bathroom require maximum privacy while bedroom can be share • Bathtub and toilet end up in the same room – Both require the same infrastructure
  • 14.
    Patterns avoid sillymistake • Putting a toilet next to a refrigerator • Putting dishwasher next to a bathtubs
  • 15.
    Patterns and softwaredesign • Starting in the 1990s software patterns were applied in many ways with success – Design Patterrns (Gang of four 1994) – Enterprise architecture (Fowler 2002)
  • 16.
    Layered Architecture • Commontechnique to break apart complicated software system into partition in which each partition is at a particular level of abstraction • A layer is a cohesive and tightly coupled partition that depends only on the layers below • The most simple but also the most important enterprise application patterns • The hardest part of a layered architecture is deciding what layers to have and what the responsibilities of each layer should be Presentation Infrastructure Layers Domain Logic
  • 17.
    Patterns • Use Factoriesto delegate the production of objects. • Use Decorators to avoid hierarchy explosion. • Use Proxies to control access to objects. • Use Strategies to pass algorithms to objects. • Use Commands to pass requests to objects. • Use Template Method to delegate parts of an algorithm • Use the Composite Pattern to treat objects and collections uniformly.
  • 18.
    Use Factories todelegate the production of objects.
  • 19.
    Use Decorators toavoid hierarchy explosion
  • 20.
    Use Proxies tocontrol access to objects
  • 21.
    Use Strategies topass algorithms to objects
  • 22.
    Use Commands topass requests to objects
  • 23.
    Use Template Methodto delegate part of an algorithm
  • 24.
    Use the CompositePattern to treat objects and collections uniformly
  • 25.
    Complexity 0 1 0 1 23 4 5 6 Local Complexity Abstraction level
  • 26.
    Complexity 0 1 0 1 23 4 5 6 Local Global Complexity Abstraction level Confort zone
  • 27.
    • Transform codeto use patterns DEMO
  • 28.
    The end • Remember –Pattern for common naming – Pattern to solve well known problem – Watch out for complexity • Eric De Carufel – eric@decarufel.net – http://blog.decarufel.net – http://pyxis-tech.com • Questions? 28