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

Software Design Patterns - Encsusers.encs.concordia.ca/~gregb/home/PDF/se_design...Software Design Patterns \Gang of Four" Book 1994 Erich Gamma, Richard Helm, Ralph Johnson, John

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Software Design Patterns

Greg Butler

Computer Science and Software EngineeringConcordia University, Montreal, Canada

Email: [email protected]

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.

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

Template for Gang of Four Book

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

SolutionStructureParticipantsCollaborationsConsequencesImplementation

OtherSample Code and UsageKnown UsesRelated Patterns

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?

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?

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?

Organization of Design Patterns

Iterator Pattern

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

Example — Iterator Pattern

Strategy Pattern

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

Strategy Pattern — Example

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

Structure — Composite Pattern

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.

Facade PatternStructure

Compiler Example

Proxy Pattern

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

Proxy Pattern — Example

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.

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.