SlideShare a Scribd company logo
1
A Brief Introduction to Design
Patterns
Based on materials from Doug Schmidt
2
Object-oriented design
• OOD methods emphasize design notations
― Fine for specification, documentation
• But OOD is more than just drawing diagrams
― Good draftsmen, good designers
• Good OO designers rely on lots of experience
― At least as important as syntax
• Most powerful reuse is design reuse
Match problem to design experience
• OO systems exhibit recurring structures that
promote: abstraction, flexibility, modularity,
elegance
3
What is a design pattern?
• Codify design decisions and best practices
for solving recurring problems
― Not software libraries
― Not packaged solutions
― Templates that must be recognized and
adapted for a particular use
4
Four basic parts
1. Name
2. Problem
3. Solution
4. Trade-offs of application
5
Example: Observer pattern
6
Observer pattern components [1/2]
7
Observer pattern components [2/2]
8
Design patterns goals
• Codify good design
― distill & generalize experience
― aid to novices & experts alike
• Give design structures explicit names
― common vocabulary
― reduced complexity
― greater expressiveness
• Capture & preserve design information
― articulate design decisions succinctly
― improve documentation
• Facilitate restructuring/refactoring
― patterns are interrelated
― additional flexibility
9
GoF design patterns
Scope: domain over which a pattern applies
Purpose: reflects what a pattern does
GoF: Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides
10
Design pattern template [1/2]
• Intent
― short description of the pattern & its purpose
• Also Known As
― Any aliases this pattern is known by
• Motivation
― motivating scenario demonstrating pattern’s use
• Applicability
― circumstances in which pattern applies
• Structure
― graphical representation of the pattern using modified UML notation
• Participants
― participating classes and/or objects & their responsibilities
11
Design pattern template [2/2]
• Collaborations
― how participants cooperate to carry out their responsibilities
• Consequences
― the results of application, benefits, liabilities
• Implementation
― pitfalls, hints, techniques, plus language-dependent issues
• Sample Code
― sample implementations in C++, Java, C#, Smalltalk, C, etc.
• Known Uses
― examples drawn from existing systems
• Related Patterns
― discussion of other patterns that relate to this one
12
UML/OMT notation
13
Observer design pattern [1/3]
Intent
― define a one-to-many dependency between objects so that when one object
changes state, all dependents are notified & updated
Applicability
― an abstraction has two aspects, one dependent on the other
― a change to one object requires changing untold others
― an object should notify unknown other objects
Structure
14
Observer design pattern [2/3]
class ProxyPushConsumer : public // …
virtual void push (const CORBA::Any &event) {
for (std::vector<PushConsumer>::iterator i
(consumers.begin ()); i != consumers.end (); i++)
(*i).push (event);
}
class MyPushConsumer : public // ….
virtual void push
(const CORBA::Any &event) { /* consume the event. */ }
15
Observer design pattern [3/3]
• Consequences
― modularity: subject & observers may vary independently
― extensibility: can define & add any number of observers
― customizability: different observers offer different views of subject
― unexpected updates: observers don’t know about each other
― update overhead: might need hints or filtering
• Implementation
― subject-observer mapping
― dangling references
― update protocols: the push & pull models
― registering modifications of interest explicitly
• Known Uses
― Smalltalk Model-View-Controller (MVC)
― InterViews (Subjects & Views, Observer/Observable)
― Pub/sub middleware (e.g., CORBA Notification Service, Java Messaging Service)
― Mailing lists
16
Benefits of design patterns
• Design reuse
• Uniform design vocabulary
• Enhance understanding, restructuring, &
team communication
• Basis for automation
• Transcends language-centric biases/myopia
• Abstracts away from many unimportant
details
17
Another example pattern: Template
Provides a skeleton of an algorithm in a method, deferring some steps to
subclasses (to avoid duplication)
class Base_Class {
public:
// Template Method.
void template_method (void) {
hook_method_1 ();
hook_method_2 ();
// ...
}
virtual void hook_method_1 () = 0;
virtual void hook_method_2 () = 0;
};
class Derived_Class_1 : public Base_Class {
virtual void hook_method_2 () { /* ... */ }
};
class Derived_Class_2 : public Base_Class {
virtual void hook_method_1 () { /* ... */ }
virtual void hook_method_2 () { /* ... */ }
};
18
Yet another design pattern: adapter [1/1]
• When two software components (e.g.,
legacy code and new development or a
COTS) cannot interface
• Adapter changes interface of one so the
other can use it
― Adapter fills the gap b/w two interfaces
― No changes needed for either
19
Yet another design pattern: adapter [2/2]
class NewTime
{
public:
int GetTime() {
return otime.get_time() * 100;
}
private:
OriginalTime otime;
};
An alternate: a wrapper
20
Relationship with other design
concepts

More Related Content

PPT
HCI 3e - Ch 11: User support
PPT
Object oriented analysis & Design- Overview
PPTX
A Comparative Analysis of Task Modeling Notations
PPT
Introduction to design patterns
PPTX
design pattern is the computer scicence subject
PPT
Design Patterns.ppt
PDF
software engineering Design Patterns.pdf
PPT
Chapter 4_Introduction to Patterns.ppt
HCI 3e - Ch 11: User support
Object oriented analysis & Design- Overview
A Comparative Analysis of Task Modeling Notations
Introduction to design patterns
design pattern is the computer scicence subject
Design Patterns.ppt
software engineering Design Patterns.pdf
Chapter 4_Introduction to Patterns.ppt

Similar to Design patterns (20)

PPT
Chapter 4_Introduction to Patterns.ppt
PPTX
Design Patterns - General Introduction
PPT
M04_DesignPatterns software engineering.ppt
PPTX
Behavioral pattern By:-Priyanka Pradhan
PDF
Design Patterns Java programming language.pdf
PPTX
sample Pattern Design explaine .pptx
PPTX
C# Design Patterns | Design Pattern Tutorial For Beginners | C# Programming T...
PPTX
Software design and Architecture.pptx
PDF
Introduction to Design Pattern
PPTX
L05 Design Patterns
PPTX
L03 Design Patterns
DOCX
Design patterns
PDF
Design Pattern in Software Engineering
PPTX
DesignPattern-Overview.pptx
PPTX
Design patterns in Object oriented analysis and design
PPT
Design pattern
DOCX
Design pattern application
PPTX
Design Pattern - Introduction
PPTX
Design pattern
PDF
Module 4: UML In Action - Design Patterns
Chapter 4_Introduction to Patterns.ppt
Design Patterns - General Introduction
M04_DesignPatterns software engineering.ppt
Behavioral pattern By:-Priyanka Pradhan
Design Patterns Java programming language.pdf
sample Pattern Design explaine .pptx
C# Design Patterns | Design Pattern Tutorial For Beginners | C# Programming T...
Software design and Architecture.pptx
Introduction to Design Pattern
L05 Design Patterns
L03 Design Patterns
Design patterns
Design Pattern in Software Engineering
DesignPattern-Overview.pptx
Design patterns in Object oriented analysis and design
Design pattern
Design pattern application
Design Pattern - Introduction
Design pattern
Module 4: UML In Action - Design Patterns
Ad

Recently uploaded (20)

PPTX
ANATOMY OF ANTERIOR CHAMBER ANGLE AND GONIOSCOPY.pptx
PDF
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
PPTX
Causes of Flooding by Slidesgo sdnl;asnjdl;asj.pptx
PDF
Quality Control Management for RMG, Level- 4, Certificate
PDF
YOW2022-BNE-MinimalViableArchitecture.pdf
PPTX
CLASS_11_BUSINESS_STUDIES_PPT_CHAPTER_1_Business_Trade_Commerce.pptx
PPTX
rapid fire quiz in your house is your india.pptx
PPTX
mahatma gandhi bus terminal in india Case Study.pptx
PDF
Urban Design Final Project-Site Analysis
PDF
Chalkpiece Annual Report from 2019 To 2025
PPTX
Special finishes, classification and types, explanation
PDF
Skskkxiixijsjsnwkwkaksixindndndjdjdjsjjssk
PDF
Integrated-2D-and-3D-Animation-Bridging-Dimensions-for-Impactful-Storytelling...
PPTX
An introduction to AI in research and reference management
PPTX
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
PPTX
CLASSIFICATION OF YARN- process, explanation
PPTX
building Planning Overview for step wise design.pptx
PPTX
Media And Information Literacy for Grade 12
PDF
Urban Design Final Project-Context
PDF
UNIT 1 Introduction fnfbbfhfhfbdhdbdto Java.pptx.pdf
ANATOMY OF ANTERIOR CHAMBER ANGLE AND GONIOSCOPY.pptx
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
Causes of Flooding by Slidesgo sdnl;asnjdl;asj.pptx
Quality Control Management for RMG, Level- 4, Certificate
YOW2022-BNE-MinimalViableArchitecture.pdf
CLASS_11_BUSINESS_STUDIES_PPT_CHAPTER_1_Business_Trade_Commerce.pptx
rapid fire quiz in your house is your india.pptx
mahatma gandhi bus terminal in india Case Study.pptx
Urban Design Final Project-Site Analysis
Chalkpiece Annual Report from 2019 To 2025
Special finishes, classification and types, explanation
Skskkxiixijsjsnwkwkaksixindndndjdjdjsjjssk
Integrated-2D-and-3D-Animation-Bridging-Dimensions-for-Impactful-Storytelling...
An introduction to AI in research and reference management
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
CLASSIFICATION OF YARN- process, explanation
building Planning Overview for step wise design.pptx
Media And Information Literacy for Grade 12
Urban Design Final Project-Context
UNIT 1 Introduction fnfbbfhfhfbdhdbdto Java.pptx.pdf
Ad

Design patterns

  • 1. 1 A Brief Introduction to Design Patterns Based on materials from Doug Schmidt
  • 2. 2 Object-oriented design • OOD methods emphasize design notations ― Fine for specification, documentation • But OOD is more than just drawing diagrams ― Good draftsmen, good designers • Good OO designers rely on lots of experience ― At least as important as syntax • Most powerful reuse is design reuse Match problem to design experience • OO systems exhibit recurring structures that promote: abstraction, flexibility, modularity, elegance
  • 3. 3 What is a design pattern? • Codify design decisions and best practices for solving recurring problems ― Not software libraries ― Not packaged solutions ― Templates that must be recognized and adapted for a particular use
  • 4. 4 Four basic parts 1. Name 2. Problem 3. Solution 4. Trade-offs of application
  • 8. 8 Design patterns goals • Codify good design ― distill & generalize experience ― aid to novices & experts alike • Give design structures explicit names ― common vocabulary ― reduced complexity ― greater expressiveness • Capture & preserve design information ― articulate design decisions succinctly ― improve documentation • Facilitate restructuring/refactoring ― patterns are interrelated ― additional flexibility
  • 9. 9 GoF design patterns Scope: domain over which a pattern applies Purpose: reflects what a pattern does GoF: Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides
  • 10. 10 Design pattern template [1/2] • Intent ― short description of the pattern & its purpose • Also Known As ― Any aliases this pattern is known by • Motivation ― motivating scenario demonstrating pattern’s use • Applicability ― circumstances in which pattern applies • Structure ― graphical representation of the pattern using modified UML notation • Participants ― participating classes and/or objects & their responsibilities
  • 11. 11 Design pattern template [2/2] • Collaborations ― how participants cooperate to carry out their responsibilities • Consequences ― the results of application, benefits, liabilities • Implementation ― pitfalls, hints, techniques, plus language-dependent issues • Sample Code ― sample implementations in C++, Java, C#, Smalltalk, C, etc. • Known Uses ― examples drawn from existing systems • Related Patterns ― discussion of other patterns that relate to this one
  • 13. 13 Observer design pattern [1/3] Intent ― define a one-to-many dependency between objects so that when one object changes state, all dependents are notified & updated Applicability ― an abstraction has two aspects, one dependent on the other ― a change to one object requires changing untold others ― an object should notify unknown other objects Structure
  • 14. 14 Observer design pattern [2/3] class ProxyPushConsumer : public // … virtual void push (const CORBA::Any &event) { for (std::vector<PushConsumer>::iterator i (consumers.begin ()); i != consumers.end (); i++) (*i).push (event); } class MyPushConsumer : public // …. virtual void push (const CORBA::Any &event) { /* consume the event. */ }
  • 15. 15 Observer design pattern [3/3] • Consequences ― modularity: subject & observers may vary independently ― extensibility: can define & add any number of observers ― customizability: different observers offer different views of subject ― unexpected updates: observers don’t know about each other ― update overhead: might need hints or filtering • Implementation ― subject-observer mapping ― dangling references ― update protocols: the push & pull models ― registering modifications of interest explicitly • Known Uses ― Smalltalk Model-View-Controller (MVC) ― InterViews (Subjects & Views, Observer/Observable) ― Pub/sub middleware (e.g., CORBA Notification Service, Java Messaging Service) ― Mailing lists
  • 16. 16 Benefits of design patterns • Design reuse • Uniform design vocabulary • Enhance understanding, restructuring, & team communication • Basis for automation • Transcends language-centric biases/myopia • Abstracts away from many unimportant details
  • 17. 17 Another example pattern: Template Provides a skeleton of an algorithm in a method, deferring some steps to subclasses (to avoid duplication) class Base_Class { public: // Template Method. void template_method (void) { hook_method_1 (); hook_method_2 (); // ... } virtual void hook_method_1 () = 0; virtual void hook_method_2 () = 0; }; class Derived_Class_1 : public Base_Class { virtual void hook_method_2 () { /* ... */ } }; class Derived_Class_2 : public Base_Class { virtual void hook_method_1 () { /* ... */ } virtual void hook_method_2 () { /* ... */ } };
  • 18. 18 Yet another design pattern: adapter [1/1] • When two software components (e.g., legacy code and new development or a COTS) cannot interface • Adapter changes interface of one so the other can use it ― Adapter fills the gap b/w two interfaces ― No changes needed for either
  • 19. 19 Yet another design pattern: adapter [2/2] class NewTime { public: int GetTime() { return otime.get_time() * 100; } private: OriginalTime otime; }; An alternate: a wrapper
  • 20. 20 Relationship with other design concepts