![Page 1: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/1.jpg)
Design Patterns:
Elements of Reusable Object – Oriented Software
Web Apps and Services
![Page 2: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/2.jpg)
Design Pattern Introduction: why a design pattern? History of design pattern What is a design pattern How we describe design pattern Classification of desing pattern Examples of design pattern Conclusions Bibliography
![Page 3: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/3.jpg)
Why a Design Pattern
Reusability:one of Wasserman’s rules(1996)for an efficient and actual SE discipline
Helping new designers to have a more flexible and reusable design
Improving the documentation and maintenance of existing system by furnishing an explicit specification of class and object interactions and their intent
![Page 4: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/4.jpg)
History of Design Pattern
1979:Christopher Alexander,architect, “The Timeless Way of Building”,Oxford Press
1987:OOPSLA (Object Oriented Programming System),Orlando, presentation of design pattern to the community OO by Ward Cunningham and Kent Beck
1995:Group of Four alias E.Gamma, R.Helm,R.Johnson and J.Vlissides : “Design Pattern:Elements of Reusable OO software”
![Page 5: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/5.jpg)
What Is a Design Pattern A design pattern is a descriptions of communicating
objects and classes that are customized to solve a general design problem in a particular context
A pattern is made by four elements: name problem solution consequences
![Page 6: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/6.jpg)
Name of Design Pattern
Describe a design problems and its solutions in a word or two
Used to talk about design pattern with our colleagues
Used in the documentation Increase our design vocabulary Have to be coherent and evocative
![Page 7: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/7.jpg)
Problem
Describes when to apply the patterns
Explains the problem and its context
Sometimes include a list of conditions that must be met before it makes sense to apply the pattern
Have to occurs over and over again in our environment
![Page 8: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/8.jpg)
Solution
Describes the elements that make up the design, their relationships, responsibilities and collaborations
Does not describe a concrete design or implementation
Has to be well proven in some projects
![Page 9: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/9.jpg)
Consequences
Results and trade-offs of applying the pattern
Helpful for describe design decisions, for evaluating design alternatives
Benefits of applying a pattern
Impacts on a system’s flexibility,extensibility or portability
![Page 10: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/10.jpg)
Description of Design Pattern
Pattern name and classificationPattern name and classification• contains the essence of pattern succinctly
• Become part of your design vocabulary
IntentIntent• What does the pattern do ?
• What particular problem does it address ?
![Page 11: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/11.jpg)
Description of Design Pattern
MotivationMotivation• Illustrate a design problem and how the class and the object structures
solve the problem
ApplicabilityApplicability• In which situations the pattern can be applied?
• How can you recognize these situations?
![Page 12: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/12.jpg)
Description of Design Pattern
StructureStructure• Graphical representation of the classes and their collaborations in the
pattern
ParticipantsParticipants• Class
• Objects
• Responsibilities
![Page 13: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/13.jpg)
Description of Design Pattern CollaborationsCollaborations
• How the participants collaborate to carry out their responsibilities
ConsequencesConsequences• How does the pattern support its objectives?
• What are the trade-offs and results of using the pattern?
![Page 14: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/14.jpg)
Description of Design Pattern
ImplementationImplementation
Sample CodeSample Code
Known UsesKnown Uses• Examples of the pattern found in real systems
![Page 15: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/15.jpg)
Description of Design Pattern
Related PatternsRelated Patterns• What design patterns are closely related to this one?
• What are the important differences?
![Page 16: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/16.jpg)
By purpose and by scope
Creational patternsCreational patterns
• Abstract the instantiation process
• Make a system independent to its realization
• Class Creational use inheritance to vary the instantiated classes
• Object Creational delegate instantiation to an another object
Classification of Design Pattern
![Page 17: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/17.jpg)
Classification of Design Pattern Structural patternsStructural patterns
• Class Structural patterns concern the aggregation of classes to form largest structures
• Object Structural pattern concern the aggregation of objects to form largest structures
![Page 18: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/18.jpg)
Classification of Design Pattern Behavioral patternsBehavioral patterns
• Concern with algorithms and assignment of responsibilities between objects
• Describe the patterns of communication between classes or objects
• Behavioral class pattern use inheritance to distribute behavior between classes
• Behavioral object pattern use object composition to distribute behavior between classes
![Page 19: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/19.jpg)
Classification of design patterns ( a view)
InterpreterAdapterFactory Method
Visitor
Strategy
StateProxy
ObserverFlyweight
MementoFaçade
MediatorDecoratorSingleton
IteratorCompositePrototype
CommandBridgeBuilder
Chain of Responsibility
AdapterAbstract Factory
Template Method
BehavioralBehavioralStructuralStructuralCreationalCreational
ObjectObject
ClassClass
purpose
Scope
![Page 20: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/20.jpg)
Creational Pattern
Singleton• Ensure a class only has one instance
• Provide a global point of access to it
Abstract Factory:• Provide an interface for creating families of related or dependent
objects without specifying their concrete classes
![Page 21: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/21.jpg)
Creational Pattern Factory Method:
• Define an interface for creating an object but let subclasses decide which class to instantiate
• Lets a class defer instantiation to subclasses
Prototype • Specify the kinds of objects to create using a prototypical instance
• Create new objects by copying this prototype
![Page 22: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/22.jpg)
Creational Pattern
Builder:• Separate the construction of a complex object from its representation so
that the same construction process can create different representations
![Page 23: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/23.jpg)
Structural Pattern Composite
• Compose objects into tree structures to represent part-whole hierarchies
• Lets clients treat individual objects and compositions of objects uniformly
Decorator• Attach additional responsibilities to an object dynamically
• Provide a flexible alternative to subclassing for extending functionality
![Page 24: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/24.jpg)
Structural Pattern
Adapter:• Convert the interface of a class into another interface clients expect
• Lets classes work together that couldn’t otherwise because of incompatible interfaces
Bridge:• Decouple an abstraction from its implementation so that the two can
vary independently
![Page 25: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/25.jpg)
Structural Pattern Façade
• Provide a unified interface to a set of interfaces in a subsystem
• Defines an higher-level interface that makes the system easier to use
Flyweight• Use sharing to support large numbers of fine-grained objects
efficiently
![Page 26: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/26.jpg)
Structural Pattern
Proxy• Provide a surrogate or placeholder for another object to control access to it
![Page 27: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/27.jpg)
Behavioral Pattern Iterator
• Provide a way to access the elements of an aggregate object without exposing its representation
Command• Encapsulate a request as an object, thereby letting you parameterize
clients with different requests
![Page 28: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/28.jpg)
Behavioral Pattern Interpreter
• Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language
Mediator• Define an object that encapsulate how a set of objects interact
• Promotes loose coupling by keeping objects from referring to each other explicitly
• Lets you vary their interaction independently
![Page 29: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/29.jpg)
Behavioral Pattern Memento
• Capture and externalize an object’s internal state
Observer• Define a one-to-many dependency between objects so when one of
them change state all its dependents are updated automatically
![Page 30: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/30.jpg)
Behavioral Pattern State
• Allow an object to alter its behavior when its internal state changes
• The object will appear to change its class
Visitor• Represent an operation to be performed on the elements of an object
structure
• Lets you define a new operation without changing the classes of the elements on which operates
![Page 31: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/31.jpg)
Behavioral Pattern Strategy
• Define a family of algorithms
• Encapsulate each one
• Make them interchangeable
• Lets the algorithms vary independently from clients that use it
Chain of responsibilities• Avoid coupling the sender of a request to its receiver by giving more then
one object a chance to handle the request
• Chain the receiving objects and pass the request along the chain until an object handles it
![Page 32: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/32.jpg)
Singleton Pattern Motivation
• we need to have exactly only one instance for a class (ex. Printer spooler)
• Make the class itself responsible for keeping track of its sole instance
• The class provide a way to access the instance
Applicability• There must be only one instance of a class accessible from a well-
known point
![Page 33: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/33.jpg)
Singleton Pattern Structure
Singleton
Static Instance()SingletonOperation()GetSingletonData()
Static uniqueInstanceSingletonData
Return uniqueInstance
![Page 34: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/34.jpg)
Singleton Pattern Participants
• Singleton class
Collaborations• Access only through Singleton’s instance operation
Consequences• Controlled access to sole instance
• Permits refinement of operation and representation
• More flexible than class operations
![Page 35: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/35.jpg)
Example of Singleton use“Lotteria Algebrica” We had to have only one instance for class
Director. We simply solve our problem using Singleton PatternDirector
Static Instance()Given(n_ticket:int):void
Error():void
Static UniqueInstance
![Page 36: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/36.jpg)
Decorator Pattern Motivation
• Add responsibilities to individual object not to an entire class
• conforming the interface of the component decorated
![Page 37: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/37.jpg)
Decorator Pattern Structure Component
Operation()
ConcreteComponent
Operation()
Decorator
Operation()
ConcreteDecoratorAOperation()AddedState
ConcreteDecoratorBOperation()
Addedbehavior
![Page 38: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/38.jpg)
Decorator Pattern Participants
• Component• Define the interface for objects that can have responsibilities added to them
dinamically
• Concrete Component• Defines an object to which additional responsibilities can be attached
• Decorator • Mantains a reference to a Component object and defines an interface that
conforms to Component’s interface
• ConcreteDecorator• Added responsibilities to the component
![Page 39: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/39.jpg)
Decorator Pattern Consequences
• More flexibility than static inheritance
• Avoids feature-laden classes high up in the hierarchy
• A decorator and its component are not identical
• Lots of little objects
![Page 40: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/40.jpg)
Example of Decorator
Motivation• If you have a TextView object that displays text in a Window
• TextView has no scroll bars by default
• TextView has no bord by default …
![Page 41: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/41.jpg)
Example of Decorator Structure
aBorderDecorator
component
aScrollDecorator
component
aTextView
![Page 42: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/42.jpg)
Example of Decorator StructureVisualComponent
Draw()
TextView
Draw()
Decorator
Draw()
ScrollDecorator BorderDecorator
Draw()ScrollTo()
ScrollPosition
Draw()DrawBorder()
BorderWidth
![Page 43: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/43.jpg)
Iterator Pattern Also know as
• Cursor
Motivation• Provide more way to access to the elements of an aggregate
List
Count()Append(Element)Remove(Element)
…
ListIterator
First()Next()
IsDone()CurrentItem
Index
![Page 44: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/44.jpg)
Iterator Pattern
Applicability• Access an aggregate object’s contents
• Support multiple traversals of aggregate objects
• Provide an uniform interface
![Page 45: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/45.jpg)
Iterator Pattern StructureAggregate
CreateIterator()
ConcreteAggregate
CreateIterator()
Iterator
First()Next()
IsDone()CurrentItem()
ConcreteIterator
Return newConcreteIterator(This)
Client
![Page 46: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/46.jpg)
Iterator Pattern Participants
• Iterator• Defines interfaces for accessing elements
• ConcreteIterator• Implements the Iterator interface
• Keeps track of the current position in the traversal of the aggregate
• Aggregate• Defines an interface for creating an Iterator object
• Concrete Aggregate• Implements the Iterator creation interface
• Return an instance of ConcreteIterator
![Page 47: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/47.jpg)
Iterator Pattern Consequences
• Simplify the Aggregate interface
• More that one traversal can be pending on an aggregate
• Variations in the traversal of an aggregate
![Page 48: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/48.jpg)
Patterns and Frameworks
Design patterns are more abstract than frameworks Design patterns are smaller architectural elements than
frameworks Design patterns are less specialized than frameworks Framework is executable software, design pattern
represent knowledge about software Frameworks are the physical realization of one or more
software pattern solutions,pattern are instructions for how to implement those solutions
Set of cooperating classes that make up reusable design for a specific class of software. Provides
architectural guidance by partitioning the design into abstract classes and defining their responsibilities
and collaborations.A developer customizes a framework to a particular application by subclassing and composing instances
of framework classes.
![Page 49: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/49.jpg)
Conclusions At present, the software community is using
pattern largely for software architecture and design
More recently the software community is using pattern also for software development processes and o organizations
Several object-oriented software design notations/methods have added support for the modeling and representation of design patterns
![Page 50: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/50.jpg)
BibliographyFor the pattern catalogue: Erich Gamma, Richard Helm,Ralph Johnson,John
Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. ADDISON-WESLEY 1995
For the pattern origin: Cristopher Alexander. The Timeless Way of Building.
Oxford Press 1979For “Night Patterns”: James O.Coplien,Douglas C.Schmidt.Pattern
Languages of Program Design. ADDISON-WESLEY 1995
![Page 51: Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services](https://reader031.vdocuments.us/reader031/viewer/2022013101/5697bfdd1a28abf838cb1c85/html5/thumbnails/51.jpg)
More InformationHere you can find some example in Italian of design patterns. http://www.ugolandini.net/PatternsHome.htmlHere you can find some general information about patterns http://www.mokabyte.it/1998/10/pattern.htm http://www.c2.comA complete Web site about pattern http://www.enteract.com/~bradapp/docs/patterns-
intro.html