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

Similar to Design patterns

DITEC - Software Engineering
DITEC - Software EngineeringDITEC - Software Engineering
DITEC - Software Engineering
Rasan Samarasinghe
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
Asma CHERIF
 
Introduction to OOAD
Introduction to OOADIntroduction to OOAD
Introduction to OOAD
Saraswati Saud
 
Pressman ch-3-prescriptive-process-models
Pressman ch-3-prescriptive-process-modelsPressman ch-3-prescriptive-process-models
Pressman ch-3-prescriptive-process-models
Noor Ul Hudda Memon
 
Design patterns tutorials
Design patterns tutorialsDesign patterns tutorials
Design patterns tutorials
University of Technology
 
ppt2.pptx
ppt2.pptxppt2.pptx
ppt2.pptx
JOHNNYGALLA2
 
Assignment 1 SYD601 2012 rick_danby completed with audio
Assignment 1 SYD601 2012 rick_danby completed with audioAssignment 1 SYD601 2012 rick_danby completed with audio
Assignment 1 SYD601 2012 rick_danby completed with audio
RickNZ
 
Object Oriented Analysis and Design - Overview
Object Oriented Analysis and Design - OverviewObject Oriented Analysis and Design - Overview
Object Oriented Analysis and Design - Overview
rmk_rrj
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
anguraju1
 
test.pptx
test.pptxtest.pptx
test.pptx
Stacia7
 
WINSEM2022-23_SWE2004_ETH_VL2022230501954_2023-02-01_Reference-Material-I.pptx
WINSEM2022-23_SWE2004_ETH_VL2022230501954_2023-02-01_Reference-Material-I.pptxWINSEM2022-23_SWE2004_ETH_VL2022230501954_2023-02-01_Reference-Material-I.pptx
WINSEM2022-23_SWE2004_ETH_VL2022230501954_2023-02-01_Reference-Material-I.pptx
Vivekananda Gn
 
5-CEN6016-Chapter1.ppt
5-CEN6016-Chapter1.ppt5-CEN6016-Chapter1.ppt
5-CEN6016-Chapter1.ppt
DrCMeenakshiVISTAS
 
Software Engineering Lec 8-design-
Software Engineering Lec 8-design-Software Engineering Lec 8-design-
Software Engineering Lec 8-design-
Taymoor Nazmy
 
Architectural Design & Patterns
Architectural Design&PatternsArchitectural Design&Patterns
Architectural Design & Patterns
Inocentshuja Ahmad
 
Object Oriented Analysis and Design Unit-1
Object Oriented Analysis and Design Unit-1Object Oriented Analysis and Design Unit-1
Object Oriented Analysis and Design Unit-1
SangeethaSubramaniam14
 
Software design
Software designSoftware design
Software design
Benazir Fathima
 
Object Oriented Analysis
Object Oriented AnalysisObject Oriented Analysis
Object Oriented Analysis
AMITJain879
 
Student feedback system
Student feedback systemStudent feedback system
Student feedback system
Akshay Surve
 
DOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in cDOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in c
floraaluoch3
 
CEN6016-Chapter1.ppt
CEN6016-Chapter1.pptCEN6016-Chapter1.ppt
CEN6016-Chapter1.ppt
SumitVishwambhar
 

Similar to Design patterns (20)

DITEC - Software Engineering
DITEC - Software EngineeringDITEC - Software Engineering
DITEC - Software Engineering
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
 
Introduction to OOAD
Introduction to OOADIntroduction to OOAD
Introduction to OOAD
 
Pressman ch-3-prescriptive-process-models
Pressman ch-3-prescriptive-process-modelsPressman ch-3-prescriptive-process-models
Pressman ch-3-prescriptive-process-models
 
Design patterns tutorials
Design patterns tutorialsDesign patterns tutorials
Design patterns tutorials
 
ppt2.pptx
ppt2.pptxppt2.pptx
ppt2.pptx
 
Assignment 1 SYD601 2012 rick_danby completed with audio
Assignment 1 SYD601 2012 rick_danby completed with audioAssignment 1 SYD601 2012 rick_danby completed with audio
Assignment 1 SYD601 2012 rick_danby completed with audio
 
Object Oriented Analysis and Design - Overview
Object Oriented Analysis and Design - OverviewObject Oriented Analysis and Design - Overview
Object Oriented Analysis and Design - Overview
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
test.pptx
test.pptxtest.pptx
test.pptx
 
WINSEM2022-23_SWE2004_ETH_VL2022230501954_2023-02-01_Reference-Material-I.pptx
WINSEM2022-23_SWE2004_ETH_VL2022230501954_2023-02-01_Reference-Material-I.pptxWINSEM2022-23_SWE2004_ETH_VL2022230501954_2023-02-01_Reference-Material-I.pptx
WINSEM2022-23_SWE2004_ETH_VL2022230501954_2023-02-01_Reference-Material-I.pptx
 
5-CEN6016-Chapter1.ppt
5-CEN6016-Chapter1.ppt5-CEN6016-Chapter1.ppt
5-CEN6016-Chapter1.ppt
 
Software Engineering Lec 8-design-
Software Engineering Lec 8-design-Software Engineering Lec 8-design-
Software Engineering Lec 8-design-
 
Architectural Design & Patterns
Architectural Design&PatternsArchitectural Design&Patterns
Architectural Design & Patterns
 
Object Oriented Analysis and Design Unit-1
Object Oriented Analysis and Design Unit-1Object Oriented Analysis and Design Unit-1
Object Oriented Analysis and Design Unit-1
 
Software design
Software designSoftware design
Software design
 
Object Oriented Analysis
Object Oriented AnalysisObject Oriented Analysis
Object Oriented Analysis
 
Student feedback system
Student feedback systemStudent feedback system
Student feedback system
 
DOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in cDOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in c
 
CEN6016-Chapter1.ppt
CEN6016-Chapter1.pptCEN6016-Chapter1.ppt
CEN6016-Chapter1.ppt
 

Recently uploaded

哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样
哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样
哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样
qo1as76n
 
Manual ISH (International Society of Hypertension)
Manual ISH (International Society of Hypertension)Manual ISH (International Society of Hypertension)
Manual ISH (International Society of Hypertension)
bagmai
 
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
21uul8se
 
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
k7nm6tk
 
UXpert_Report (UALR Mapping Renewal 2022).pdf
UXpert_Report (UALR Mapping Renewal 2022).pdfUXpert_Report (UALR Mapping Renewal 2022).pdf
UXpert_Report (UALR Mapping Renewal 2022).pdf
anthonylin333
 
一比一原版阿肯色大学毕业证(UCSF毕业证书)如何办理
一比一原版阿肯色大学毕业证(UCSF毕业证书)如何办理一比一原版阿肯色大学毕业证(UCSF毕业证书)如何办理
一比一原版阿肯色大学毕业证(UCSF毕业证书)如何办理
bo44ban1
 
CocaCola_Brand_equity_package_2012__.pdf
CocaCola_Brand_equity_package_2012__.pdfCocaCola_Brand_equity_package_2012__.pdf
CocaCola_Brand_equity_package_2012__.pdf
PabloMartelLpez
 
定制美国西雅图城市大学毕业证学历证书原版一模一样
定制美国西雅图城市大学毕业证学历证书原版一模一样定制美国西雅图城市大学毕业证学历证书原版一模一样
定制美国西雅图城市大学毕业证学历证书原版一模一样
qo1as76n
 
一比一原版马来西亚世纪大学毕业证成绩单一模一样
一比一原版马来西亚世纪大学毕业证成绩单一模一样一比一原版马来西亚世纪大学毕业证成绩单一模一样
一比一原版马来西亚世纪大学毕业证成绩单一模一样
k4krdgxx
 
Heuristics Evaluation - How to Guide.pdf
Heuristics Evaluation - How to Guide.pdfHeuristics Evaluation - How to Guide.pdf
Heuristics Evaluation - How to Guide.pdf
Jaime Brown
 
LGBTQIA Pride Month presentation Template
LGBTQIA Pride Month presentation TemplateLGBTQIA Pride Month presentation Template
LGBTQIA Pride Month presentation Template
DakshGudwani
 
Practical eLearning Makeovers for Everyone
Practical eLearning Makeovers for EveryonePractical eLearning Makeovers for Everyone
Practical eLearning Makeovers for Everyone
Bianca Woods
 
一比一原版(Vancouver毕业证书)温哥华岛大学毕业证如何办理
一比一原版(Vancouver毕业证书)温哥华岛大学毕业证如何办理一比一原版(Vancouver毕业证书)温哥华岛大学毕业证如何办理
一比一原版(Vancouver毕业证书)温哥华岛大学毕业证如何办理
ijk38lw
 
一比一原版马里兰大学毕业证(UMD毕业证书)如何办理
一比一原版马里兰大学毕业证(UMD毕业证书)如何办理一比一原版马里兰大学毕业证(UMD毕业证书)如何办理
一比一原版马里兰大学毕业证(UMD毕业证书)如何办理
9lq7ultg
 
International Upcycling Research Network advisory board meeting 4
International Upcycling Research Network advisory board meeting 4International Upcycling Research Network advisory board meeting 4
International Upcycling Research Network advisory board meeting 4
Kyungeun Sung
 
NHR Engineers Portfolio 2023 2024 NISHANT RATHI
NHR Engineers Portfolio 2023 2024 NISHANT RATHINHR Engineers Portfolio 2023 2024 NISHANT RATHI
NHR Engineers Portfolio 2023 2024 NISHANT RATHI
NishantRathi18
 
Getting Data Ready for Culture Hack by Neontribe
Getting Data Ready for Culture Hack by NeontribeGetting Data Ready for Culture Hack by Neontribe
Getting Data Ready for Culture Hack by Neontribe
Harry Harrold
 
Introduction to User experience design for beginner
Introduction to User experience design for beginnerIntroduction to User experience design for beginner
Introduction to User experience design for beginner
ellemjani
 
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
3vgr39kx
 
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Designforuminternational
 

Recently uploaded (20)

哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样
哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样
哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样
 
Manual ISH (International Society of Hypertension)
Manual ISH (International Society of Hypertension)Manual ISH (International Society of Hypertension)
Manual ISH (International Society of Hypertension)
 
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
 
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
 
UXpert_Report (UALR Mapping Renewal 2022).pdf
UXpert_Report (UALR Mapping Renewal 2022).pdfUXpert_Report (UALR Mapping Renewal 2022).pdf
UXpert_Report (UALR Mapping Renewal 2022).pdf
 
一比一原版阿肯色大学毕业证(UCSF毕业证书)如何办理
一比一原版阿肯色大学毕业证(UCSF毕业证书)如何办理一比一原版阿肯色大学毕业证(UCSF毕业证书)如何办理
一比一原版阿肯色大学毕业证(UCSF毕业证书)如何办理
 
CocaCola_Brand_equity_package_2012__.pdf
CocaCola_Brand_equity_package_2012__.pdfCocaCola_Brand_equity_package_2012__.pdf
CocaCola_Brand_equity_package_2012__.pdf
 
定制美国西雅图城市大学毕业证学历证书原版一模一样
定制美国西雅图城市大学毕业证学历证书原版一模一样定制美国西雅图城市大学毕业证学历证书原版一模一样
定制美国西雅图城市大学毕业证学历证书原版一模一样
 
一比一原版马来西亚世纪大学毕业证成绩单一模一样
一比一原版马来西亚世纪大学毕业证成绩单一模一样一比一原版马来西亚世纪大学毕业证成绩单一模一样
一比一原版马来西亚世纪大学毕业证成绩单一模一样
 
Heuristics Evaluation - How to Guide.pdf
Heuristics Evaluation - How to Guide.pdfHeuristics Evaluation - How to Guide.pdf
Heuristics Evaluation - How to Guide.pdf
 
LGBTQIA Pride Month presentation Template
LGBTQIA Pride Month presentation TemplateLGBTQIA Pride Month presentation Template
LGBTQIA Pride Month presentation Template
 
Practical eLearning Makeovers for Everyone
Practical eLearning Makeovers for EveryonePractical eLearning Makeovers for Everyone
Practical eLearning Makeovers for Everyone
 
一比一原版(Vancouver毕业证书)温哥华岛大学毕业证如何办理
一比一原版(Vancouver毕业证书)温哥华岛大学毕业证如何办理一比一原版(Vancouver毕业证书)温哥华岛大学毕业证如何办理
一比一原版(Vancouver毕业证书)温哥华岛大学毕业证如何办理
 
一比一原版马里兰大学毕业证(UMD毕业证书)如何办理
一比一原版马里兰大学毕业证(UMD毕业证书)如何办理一比一原版马里兰大学毕业证(UMD毕业证书)如何办理
一比一原版马里兰大学毕业证(UMD毕业证书)如何办理
 
International Upcycling Research Network advisory board meeting 4
International Upcycling Research Network advisory board meeting 4International Upcycling Research Network advisory board meeting 4
International Upcycling Research Network advisory board meeting 4
 
NHR Engineers Portfolio 2023 2024 NISHANT RATHI
NHR Engineers Portfolio 2023 2024 NISHANT RATHINHR Engineers Portfolio 2023 2024 NISHANT RATHI
NHR Engineers Portfolio 2023 2024 NISHANT RATHI
 
Getting Data Ready for Culture Hack by Neontribe
Getting Data Ready for Culture Hack by NeontribeGetting Data Ready for Culture Hack by Neontribe
Getting Data Ready for Culture Hack by Neontribe
 
Introduction to User experience design for beginner
Introduction to User experience design for beginnerIntroduction to User experience design for beginner
Introduction to User experience design for beginner
 
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
 
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
 

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