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

Refactoring to Design Patterns

Aug 20, 2015

Download

Software

Eric De Carufel
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Refactoring to Design Patterns

Design Patterns

How to include them in existing code

/Studio

Page 2: Refactoring to Design Patterns

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.

Page 3: Refactoring to Design Patterns

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

Page 4: Refactoring to Design Patterns

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

Page 5: Refactoring to Design Patterns

Architect defined - Irreversibility

• Architect’s most important tasks is to remove architecture by finding ways to eliminate irreversibility in software designs

Martin Fowler

Page 6: Refactoring to Design Patterns

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

Page 7: Refactoring to Design Patterns

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

Page 8: Refactoring to Design Patterns

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

Page 9: Refactoring to Design 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

Page 10: Refactoring to Design Patterns

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

Page 11: Refactoring to Design 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

Page 12: Refactoring to Design Patterns

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

Page 13: Refactoring to Design Patterns

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

Page 14: Refactoring to Design Patterns

Patterns avoid silly mistake

• Putting a toilet next to a refrigerator• Putting dishwasher next to a bathtubs

Page 15: Refactoring to Design Patterns

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)

Page 16: Refactoring to Design Patterns

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

Page 17: Refactoring to Design Patterns

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.

Page 18: Refactoring to Design Patterns

Use Factories to delegate the production of objects.

Page 19: Refactoring to Design Patterns

Use Decorators to avoid hierarchy explosion

Page 20: Refactoring to Design Patterns

Use Proxies to control access to objects

Page 21: Refactoring to Design Patterns

Use Strategies to pass algorithms to objects

Page 22: Refactoring to Design Patterns

Use Commands to pass requests to objects

Page 23: Refactoring to Design Patterns

Use Template Method to delegate part of an algorithm

Page 24: Refactoring to Design Patterns

Use the Composite Pattern to treat objects and collections uniformly

Page 25: Refactoring to Design Patterns

Complexity

0 1 2 3 4 5 60

1

Local

Com

plex

ity

Abstraction level

Page 26: Refactoring to Design Patterns

Complexity

0 1 2 3 4 5 60

1

LocalGlobal

Com

plex

ity

Abstraction level

Confort zone

Page 27: Refactoring to Design Patterns

• Transform code to use patternsD

EM

O

Page 28: Refactoring to Design Patterns

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?