22
Software Design Patterns Greg Butler Computer Science and Software Engineering Concordia University, Montreal, Canada Email: [email protected]

Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

  • Upload
    others

  • View
    19

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Software Design Patterns

Greg Butler

Computer Science and Software EngineeringConcordia University, Montreal, Canada

Email: [email protected]

Page 2: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Software Design Patterns

“Gang of Four” Book 1994

Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides,Design Patterns: Elements of Reusable Object-Oriented Software,Addison-Wesley, 1994.

What is a Design PatternA design pattern describes

a commonly-recurring structure of communicating componentsthat solves

a general design problem within a particular context.

What is a Design Pattern

An example of “best practice” in OO design.

What is a Design Pattern

Design patterns are a “vocabulary” for designersto better communicate design ideas.

Page 3: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Description of a Design Pattern

Essentials for Design Pattern Description

problem that the pattern addresses

context in which the pattern is used

the suggested solution

consequences of choosing that solution, e.g. strength and weakness

Page 4: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Template for Gang of Four Book

Name, Motivation, Context, ProblemPattern Name (Scope, Purpose)IntentAlso Known AsMotivationApplicability

SolutionStructureParticipantsCollaborationsConsequencesImplementation

OtherSample Code and UsageKnown UsesRelated Patterns

Page 5: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Name, Motivation, Context, ProblemPattern Name (Scope, Purpose)The pattern’s name conveys the essence of the pattern succinctly. Agood name is vital, because it will become part of your design vocabulary.

IntentA short statement that answers the following questions: What does thedesign pattern do? What is its rationale and intent? What particulardesign issue or problem does it address?

Also Known AsOther well-known names for the pattern, if any.

MotivationA scenario that illustrates a design problem and how the class and objectstructures in the pattern solve the problem. The scenario will help youunderstand the more abstract description of the pattern that follows.

ApplicabilityWhat are the situations in which the design pattern can be applied?What are examples of poor designs that the pattern can address? Howcan you recognize these situations?

Page 6: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Solution

Structure

ParticipantsThe classes and/or objects participating in the design pattern and theirresponsibilities.

CollaborationsHow the participants collaborate to carry out their responsibilities.

ConsequencesHow does the pattern support its objectives? What are the trade-offs andresults of using the pattern? What aspect of system structure does it letyou vary independently?

ImplementationWhat pitfalls, hints, or techniques should you be aware of whenimplementing the pattern? Are there language-specific issues?

Page 7: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Other

Sample Code and UsageCode fragments that illustrate how you might implement the pattern inC++ or Smalltalk.

Known UsesExamples of the pattern found in real systems. We include at least twoexamples from different domains.

Related PatternsWhat design patterns are closely related to this one? What are theimportant differences? With which other patterns should this one beused?

Page 8: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Organization of Design Patterns

Page 9: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented
Page 10: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented
Page 11: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Iterator Pattern

Intent: An Iterator provides a way to access the elements of anaggregate object sequentially without exposing its underlyingrepresentation.

Page 12: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Example — Iterator Pattern

Page 13: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Strategy Pattern

Intent: A strategy object encapsulates an algorithm, so that thealgorithm can vary independently from the clients that use it.

Page 14: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Strategy Pattern — Example

Page 15: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Composite PatternIntent: Composite lets clients treat individual objects andcompositions of objects uniformly.

Page 16: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Structure — Composite Pattern

Page 17: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Facade Pattern

Intent: A facade provides a uniform interface to a set of interfacesin a subsystem. The Facade defines a higher-level interface thatmakes the subsystem easier to use.

Page 18: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Facade PatternStructure

Compiler Example

Page 19: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Proxy Pattern

Intent: A proxy is an object used as a substitute or placeholder foran object in order to control access to it.

Page 20: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Proxy Pattern — Example

Page 21: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Observer PatternIntent: Define a one-to-many dependency between objects so thatwhen one object changes state, all its dependents are notified andupdated automatically.that is, a subscription service, or event notification service.

Page 22: Software Design Patterns - EncsSoftware Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented

Command PatternIntent: A Command object encapsulates a request as an object,thereby allowing you to parameterize clients with differentrequests, queue or log requests, and support undoable operations.