Refactoring to Design Patterns

28
Design Patterns How to include them in existing code /Studio

Transcript of Refactoring to Design Patterns

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

CODEimplementation

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

PresentationPresentation

InfrastructureInfrastructure

Layers

Domain LogicDomain 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 2 3 4 5 60

1

Local

Com

plex

ity

Abstraction level

Complexity

0 1 2 3 4 5 60

1

LocalGlobal

Com

plex

ity

Abstraction level

Confort zone

• Transform code to use patternsD

EM

O

28

The end

• Remember– Pattern for common naming– Pattern to solve well known problem– Watch out for complexity

• Eric De Carufel– [email protected]– http://blog.decarufel.net– http://pyxis-tech.com

• Questions?