SlideShare a Scribd company logo
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Design Patterns
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Acknowledgements
Materials based on a number of sources
– D. Levine and D. Schmidt
– R. Helm
– Gamma et al
– S. Konrad
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Motivation
• Developing software is hard
• Designing reusable software is more
challenging
– finding good objects and abstractions
– flexibility, modularity, elegance reuse
– takes time for them to emerge, trial and
error
• Successful designs do exist
– exhibit recurring class and object
structures
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Design Pattern
• Describes recurring design structure
– names, abstracts from concrete designs
– identifies classes, collaborations,
responsibilities
– applicability, trade-offs, consequences
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Becoming a Chess Master
• First learn rules and physical requirements
– e.g., names of pieces, legal movements, chess
board geometry and orientation, etc.
• Then learn principles
– e.g, relative value of certain pieces, strategic value
of center squares, power of a threat, etc.
• To become a Master of chess, one must
study the games of other masters
– These games contain patterns that must be
understood, memorized, and applied repeatedly.
• There are hundreds of these patterns
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Becoming a Software Design Master
• First learn rules
– e.g., algorithms, data structures, and languages of
software.
• Then learn principles
– e.g., structured programming, modular
programming, object-oriented programming, etc.
• To become a Master of SW design, one must
study the designs of other masters
– These designs contain patterns that must be
understood, memorized, and applied repeatedly.
• There are hundreds of these patterns
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Design Patterns
• Design patterns represent solutions to problems that
arise when developing software within a particular
context
– “Patterns == problem/solution pairs in a context”
• Patterns capture the static and dynamic structure and
collaboration among key participants in software
designs
– Especially good for describing how and why to resolve non-
functional issues
• Patterns facilitate reuse of successful software
architectures and designs.
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Design Patterns: Applications
• Wide variety of application domains:
– drawing editors, banking, CAD, CAE,
cellular network management, telecomm
switches, program visualization
• Wide variety of technical areas:
– user interface, communications, persistent
objects, O/S kernels, distributed systems
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
“Each pattern describes a problem which occurs over and over
again in our environment and then describes the core of the
solution to that problem, in such a way that you can use this
solution a million times over, without ever doing it in the same
way twice”
Christopher Alexander, A Pattern Language, 1977
What Is a Design Pattern (1)
Context: City Planning and Building architectures
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
A pattern has 4 essential elements:
• Pattern name
• Problem
• Solution
• Consequences
What Is a Design Pattern (2)
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
• A handle used to describe:
• a design problem,
• its solutions and
• its consequences
• Increases design vocabulary
• Makes it possible to design at a higher level of abstraction
• Enhances communication
But finding a good name is often difficult
Pattern Name
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
• Describes when to apply the pattern
• Explains the problem and its context
• Might describe specific design problems or class
or object structures
• May contain a list of conditions
• must be met
• before it makes sense to apply the pattern
Problem
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
• Describes the elements that make up the
• design,
• their relationships,
• responsibilities and
• collaborations
• Does not describe specific concrete
implementation
• Abstract description of design problems and
• how the pattern solves it
Solution
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
• Results and trade-offs of applying the pattern
• Critical for:
• evaluate design alternatives and
• understand costs and
• understand benefits of applying the pattern
• Includes the impacts of a pattern on a system’s:
• flexibility,
• extensibility
• portability
Consequences
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
• Designs that can be encoded in classes and reused as is
(i.e. linked lists, hash tables)
• Complex domain-specific designs (for an entire
application or subsystem)
They are:
“Descriptions of communicating objects and classes that are
customized to solve a general design problem in a particular
context.”
Design Patterns Are NOT
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
• Object-Oriented Programming Languages:
• more amenable to implementing design
patterns
• Procedural languages: need to define
• Inheritance,
• Polymorphism and
• Encapsulation
Where Design Patterns
Are Used
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
• Graphical notation is not sufficient
• In order to reuse design decisions,
• alternatives and trade-offs that led to the
decisions are important
• Concrete examples are also important
How to Describe Design Patterns
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
A Design Pattern
• Describes a recurring design structure
– names, abstracts from concrete designs
– identifies classes, collaborations,
responsibilities
– applicability, trade-offs, consequences
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Observer Pattern
• Intent:
– Define a one-to-many dependency between objects
so that when one object changes state, all its
dependents are notified and updated automatically
• Key forces:
– There may be many observers
– Each observer may react differently to the same
notification
– The subject should be as decoupled as possible from
the observers
• allow observers to change independently of the
subject
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Subject
notify()
attach(observer)
detach(observer)
Structure of the Observer Pattern
Concrete
Observer
update()
Observer
update()
*
Foreach o in observers loop
o.update()
End loop
subject -> get_state()
return subject_state
Concrete
Subject
subject state
get state()
*
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Collaboration in the Observer Pattern
set_state()
notify()
update()
get_state()
update()
get_state()
Concrete
Subject
Concrete
Observer1
Concrete
Observer2
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Design Pattern Descriptions
• Main Parts:
– Name and Classification (see table in two more slides)
– Intent: Problem and Context
– Also known as (other well-known names)
– Motivation: scenario illustrates a design problem
– Applicability: situations where pattern can be applied
– Structure: graphical representation of classes (class diagram, interaction
diagram)
– Participants: objects/classes and their responsibilities
– Collaborations: how participants collaborate
– Consequences: trade-offs and results
– Implementation: pitfalls, hints, techniques for coding; language-specific issues
– Sample Code
– Known Uses: examples of pattern in real systems
– Related Patterns: closely related; what are diffs.
• Pattern descriptions are often independent of programming language or
implementation details
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Design Pattern Space
• Creational patterns:
– Deal with initializing and configuring classes and
objects
• Structural patterns:
– Deal with decoupling interface and implementation
of classes and objects
– Composition of classes or objects
• Behavioral patterns:
– Deal with dynamic interactions among societies of
classes and objects
– How they distribute responsibility
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Creational Structural Behavioral
Factory Method
Abstract Factory
Prototype
Singleton
Builder
Adapter (class)
Adapter (object)
Bridge
Flyweight
Decorator
Proxy
Composite
Facade
Template Method
Chain of Responsibility
Command
Iterator
Mediator
Memento
Observer
State
Strategy
Interpreter
Visitor
Categorize Design Patterns
Purpose
Class
Object
Scope
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Categorization Terms
• Scope: domain over which a pattern applies
– Class Scope:
• relationships between base classes and their
subclasses
• Static semantics
– Object Scope:
• relationships between peer objects
• Can be changed at runtime
• More dynamic
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Purpose of Patterns
• Creational:
– Class: defer some part of object creation to
subclasses
– Object: Defer object creation to another object
• Structural:
– Class: use inheritance to compose classes
– Object: describe ways to assemble classes
• Behavioral:
– Class: use inheritance to describe algs and flow of
control
– Object: describes how a group of objects
cooperate to perform task that no single object
can complete
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Terminology
• Signature:
– operation name,
– objects taken as parameters, and
– operation’s return value
• Interface:
– Set of all signatures defined by an object’s
operations
– Characterizes the complete set of requests that
can be sent to object.
– Key to OO technology
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Creational Patterns
• Factory Method:
– method in a derived class creates associations
• Abstract Factory:
– Factory for building related objects
• Builder:
– Factory for building complex objects incrementally
• Prototype:
– Factory for cloning new instances from a prototype
• Singleton:
– Factory for a singular (sole) instance
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Structural Patterns:
• Adapter:
– Translator adapts a
server interface for a
client
• Bridge:
– Abstraction for binding
one of many
implementations
• Composite:
– Structure for building
recursive aggregations
• Decorator:
– Decorator extends an
object transparently
• Facade:
– simplifies the interface for
a subsystem
• Flyweight:
– many fine-grained
objects shared efficiently.
• Proxy:
– one object approximates
another
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Behavioral Patterns
• Chain of Responsibility
– request delegated to the
responsible service
provider
• Command:
– request is first-class
object
• Iterator:
– Aggregate elements are
accessed sequentially
• Interpreter:
– language interpreter for a
small grammar
• Mediator:
– coordinates interactions
between its associates
• Memento:
– snapshot captures and
restores object states
privately
• Observer:
– dependents update
automatically when
subject changes
• State:
– object whose behavior
depends on its state
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Behavior Patterns (more)
• Strategy:
– Abstraction for selecting one of many algorithms
• Template Method:
– algorithm with some steps supplied by a derived class
• Visitor:
– operations applied to elements of a heterogeneous object
structure
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
When to Use Patterns
• Solutions to problems that recur with variations
– No need for reuse if problem only arises in one context
• Solutions that require several steps:
– Not all problems need all steps
– Patterns can be overkill if solution is a simple linear set of
instructions
• Solutions where the solver is more interested in the
existence of the solution than its complete derivation
– Patterns leave out too much to be useful to someone who
really wants to understand
• They can be a temporary bridge
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
What Makes it a Pattern
A Pattern must:
– Solve a problem
• must be useful
– Have a context
• describe where the
solution can be used
– Recur
• relevant in other
situations
– Teach
• provide sufficient
understanding to tailor
the solution
– have a name
• referenced consistently
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Class Scope
• Class Creational: abstract how objects are
instantiated
– hide specifics of creation process
– may want to delay specifying a class name explicitly
when instantiating an object
– just want a specific protocol
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Example Class Creational
• Use of Factory Method: instantiate members in base
classes with objects created by subclasses.
• Abstract Application class: create application-specific
documents conforming to particular Document type
• Application instantiates these Document objects by
calling the factory method DoMakeDocument
• Method is overridden in classes derived from
Application
• Subclass DrawApplication overrides
DoMakeDocument to return a DrawDocument object
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Factory Method
Product
ConcreteProduct
+FactorMethod()
+AnOperation()
Creator
+FactoryMethod()
ConcreteCreator
product=FactoryMethod()
return new ConcreteProduct
+Open()
+Close()
+Save()
+Revert()
Document
DrawDocument
+CreateDocument()
+New Document()
+OpenDocument()
Application
+CreateDocument()
DrawApplication
Document* doc = CreateDocument();
docs.Add(doc);
doc->Open();
return new Draw Document
-docs
1
*
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Class Structural
• Class Structural: use inheritance to compose protocols
or code
• Example:
– Adapter Pattern: makes one interface (Adaptee’s) conform
to another --> uniform abstraction of different interfaces.
– Class Adapter inherits privately from an Adaptee class.
– Adapter then expresses its interface in terms of the
Adaptee’s.
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Adapter Example
Client Target
Request()
Adaptee
SpecificRequest()
Adapter
Request()
Drawing
Editor
TextView
GetExtent()
TextShape
BoundingBox()
CreateManip()
Shape
BoundingBox()
CreateManip()
Line
BoundingBox()
CreateManip()
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Class Behavioral
• Class Behavioral: capture how classes
cooperate with their subclasses to satisfy
semantics.
– Template Method: defines algorithms step by step.
– Each step can invoke an abstract method (that must
be defined by the subclass) or a base method.
– Subclass must implement specific behavior to provide
required services
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Object Scope
• Object Patterns all apply various forms of
non-recursive object composition.
• Object Composition: most powerful form of
reuse
• Reuse of a collection of objects is better
achieved through variations of their
composition, rather than through subclassing.
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Object Creational
• Creational Object Patterns: abstract how sets of objects are
created
• Example:
– Abstract Factory: create “product” objects through generic
interface
• Subclasses may manufacture specialized versions or compositions
of objects as allowed by this generic interface
– User Interface Toolkit: 2 types of scroll bars (Motif and Open Look)
• Don’t want to hard-code specific one; an environment variable decides
– Class Kit:
• encapsulates scroll bar creation (and other UI entities);
• an abstract factory that abstracts the specific type of scroll bar to
instantiate
• Subclasses of Kit refine operations in the protocol to return specialized
types of scroll bars.
• Subclasses MotifKit and OpenLookKit each have scroll bar operation.
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Kit: Abstract Factory
WidgetFactory
CreateScrollBar()
CreateWindow()
OpenWidgetFactory
CreateScrollBar()
CreateWindow()
MotifWidgetFactory
CreateScrollBar()
CreateWindow()
Window
MotifWindow OpenWindow
Scrollbar
MotifScroll OpenScroll
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Object Structural
• Object Structural: Describe ways to assemble objects
to realize new functionality
– Added flexibility inherent in object composition due to ability
to change composition at run-time
– not possible with static class composition.
• Example:
– Proxy: acts as convenient surrogate or placeholder
for another object.
• Remote Proxy: local representative for object in a different
address space
• Virtual Proxy: represent large object that should be
loaded on demand
• Protected Proxy: protect access to the original object
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Proxy Example
DocumentEditor
Graphic
Draw()
GetExtent()
Store()
Load()
Image
imageImp
extent
Draw()
GetExtent()
Store()
Load()
Graphic
fileName
extent
Draw()
GetExtent()
Store()
Load()
image
*
Client
Subject
Draw()
GetExtent()
Store()
Load()
RealSubject
Request()
Proxy
Request()
realSubject
*
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Object Behavioral
• Object Behavioral: Describe how a group of peer objects
cooperate to perform a task that can be carried out by
itself.
• Example:
– Strategy Pattern: objectifies an algorithm
– Text Composition Object: support different line breaking algorithms
• Don’t want to hard-code all algs into text composition class/subclasses
– Objectify different algs and provide them as Compositor
subclasses (contains criteria for line breaking strategies)
– Interface for Compositors defined by Abstract Compositor Class
• Derived classes provide different layout strategies (simple line breaks,
left/right justification, etc.)
– Instances of Compositor subclasses couple with text composition at
run-time to provide text layout
– Whenever text composition has to find line breaks, forwards the
responsibility to its current Compositor object.
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Object Behavioral Example
– Iterator Pattern: Iteration over a recursive structure
– Traversal strategies for a given structure:
• Extract and implement ea traversal strategy in an Iterator class.
• Iterators objectify traversal algs over recursive structures
• Different iterators can implement pre-order, in-order, post-order
traversals
• Require nodes in structure to provide services to enumerate
their sub-structures
• Don’t need to hard-code traversal algs throughout classes of
objects in composite structure
• Iterators may be replaced at run-time to provide alternate
traversals.
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Object Structural Example
– Facade Pattern (Wrapper): describes how to flexibly
attach additional properties and services to an
object
• Can be nested recursively; compose more complex
object structures
– User Interface Example:
• A Facade containing a single UI component can add
decorations such as border, shadows,scroll bars, or
services (scrolling and zooming)
• Facade must conform to interface of its wrapped
component and forward messages to it
• Facade can perform additional actions (e.g., drawing
border around component) either before or after
forwarding a message.
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Benefits of Design Patterns
• Design patterns enable large-scale reuse of
software architectures
– also help document systems
• Patterns explicitly capture expert knowledge
and design tradeoffs
– make it more widely available
• Patterns help improve developer
communication
– Pattern names form a vocabulary
• Patterns help ease the transition to OO
technology
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Drawbacks to Design Patterns
• Patterns do not lead to direct code reuse
• Patterns are deceptively simple
• Teams may suffer from pattern overload
• Patterns are validated by experience and
discussion rather than by automated testing
• Integrating patterns into a SW development
process is a human-intensive activity.
R
R
R
CSE870: Advanced Software Engineering (Design Patterns): Cheng
Suggestions for Effective Pattern Use
• Do not recast everything as a pattern
– Instead, develop strategic domain patterns and reuse
existing tactical patterns
• Institutionalize rewards for developing patterns
• Directly involve pattern authors with application
developers and domain experts
• Clearly document when patterns apply and do not
apply
• Manage expectations carefully.

More Related Content

PDF
Graphical programming
PPTX
Functional Programming.pptx
PPTX
Mutable and immutable classes
PPTX
Programming paradigm
PDF
AlgorithmAndFlowChart.pdf
PDF
C++ Standard Template Library
PPTX
Modular programming
PPT
11 constructors in derived classes
Graphical programming
Functional Programming.pptx
Mutable and immutable classes
Programming paradigm
AlgorithmAndFlowChart.pdf
C++ Standard Template Library
Modular programming
11 constructors in derived classes

What's hot (20)

PPT
Lecture 5 - Structured Programming Language
PPT
Visual programming lecture
PPTX
Introduction to c programming
PPTX
Web browser architecture.pptx
PPTX
Lesson 6 php if...else...elseif statements
PPTX
Basic knowledge on html and dhtml
PDF
Lecture 01 introduction to compiler
PPTX
Essentials of Multithreaded System Programming in C++
ODP
Java Web Programming [1/9] : Introduction to Web Application
PPTX
Preprocessor directives in c language
PPTX
Introduction to programming
PDF
Introduction to Bootstrap
PPTX
Language design and translation issues
PPTX
Intro to front end development Basic
PPTX
Constructor and Types of Constructors
PDF
String operation
PPT
Component based models and technology
PPTX
All You Need to Know About Java – Advantages and Disadvantages
Lecture 5 - Structured Programming Language
Visual programming lecture
Introduction to c programming
Web browser architecture.pptx
Lesson 6 php if...else...elseif statements
Basic knowledge on html and dhtml
Lecture 01 introduction to compiler
Essentials of Multithreaded System Programming in C++
Java Web Programming [1/9] : Introduction to Web Application
Preprocessor directives in c language
Introduction to programming
Introduction to Bootstrap
Language design and translation issues
Intro to front end development Basic
Constructor and Types of Constructors
String operation
Component based models and technology
All You Need to Know About Java – Advantages and Disadvantages
Ad

Similar to 08-design-patterns.ppt (20)

PPTX
Design Patterns- Course for students .pptx
PPTX
Cs 1023 lec 9 design pattern (week 2)
PPTX
Design pattern
PDF
Generation of Automatic Code using Design Patterns
PPT
CS6201 Software Reuse - Design Patterns
PDF
Design pattern
PDF
software engineering Design Patterns.pdf
PDF
Patterns Overview
PDF
Cse 6007 fall2012
PPT
Class (1)
PPT
Stoop 430-design patternsintro
DOCX
Design pattern application
PPT
Introduction to design patterns
PPT
Design Patterns
PPTX
Design patterns
PPTX
Final sdp ppt
PPT
Chapter 4_Introduction to Patterns.ppt
PPT
Chapter 4_Introduction to Patterns.ppt
PDF
Module 2 design patterns-2
PPT
Design pattern & categories
Design Patterns- Course for students .pptx
Cs 1023 lec 9 design pattern (week 2)
Design pattern
Generation of Automatic Code using Design Patterns
CS6201 Software Reuse - Design Patterns
Design pattern
software engineering Design Patterns.pdf
Patterns Overview
Cse 6007 fall2012
Class (1)
Stoop 430-design patternsintro
Design pattern application
Introduction to design patterns
Design Patterns
Design patterns
Final sdp ppt
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
Module 2 design patterns-2
Design pattern & categories
Ad

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Mushroom cultivation and it's methods.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Hybrid model detection and classification of lung cancer
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Encapsulation theory and applications.pdf
Unlocking AI with Model Context Protocol (MCP)
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Enhancing emotion recognition model for a student engagement use case through...
Web App vs Mobile App What Should You Build First.pdf
TLE Review Electricity (Electricity).pptx
Mushroom cultivation and it's methods.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
1 - Historical Antecedents, Social Consideration.pdf
A comparative analysis of optical character recognition models for extracting...
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Agricultural_Statistics_at_a_Glance_2022_0.pdf
A novel scalable deep ensemble learning framework for big data classification...
Building Integrated photovoltaic BIPV_UPV.pdf
A comparative study of natural language inference in Swahili using monolingua...
MIND Revenue Release Quarter 2 2025 Press Release
NewMind AI Weekly Chronicles - August'25-Week II
Hybrid model detection and classification of lung cancer
Programs and apps: productivity, graphics, security and other tools
Encapsulation theory and applications.pdf

08-design-patterns.ppt

  • 1. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Design Patterns
  • 2. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Acknowledgements Materials based on a number of sources – D. Levine and D. Schmidt – R. Helm – Gamma et al – S. Konrad
  • 3. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Motivation • Developing software is hard • Designing reusable software is more challenging – finding good objects and abstractions – flexibility, modularity, elegance reuse – takes time for them to emerge, trial and error • Successful designs do exist – exhibit recurring class and object structures
  • 4. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Design Pattern • Describes recurring design structure – names, abstracts from concrete designs – identifies classes, collaborations, responsibilities – applicability, trade-offs, consequences
  • 5. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Becoming a Chess Master • First learn rules and physical requirements – e.g., names of pieces, legal movements, chess board geometry and orientation, etc. • Then learn principles – e.g, relative value of certain pieces, strategic value of center squares, power of a threat, etc. • To become a Master of chess, one must study the games of other masters – These games contain patterns that must be understood, memorized, and applied repeatedly. • There are hundreds of these patterns
  • 6. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Becoming a Software Design Master • First learn rules – e.g., algorithms, data structures, and languages of software. • Then learn principles – e.g., structured programming, modular programming, object-oriented programming, etc. • To become a Master of SW design, one must study the designs of other masters – These designs contain patterns that must be understood, memorized, and applied repeatedly. • There are hundreds of these patterns
  • 7. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Design Patterns • Design patterns represent solutions to problems that arise when developing software within a particular context – “Patterns == problem/solution pairs in a context” • Patterns capture the static and dynamic structure and collaboration among key participants in software designs – Especially good for describing how and why to resolve non- functional issues • Patterns facilitate reuse of successful software architectures and designs.
  • 8. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Design Patterns: Applications • Wide variety of application domains: – drawing editors, banking, CAD, CAE, cellular network management, telecomm switches, program visualization • Wide variety of technical areas: – user interface, communications, persistent objects, O/S kernels, distributed systems
  • 9. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng “Each pattern describes a problem which occurs over and over again in our environment and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it in the same way twice” Christopher Alexander, A Pattern Language, 1977 What Is a Design Pattern (1) Context: City Planning and Building architectures
  • 10. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng A pattern has 4 essential elements: • Pattern name • Problem • Solution • Consequences What Is a Design Pattern (2)
  • 11. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng • A handle used to describe: • a design problem, • its solutions and • its consequences • Increases design vocabulary • Makes it possible to design at a higher level of abstraction • Enhances communication But finding a good name is often difficult Pattern Name
  • 12. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng • Describes when to apply the pattern • Explains the problem and its context • Might describe specific design problems or class or object structures • May contain a list of conditions • must be met • before it makes sense to apply the pattern Problem
  • 13. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng • Describes the elements that make up the • design, • their relationships, • responsibilities and • collaborations • Does not describe specific concrete implementation • Abstract description of design problems and • how the pattern solves it Solution
  • 14. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng • Results and trade-offs of applying the pattern • Critical for: • evaluate design alternatives and • understand costs and • understand benefits of applying the pattern • Includes the impacts of a pattern on a system’s: • flexibility, • extensibility • portability Consequences
  • 15. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng • Designs that can be encoded in classes and reused as is (i.e. linked lists, hash tables) • Complex domain-specific designs (for an entire application or subsystem) They are: “Descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.” Design Patterns Are NOT
  • 16. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng • Object-Oriented Programming Languages: • more amenable to implementing design patterns • Procedural languages: need to define • Inheritance, • Polymorphism and • Encapsulation Where Design Patterns Are Used
  • 17. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng • Graphical notation is not sufficient • In order to reuse design decisions, • alternatives and trade-offs that led to the decisions are important • Concrete examples are also important How to Describe Design Patterns
  • 18. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng A Design Pattern • Describes a recurring design structure – names, abstracts from concrete designs – identifies classes, collaborations, responsibilities – applicability, trade-offs, consequences
  • 19. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng
  • 20. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Observer Pattern • Intent: – Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically • Key forces: – There may be many observers – Each observer may react differently to the same notification – The subject should be as decoupled as possible from the observers • allow observers to change independently of the subject
  • 21. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Subject notify() attach(observer) detach(observer) Structure of the Observer Pattern Concrete Observer update() Observer update() * Foreach o in observers loop o.update() End loop subject -> get_state() return subject_state Concrete Subject subject state get state() *
  • 22. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Collaboration in the Observer Pattern set_state() notify() update() get_state() update() get_state() Concrete Subject Concrete Observer1 Concrete Observer2
  • 23. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Design Pattern Descriptions • Main Parts: – Name and Classification (see table in two more slides) – Intent: Problem and Context – Also known as (other well-known names) – Motivation: scenario illustrates a design problem – Applicability: situations where pattern can be applied – Structure: graphical representation of classes (class diagram, interaction diagram) – Participants: objects/classes and their responsibilities – Collaborations: how participants collaborate – Consequences: trade-offs and results – Implementation: pitfalls, hints, techniques for coding; language-specific issues – Sample Code – Known Uses: examples of pattern in real systems – Related Patterns: closely related; what are diffs. • Pattern descriptions are often independent of programming language or implementation details
  • 24. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Design Pattern Space • Creational patterns: – Deal with initializing and configuring classes and objects • Structural patterns: – Deal with decoupling interface and implementation of classes and objects – Composition of classes or objects • Behavioral patterns: – Deal with dynamic interactions among societies of classes and objects – How they distribute responsibility
  • 25. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Creational Structural Behavioral Factory Method Abstract Factory Prototype Singleton Builder Adapter (class) Adapter (object) Bridge Flyweight Decorator Proxy Composite Facade Template Method Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Interpreter Visitor Categorize Design Patterns Purpose Class Object Scope
  • 26. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Categorization Terms • Scope: domain over which a pattern applies – Class Scope: • relationships between base classes and their subclasses • Static semantics – Object Scope: • relationships between peer objects • Can be changed at runtime • More dynamic
  • 27. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Purpose of Patterns • Creational: – Class: defer some part of object creation to subclasses – Object: Defer object creation to another object • Structural: – Class: use inheritance to compose classes – Object: describe ways to assemble classes • Behavioral: – Class: use inheritance to describe algs and flow of control – Object: describes how a group of objects cooperate to perform task that no single object can complete
  • 28. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Terminology • Signature: – operation name, – objects taken as parameters, and – operation’s return value • Interface: – Set of all signatures defined by an object’s operations – Characterizes the complete set of requests that can be sent to object. – Key to OO technology
  • 29. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Creational Patterns • Factory Method: – method in a derived class creates associations • Abstract Factory: – Factory for building related objects • Builder: – Factory for building complex objects incrementally • Prototype: – Factory for cloning new instances from a prototype • Singleton: – Factory for a singular (sole) instance
  • 30. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Structural Patterns: • Adapter: – Translator adapts a server interface for a client • Bridge: – Abstraction for binding one of many implementations • Composite: – Structure for building recursive aggregations • Decorator: – Decorator extends an object transparently • Facade: – simplifies the interface for a subsystem • Flyweight: – many fine-grained objects shared efficiently. • Proxy: – one object approximates another
  • 31. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Behavioral Patterns • Chain of Responsibility – request delegated to the responsible service provider • Command: – request is first-class object • Iterator: – Aggregate elements are accessed sequentially • Interpreter: – language interpreter for a small grammar • Mediator: – coordinates interactions between its associates • Memento: – snapshot captures and restores object states privately • Observer: – dependents update automatically when subject changes • State: – object whose behavior depends on its state
  • 32. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Behavior Patterns (more) • Strategy: – Abstraction for selecting one of many algorithms • Template Method: – algorithm with some steps supplied by a derived class • Visitor: – operations applied to elements of a heterogeneous object structure
  • 33. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng When to Use Patterns • Solutions to problems that recur with variations – No need for reuse if problem only arises in one context • Solutions that require several steps: – Not all problems need all steps – Patterns can be overkill if solution is a simple linear set of instructions • Solutions where the solver is more interested in the existence of the solution than its complete derivation – Patterns leave out too much to be useful to someone who really wants to understand • They can be a temporary bridge
  • 34. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng What Makes it a Pattern A Pattern must: – Solve a problem • must be useful – Have a context • describe where the solution can be used – Recur • relevant in other situations – Teach • provide sufficient understanding to tailor the solution – have a name • referenced consistently
  • 35. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Class Scope • Class Creational: abstract how objects are instantiated – hide specifics of creation process – may want to delay specifying a class name explicitly when instantiating an object – just want a specific protocol
  • 36. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Example Class Creational • Use of Factory Method: instantiate members in base classes with objects created by subclasses. • Abstract Application class: create application-specific documents conforming to particular Document type • Application instantiates these Document objects by calling the factory method DoMakeDocument • Method is overridden in classes derived from Application • Subclass DrawApplication overrides DoMakeDocument to return a DrawDocument object
  • 37. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Factory Method Product ConcreteProduct +FactorMethod() +AnOperation() Creator +FactoryMethod() ConcreteCreator product=FactoryMethod() return new ConcreteProduct +Open() +Close() +Save() +Revert() Document DrawDocument +CreateDocument() +New Document() +OpenDocument() Application +CreateDocument() DrawApplication Document* doc = CreateDocument(); docs.Add(doc); doc->Open(); return new Draw Document -docs 1 *
  • 38. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Class Structural • Class Structural: use inheritance to compose protocols or code • Example: – Adapter Pattern: makes one interface (Adaptee’s) conform to another --> uniform abstraction of different interfaces. – Class Adapter inherits privately from an Adaptee class. – Adapter then expresses its interface in terms of the Adaptee’s.
  • 39. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Adapter Example Client Target Request() Adaptee SpecificRequest() Adapter Request() Drawing Editor TextView GetExtent() TextShape BoundingBox() CreateManip() Shape BoundingBox() CreateManip() Line BoundingBox() CreateManip()
  • 40. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Class Behavioral • Class Behavioral: capture how classes cooperate with their subclasses to satisfy semantics. – Template Method: defines algorithms step by step. – Each step can invoke an abstract method (that must be defined by the subclass) or a base method. – Subclass must implement specific behavior to provide required services
  • 41. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Object Scope • Object Patterns all apply various forms of non-recursive object composition. • Object Composition: most powerful form of reuse • Reuse of a collection of objects is better achieved through variations of their composition, rather than through subclassing.
  • 42. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Object Creational • Creational Object Patterns: abstract how sets of objects are created • Example: – Abstract Factory: create “product” objects through generic interface • Subclasses may manufacture specialized versions or compositions of objects as allowed by this generic interface – User Interface Toolkit: 2 types of scroll bars (Motif and Open Look) • Don’t want to hard-code specific one; an environment variable decides – Class Kit: • encapsulates scroll bar creation (and other UI entities); • an abstract factory that abstracts the specific type of scroll bar to instantiate • Subclasses of Kit refine operations in the protocol to return specialized types of scroll bars. • Subclasses MotifKit and OpenLookKit each have scroll bar operation.
  • 43. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Kit: Abstract Factory WidgetFactory CreateScrollBar() CreateWindow() OpenWidgetFactory CreateScrollBar() CreateWindow() MotifWidgetFactory CreateScrollBar() CreateWindow() Window MotifWindow OpenWindow Scrollbar MotifScroll OpenScroll
  • 44. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Object Structural • Object Structural: Describe ways to assemble objects to realize new functionality – Added flexibility inherent in object composition due to ability to change composition at run-time – not possible with static class composition. • Example: – Proxy: acts as convenient surrogate or placeholder for another object. • Remote Proxy: local representative for object in a different address space • Virtual Proxy: represent large object that should be loaded on demand • Protected Proxy: protect access to the original object
  • 45. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Proxy Example DocumentEditor Graphic Draw() GetExtent() Store() Load() Image imageImp extent Draw() GetExtent() Store() Load() Graphic fileName extent Draw() GetExtent() Store() Load() image * Client Subject Draw() GetExtent() Store() Load() RealSubject Request() Proxy Request() realSubject *
  • 46. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Object Behavioral • Object Behavioral: Describe how a group of peer objects cooperate to perform a task that can be carried out by itself. • Example: – Strategy Pattern: objectifies an algorithm – Text Composition Object: support different line breaking algorithms • Don’t want to hard-code all algs into text composition class/subclasses – Objectify different algs and provide them as Compositor subclasses (contains criteria for line breaking strategies) – Interface for Compositors defined by Abstract Compositor Class • Derived classes provide different layout strategies (simple line breaks, left/right justification, etc.) – Instances of Compositor subclasses couple with text composition at run-time to provide text layout – Whenever text composition has to find line breaks, forwards the responsibility to its current Compositor object.
  • 47. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Object Behavioral Example – Iterator Pattern: Iteration over a recursive structure – Traversal strategies for a given structure: • Extract and implement ea traversal strategy in an Iterator class. • Iterators objectify traversal algs over recursive structures • Different iterators can implement pre-order, in-order, post-order traversals • Require nodes in structure to provide services to enumerate their sub-structures • Don’t need to hard-code traversal algs throughout classes of objects in composite structure • Iterators may be replaced at run-time to provide alternate traversals.
  • 48. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Object Structural Example – Facade Pattern (Wrapper): describes how to flexibly attach additional properties and services to an object • Can be nested recursively; compose more complex object structures – User Interface Example: • A Facade containing a single UI component can add decorations such as border, shadows,scroll bars, or services (scrolling and zooming) • Facade must conform to interface of its wrapped component and forward messages to it • Facade can perform additional actions (e.g., drawing border around component) either before or after forwarding a message.
  • 49. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Benefits of Design Patterns • Design patterns enable large-scale reuse of software architectures – also help document systems • Patterns explicitly capture expert knowledge and design tradeoffs – make it more widely available • Patterns help improve developer communication – Pattern names form a vocabulary • Patterns help ease the transition to OO technology
  • 50. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Drawbacks to Design Patterns • Patterns do not lead to direct code reuse • Patterns are deceptively simple • Teams may suffer from pattern overload • Patterns are validated by experience and discussion rather than by automated testing • Integrating patterns into a SW development process is a human-intensive activity.
  • 51. R R R CSE870: Advanced Software Engineering (Design Patterns): Cheng Suggestions for Effective Pattern Use • Do not recast everything as a pattern – Instead, develop strategic domain patterns and reuse existing tactical patterns • Institutionalize rewards for developing patterns • Directly involve pattern authors with application developers and domain experts • Clearly document when patterns apply and do not apply • Manage expectations carefully.

Editor's Notes

  • #2: CSE870: Advanced Software Engineering (Cheng)
  • #3: CSE870: Advanced Software Engineering (Cheng)
  • #4: CSE870: Advanced Software Engineering (Cheng)
  • #5: CSE870: Advanced Software Engineering (Cheng)
  • #6: CSE870: Advanced Software Engineering (Cheng)
  • #7: CSE870: Advanced Software Engineering (Cheng)
  • #8: CSE870: Advanced Software Engineering (Cheng)
  • #9: CSE870: Advanced Software Engineering (Cheng)
  • #10: CSE870: Advanced Software Engineering (Cheng)
  • #11: CSE870: Advanced Software Engineering (Cheng)
  • #12: CSE870: Advanced Software Engineering (Cheng)
  • #13: CSE870: Advanced Software Engineering (Cheng)
  • #14: CSE870: Advanced Software Engineering (Cheng)
  • #15: CSE870: Advanced Software Engineering (Cheng)
  • #16: CSE870: Advanced Software Engineering (Cheng)
  • #17: CSE870: Advanced Software Engineering (Cheng)
  • #18: CSE870: Advanced Software Engineering (Cheng)
  • #19: CSE870: Advanced Software Engineering (Cheng)
  • #20: CSE870: Advanced Software Engineering (Cheng)
  • #21: CSE870: Advanced Software Engineering (Cheng)
  • #22: CSE870: Advanced Software Engineering (Cheng)
  • #23: CSE870: Advanced Software Engineering (Cheng)
  • #24: CSE870: Advanced Software Engineering (Cheng)
  • #25: CSE870: Advanced Software Engineering (Cheng)
  • #26: CSE870: Advanced Software Engineering (Cheng)
  • #27: CSE870: Advanced Software Engineering (Cheng)
  • #28: CSE870: Advanced Software Engineering (Cheng)
  • #29: CSE870: Advanced Software Engineering (Cheng)
  • #30: CSE870: Advanced Software Engineering (Cheng)
  • #31: CSE870: Advanced Software Engineering (Cheng)
  • #32: CSE870: Advanced Software Engineering (Cheng)
  • #33: CSE870: Advanced Software Engineering (Cheng)
  • #34: CSE870: Advanced Software Engineering (Cheng)
  • #35: CSE870: Advanced Software Engineering (Cheng)
  • #36: CSE870: Advanced Software Engineering (Cheng)
  • #37: CSE870: Advanced Software Engineering (Cheng)
  • #38: CSE870: Advanced Software Engineering (Cheng)
  • #39: CSE870: Advanced Software Engineering (Cheng)
  • #40: CSE870: Advanced Software Engineering (Cheng)
  • #41: CSE870: Advanced Software Engineering (Cheng)
  • #42: CSE870: Advanced Software Engineering (Cheng)
  • #43: CSE870: Advanced Software Engineering (Cheng)
  • #44: CSE870: Advanced Software Engineering (Cheng)
  • #45: CSE870: Advanced Software Engineering (Cheng)
  • #46: CSE870: Advanced Software Engineering (Cheng)
  • #47: CSE870: Advanced Software Engineering (Cheng)
  • #48: CSE870: Advanced Software Engineering (Cheng)
  • #49: CSE870: Advanced Software Engineering (Cheng)
  • #50: CSE870: Advanced Software Engineering (Cheng)
  • #51: CSE870: Advanced Software Engineering (Cheng)
  • #52: CSE870: Advanced Software Engineering (Cheng)
  • #53: CSE870: Advanced Software Engineering (Cheng)
  • #54: CSE870: Advanced Software Engineering (Cheng)