23
Introduction to Design Patterns Introduction to Design Patterns João Miguel Pereira http://jpereira.eu M 2012 May 2012 Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons AttributionNonCommercialShareAlike 3.0 Unported License .

Introduction to Design Patterns

Embed Size (px)

DESCRIPTION

This is the introduction to design patterns, part of my course on Object Oriented Programming.

Citation preview

Page 1: Introduction to Design Patterns

Introduction to Design PatternsIntroduction to Design Patterns

João Miguel Pereirahttp://jpereira.eu

M 2012May 2012

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike3.0 Unported License.

Page 2: Introduction to Design Patterns

Introduction to Design PatternsIntroduction to Design Patterns

• What is a patternWhat is a pattern

• Architecture Patterns

i• Design Patterns

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

2

Page 3: Introduction to Design Patterns

Introduction to Design PatternsIntroduction to Design Patterns

• What is a PatternWhat is a Pattern

• Architecture Patterns

i• Design Patterns

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

3

Page 4: Introduction to Design Patterns

What is a PatternWhat is a Pattern

“It can be a template or model which can be used to generate things or parts of a thing, especially if the things that are created have 

enough in common for the underlying pattern to be inferred, in which enough in common for the underlying pattern to be inferred, in whichcase the things are said to exhibit the unique pattern.“ ‐

http://en.wikipedia.org/wiki/Pattern

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

4

Page 5: Introduction to Design Patterns

What is a PatternIn construction of towns and building

"Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a 

h hi l i illi i i h d i i hway that you can use this solution a million times over, without ever doing it the same way twice” ‐‐ Christopher Alexander, Sara Ishikawa, Murray Silverstein, Max Jacobson, Ingrid Fiksdahl‐King, 

and Shlomo Angel. A Pattern Language. Oxford University Press, New York, 1977. 

fl lfl lInfluential in Software design 

patterns

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

5

Christopher Alexander

Page 6: Introduction to Design Patterns

What is a PatternIn construction of Software

“Our solutions are expressed in terms of objects and interfaces instead of walls and doors, but at the core of both kinds of patterns is a solution to a problem in a doo s, but at t e co e o bot ds o patte s s a so ut o to a p ob e acontext.” – Eric Gamma, et.al. Design patterns : elements of reusable object‐oriented software

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

6

Christopher AlexanderEric Gamma

Page 7: Introduction to Design Patterns

What is a PatternEssential Elements of a Pattern

• Name Use it in our design vocabularyName  Use it in our design vocabulary

• Problem  Explain the problem and the context.

S l i• Solution  Describes the elements that make up the design, their relationships, responsibilities, and 

collaborationscollaborations. 

• Consequences  The results and trade‐offs of applying the patternthe pattern .

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

7

Christopher Alexander

Page 8: Introduction to Design Patterns

What is a patternAbstraction in Software Design Patterns

Abstract

Architecture patterns

FocusFocusDesign patterns

Details

Focus

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

8

Page 9: Introduction to Design Patterns

Introduction to Design PatternsIntroduction to Design Patterns

• What is a Design PatternWhat is a Design Pattern

• Architecture Patterns

i• Design Patterns

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

9

Page 10: Introduction to Design Patterns

Architecture PatternsArchitecture Patterns

• A fundamental structural organization schemaA fundamental structural organization schema for software systems.

• Provides predefined subsystems and their• Provides predefined subsystems and their responsibilities

Gi l f i i h l i hi• Give rules for organizing the relationships between these sub systems

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

10

Page 11: Introduction to Design Patterns

Architecture PatternsArchitecture Patterns

• Architecture pattern example: LayersArchitecture pattern example: Layers– Helps to structure applications that can be decomposed into groups of subtasks in whichdecomposed into groups of subtasks in which each group of subtasks is at a particular level of abstraction

Presentation Layer

Domain/Business Layer

Data Layer

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

11

y

Page 12: Introduction to Design Patterns

Architecture PatternsArchitecture Patterns

• Architecture pattern example: Pipes & FiltersArchitecture pattern example: Pipes & Filters– Help to structure systems that process a stream of datadata. 

JSON FilterData 

Extractor Filter

Data Normalizer

Data Storage Filter

Internet Publisher Filter

JSON Filter

XML Filter

HTML Filter

Data

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

12

ata

Page 13: Introduction to Design Patterns

Architecture PatternsArchitecture Patterns• Architecture pattern example: Blackboardc tectu e patte e a p e: ac boa d

– Several specialized subsystems assemble their knowledge to build a possibly partial or approximate l tisolution.

– Useful for problems for which no deterministic solution strategies are known.g

CRM S t

InventoryS t

FinancialSystem

DeliverySystem System SystemSystem

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

13

Enterprise Service Bus (EX: Service Mix)

Page 14: Introduction to Design Patterns

Architecture PatternsArchitecture Patterns

• Architecture pattern example:Model ViewArchitecture pattern example: Model View Controller– Splits the application into separate layers (Using– Splits the application into separate layers (Using Layers Pattern)

Presentation Layer

Model LayerModel Layer

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

14

Page 15: Introduction to Design Patterns

Architecture PatternsArchitecture Patterns

• Architecture pattern example:Model ViewArchitecture pattern example: Model View Controller– Presentation layer is further split into View and– Presentation layer is further split into View and Controller

Presentation Layer

View Layer

Controller Layer

Model Layer

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

15

Page 16: Introduction to Design Patterns

Architecture PatternsArchitecture Patterns

• Architecture pattern example:Model ViewArchitecture pattern example: Model View Controller– Each layer is assigned a set of responsibilities– Each layer is assigned a set of responsibilities

View LayerRenders the model into a user interface. 

Can render HTLM, JSON, XML, etc.View Layer

Controller Layer

Responds to user events and may invoke changes on the 

model layer

Model Layer

Controller Layer

Operate on business data, i.e., the business Logic.

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

16

Page 17: Introduction to Design Patterns

Architecture PatternsArchitecture Patterns

• Architecture pattern example:Model ViewArchitecture pattern example: Model View Controller– User events in a Web application are usually an– User events, in a Web application, are usually an HTTP Request

HTTP Request1 HTTP Response4

Controller

ModelViews

Render3 Update2

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

17

Page 18: Introduction to Design Patterns

Architecture PatternsArchitecture Patterns• Architecture pattern example: Model View Controllerp p

– Many interpretations of the Pattern– Some web links:

h //h i ifi i / / h / / i d h l• http://heim.ifi.uio.no/~trygver/themes/mvc/mvc‐index.html• http://martinfowler.com/eaaDev/uiArchs.html• http://st‐www.cs.illinois.edu/users/smarch/st‐docs/mvc.html

– Some books:• Patterns of Enterprise Architecture (http://amzn.to/book_PEA )• Pattern‐Oriented Software Architecture Volume 1: A System of Patterns (http://amzn.to/book_POSAV1 )

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

18

Page 19: Introduction to Design Patterns

Architecture PatternsArchitecture Patterns

• More Architecture Patterns:More Architecture Patterns:– Patterns of Enterprise Architecture

• http://amzn to/book PEAhttp://amzn.to/book_PEA

– Pattern‐Oriented Software Architecture Volume 1: A System of Patterns

• http://amzn.to/book_POSAV1 

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

19

Page 20: Introduction to Design Patterns

Introduction to Design PatternsIntroduction to Design Patterns

• What is a Design PatternWhat is a Design Pattern

• Architecture Patterns

i• Design Patterns

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

20

Page 21: Introduction to Design Patterns

Design patternDesign pattern

Framework for thinking and d i i OO !designing OO programs!

Reusable knowledge in SoftwareReusable knowledge in Software Design!

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

21

Page 22: Introduction to Design Patterns

Design patternDesign pattern

Java Frameworks for OOKnowledge Framework Java Frameworks for OO Development

Knowledge Framework for OO Design

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

22

Page 23: Introduction to Design Patterns

Design PatternsThe Book

http://amzn.to/book_DPEROOS 

Introduction to Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0 Unported License

23