SlideShare a Scribd company logo
1 of 62
Design Patterns:
Elements of Reusable Object-Oriented Software
(Part 1)
Tom Chen
Overview
● Introduction
● What is Design Pattern?
● Organizing Design Patterns
● How to Select a Design Pattern?
● Design Patterns Part 1
● Creational: Factory, Abstract Factory, Singleton
● Structual: Bridge, Decorator
● Behavioral: Interpreter, Visitor
What is Design Pattern?
● "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 the same way twice"
What is Design Pattern?
Four Essential Elements
● Pattern name
● A handle used to describe a design problem, its
solutions, and consequences in a word or two
● Problem
● When to apply the pattern.
● Solution
● The elements that make up the design, their
relationships, responsibilities, and collaborations
● Consequences
● The results and trade-offs of applying the pattern
Priciples of Design Patterns
● Program to an interface, not an implementation.
● Polymorphism
● Favor object composition over class
inheritance.
● Delegation
Organizing Design Patterns
● Creational Patterns
● Abstract the instantiation process
● Structural Patterns
● How classes and objects are composed to form
larger structures
● Behavioral Patterns
● Describe algorithms and the assignment of
responsibilities between objects
– Not just patterns of objects or classes but also the
patterns of communication between them
Organizing Design Patterns c.1
Creational Patterns
Factory Method
● Intent
● Define an interface for creating an object, but let
subclasses decide which class to instantiate.
● Motivation
● A framework for applications that can present
multiple documents to user
● Both classes of application and document are
abstract
– subclass them to realize their application-specific
implementations
Creational Patterns
Factory Method
Application doesn't know which subclass
to instantiate.
CreateDocument() is the factory method!
Creational Patterns
Factory Method
● Applicability
● A class can't anticipate the class of objects it must
create
● A class wants its subclasses to specify the objects it
creates
● Classes delegate responsibility to one of several
helper subclasses, and you want to localize the
knowledge of which helper subclass is the delegate.
Factory Method
Structure
Factory Method
Sample Code
Factory Method
Sample Code c.1
Factory Method
Sample Code c.2
Factory Method
Consequences
● Eliminate the need to bind application-specific
classes into your code
● Potential Disadv. : clients might have to
subclass the Creator class just to create a
particular ConcreteProduct object
Creational Patterns
Abstract Factory
● Intent
● Provide an interface for creating families of related
or dependent objects without specifying their
concrete classes.
● Motivation
● A UI toolkit that supports multiple look-and-feel
standards
● To be portable, should not hard-code widgets for a
particular look and feel
Creational Patterns
Abstract Factory
This is the Abstract Factory!
Abstract Factory
Applicability
● A system should be independent of how its
products are created, composed, and
represented
● A system should be configured with one of
multiple families of products.
● A family of related product objects is designed
to be used together
● Only want to reveal the interface of a class
library of products, not the implementations
Abstract Factory
Structure
Abstract Factory
Sample Code
New interface! An abstract factory.
Factory Methods are defined!
Pass in abstract factory.
Create objects with the factory
Usage of the new interface:
Abstract Factory
Consequences
● Isolates concrete classes
● Makes exchanging product families easy
● Promotes consistency among products
● Application use objects from only one family at a
time
● Difficult to support new kinds of products
● Change the AbstractFactory class and all of its
subclasses
Creational Patterns
Singleton
● Intent
● Ensure a class only has one instance, and provide
a global point of access to it.
● Motivation
● Important for some classes to have exactly one
instance
– Only one printer spooler, one file system, one window
manager...
● Solution: make the class itself responsible for
keeping track of its sole instance
– Ensure that no other instance can be created
– Provide a way to access the instance
Singleton
Applicability
● Must be exactly one instance of a class and
must be accessible to clients from a well-known
access point
● When the sole instance should be extensible by
subclassing, and clients should be able to use
an extended instance without modifying their
code
Singleton
Structure
Singleton
Sample Code
Singleton
Consequences
● Controlled access to sole instance
● Reduced name space
● An improvement over global variables
● Permits refinement of operations and
representation
● By subclassing
● Permits a variable number of instances
● More flexible than class operations (Static
member functions)
Structural Patterns
Bridge
● Intent
● Decouple an abstraction from its implementation so
that the two can vary independently.
● Motivation
● An abstract class defines the interface and concrete
subclasses implement it in different ways.
– Not always flexible enough
● Inheritance binds an implementation to the
abstraction permanently
– Difficult to modify, extend, and reuse abstractions and
implementations independently
Bridge
Motivation c.1
● Case: Implement a portable Window abstract
● Work on both XWindow and IBM's Presentation
Manager(PM)
Bridge
Motivation c.2
Bridge
Applicability
● Avoid permanent binding between abstraction
and implementation
● EX: Select or switch implementation at run-time
● Both the abstractions and their implementations
should be extensible by subclassing
● Changes in the implementation of an
abstraction should have no impact on clients
Bridge
Structure
Bridge
Sample Code
Bridge
Sample Code c.1
Bridge
Consequences
● Decoupling interface and implementation
● Even possible for an object to change
implementation at run-time
● Eliminate compile-time dependencies on the
implementation
● Improved extensibility
● Extend the Abstraction and Implementor hierarchies
independently
● Hiding implementation details from clients
Structural Patterns
Decorator
● Intent
● Attach additional responsibilities to an object
dynamically. Decorators provide a flexible
alternative to subclassing for extending
functionality.
● Motivation
● Add responsibilities to individual objects, not to an
entire class
– EX: Add properties like borders or behaviors like scrolling
to any UI component
● Inheritance is inflexible
Structural Patterns
Decorator
Structural Patterns
Decorator
DrawScrollbar()
Draw()
Draw()
Draw()
DrawBorder()
Decorator
Applicability
● Add responsibilities to individual objects
dynamically and transparently
● For responsibilities that can be withdrawn
● When extension by subclassing is impractical
● Produce an explosion of subclasses
● Class definition may be hidden or unavailable for
subclassing
Decorator
Structure
Decorator
Sample Code
Decorator
Sample Code c.1
Decorator
Consequences
● More flexibility than static inheritance
● Responsibilities can be added and removed at run-
time
● Avoid feature-laden classes high up in the
hierarchy
● Inheritance requires creating a new class for each
additional responsibility
● A decorator and its component aren't identical
● Lots of little objects
● Easy to customize but can be hard to learn and
debug
Behavioral Patterns
Visitor
● Intent
● Represent an operation to be performed on the
elements of an object structure
● Define a new operation without changing the
classes of the elements on which it operates
● Motivation
● 2D CAD System: Simple geometries compose a
model
● Basic functions
– Save model to file, drawing, dragging, scale ...
Visitor
Motivation
Difficult to extend the ability of Shape!
Visitor
Motivation c.1
Key: Double Dispatch!
Visitor
Applicability
● Perform operations on object structure
containig many classes of objects with differing
interfaces
● Avoid polluting classes with distinct and
unrelated operations
● Visitor keep related operations together
● Classes defining object structure rarely change,
but often define new operations
● Change class need redefining interface to all
visitors
Visitor
Structure
Visitor
Structure c.1
Visitor
Sample Code
Visitor
Sample Code c.1
Visitor
Consequences
● Visitor makes adding new operations easy
● Visitor gathers related operations and
separates unrelated ones
● Adding new ConcreteElement classes is hard
● Corresponding implementation in every
ConcreteVisitor class
Visitor
Consequences c.1
● Visiting across class hierarchies
● Iterator works for classes of the same parent
● Visitor works for classes of different parent
● Accumulating state while visiting elements
● Breaking encapsulation
● Must provide public operations that access an
element's internal state
Behavioral Patterns
Interpreter
● Intent
● Given a language, define a representation for its
grammar along with an interpreter using the
representation to interpret sentences in the
language
● Motivation
● Search for strings matching a pattern decribed by
Regular Expression
● Search algorithms could interpret a regular
expression rather than building custom algorithm
Interpreter
Motivation c.1
● Suppose the following grammar defines the
regular expressions
Interpreter
Motivation c.2
Context
Interpreter
Motivation c.3
● raining & (dogs | cats) *
EX: Context = It is raining cats and dogs.
Interpreter
Applicability
● Grammar is simple
● For complex grammars, the class hierarchy for the
grammar becomes large and unmanageable
● Efficiency is not a critical concern
Interpreter
Structure
Interpreter
Consequences
● Easy to change and extend the grammar
● Implementing the grammar is easy, too
● Complex grammars are hard to maintain.
● Adding new ways to interpret expressions
(Easier)
● EX: Support pretty printing or type-checking an
expression by defining a new operation
– Use Visitor to avoid changing grammar classes
Design Patterns  Part1

More Related Content

What's hot

P Training Presentation
P Training PresentationP Training Presentation
P Training PresentationGaurav Tyagi
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)paramisoft
 
Design Patterns .Net
Design Patterns .NetDesign Patterns .Net
Design Patterns .NetHariom Shah
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Professor Lili Saghafi
 
Introducing Android Programming
Introducing Android ProgrammingIntroducing Android Programming
Introducing Android ProgrammingArif Huda
 

What's hot (8)

P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
 
Gof design patterns
Gof design patternsGof design patterns
Gof design patterns
 
Design Patterns .Net
Design Patterns .NetDesign Patterns .Net
Design Patterns .Net
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Introducing Android Programming
Introducing Android ProgrammingIntroducing Android Programming
Introducing Android Programming
 
1
11
1
 

Viewers also liked

Proxy & adapter pattern
Proxy & adapter patternProxy & adapter pattern
Proxy & adapter patternbabak danyal
 
Adapter design-pattern2015
Adapter design-pattern2015Adapter design-pattern2015
Adapter design-pattern2015Vic Tarchenko
 
Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)APU
 
The 23 gof design patterns in java ,the summary
The 23 gof design patterns in java ,the summaryThe 23 gof design patterns in java ,the summary
The 23 gof design patterns in java ,the summaryachraf_ing
 
Bridge pattern for Dummies
Bridge pattern for DummiesBridge pattern for Dummies
Bridge pattern for DummiesTaekSoon Jang
 
Design Patterns - 04 Adapter and Facade Pattern
Design Patterns - 04 Adapter and Facade PatternDesign Patterns - 04 Adapter and Facade Pattern
Design Patterns - 04 Adapter and Facade Patterneprafulla
 
Design pattern tutorial
Design pattern tutorialDesign pattern tutorial
Design pattern tutorialPiyush Mittal
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Patternguy_davis
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design patternMindfire Solutions
 
Design Patterns
Design PatternsDesign Patterns
Design Patternssoms_1
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns pptAman Jain
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design PatternAdeel Riaz
 
Design Patterns & JDK Examples
Design Patterns & JDK ExamplesDesign Patterns & JDK Examples
Design Patterns & JDK ExamplesEnder Aydin Orak
 

Viewers also liked (15)

Proxy & adapter pattern
Proxy & adapter patternProxy & adapter pattern
Proxy & adapter pattern
 
Adapter design-pattern2015
Adapter design-pattern2015Adapter design-pattern2015
Adapter design-pattern2015
 
Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)
 
The 23 gof design patterns in java ,the summary
The 23 gof design patterns in java ,the summaryThe 23 gof design patterns in java ,the summary
The 23 gof design patterns in java ,the summary
 
Design patterns - Adapter Pattern
Design patterns - Adapter PatternDesign patterns - Adapter Pattern
Design patterns - Adapter Pattern
 
Bridge pattern for Dummies
Bridge pattern for DummiesBridge pattern for Dummies
Bridge pattern for Dummies
 
Design Patterns - 04 Adapter and Facade Pattern
Design Patterns - 04 Adapter and Facade PatternDesign Patterns - 04 Adapter and Facade Pattern
Design Patterns - 04 Adapter and Facade Pattern
 
Design pattern tutorial
Design pattern tutorialDesign pattern tutorial
Design pattern tutorial
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Design Patterns & JDK Examples
Design Patterns & JDK ExamplesDesign Patterns & JDK Examples
Design Patterns & JDK Examples
 

Similar to Design Patterns Part1

UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxanguraju1
 
Creational Design Patterns.pptx
Creational Design Patterns.pptxCreational Design Patterns.pptx
Creational Design Patterns.pptxSachin Patidar
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2Julie Iskander
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)stanbridge
 
[Android] DI in multimodule application
[Android] DI in multimodule application[Android] DI in multimodule application
[Android] DI in multimodule applicationOleg Mazhukin
 
Boston Startup School - OO Design
Boston Startup School - OO DesignBoston Startup School - OO Design
Boston Startup School - OO DesignBryan Warner
 
Introduction To Design Patterns
Introduction To Design PatternsIntroduction To Design Patterns
Introduction To Design Patternssukumarraju6
 
Benefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBenefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBeroza Paul
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopMohammad Shawahneh
 
Design pattern in android
Design pattern in androidDesign pattern in android
Design pattern in androidJay Kumarr
 
Dependency injection using Google guice
Dependency injection using Google guiceDependency injection using Google guice
Dependency injection using Google guiceAman Verma
 
Introduction to Domain driven design
Introduction to Domain driven designIntroduction to Domain driven design
Introduction to Domain driven designMuhammad Ali
 
Foster - Getting started with Angular
Foster - Getting started with AngularFoster - Getting started with Angular
Foster - Getting started with AngularMukundSonaiya1
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code cleanBrett Child
 
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...Isuru Perera
 
Django Patterns - Pycon India 2014
Django Patterns - Pycon India 2014Django Patterns - Pycon India 2014
Django Patterns - Pycon India 2014arunvr
 
Design Patterns In Scala
Design Patterns In ScalaDesign Patterns In Scala
Design Patterns In ScalaKnoldus Inc.
 

Similar to Design Patterns Part1 (20)

UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
Creational Design Patterns.pptx
Creational Design Patterns.pptxCreational Design Patterns.pptx
Creational Design Patterns.pptx
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
 
[Android] DI in multimodule application
[Android] DI in multimodule application[Android] DI in multimodule application
[Android] DI in multimodule application
 
Boston Startup School - OO Design
Boston Startup School - OO DesignBoston Startup School - OO Design
Boston Startup School - OO Design
 
Introduction To Design Patterns
Introduction To Design PatternsIntroduction To Design Patterns
Introduction To Design Patterns
 
Benefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBenefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design pattern
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshop
 
Design pattern in android
Design pattern in androidDesign pattern in android
Design pattern in android
 
Dependency injection using Google guice
Dependency injection using Google guiceDependency injection using Google guice
Dependency injection using Google guice
 
Introduction to Domain driven design
Introduction to Domain driven designIntroduction to Domain driven design
Introduction to Domain driven design
 
Foster - Getting started with Angular
Foster - Getting started with AngularFoster - Getting started with Angular
Foster - Getting started with Angular
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
Design pattern
Design patternDesign pattern
Design pattern
 
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
 
Django Patterns - Pycon India 2014
Django Patterns - Pycon India 2014Django Patterns - Pycon India 2014
Django Patterns - Pycon India 2014
 
Decorator Pattern
Decorator PatternDecorator Pattern
Decorator Pattern
 
Design Patterns in Ruby
Design Patterns in RubyDesign Patterns in Ruby
Design Patterns in Ruby
 
Design Patterns In Scala
Design Patterns In ScalaDesign Patterns In Scala
Design Patterns In Scala
 

Recently uploaded

Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 

Recently uploaded (20)

Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 

Design Patterns Part1

  • 1. Design Patterns: Elements of Reusable Object-Oriented Software (Part 1) Tom Chen
  • 2. Overview ● Introduction ● What is Design Pattern? ● Organizing Design Patterns ● How to Select a Design Pattern? ● Design Patterns Part 1 ● Creational: Factory, Abstract Factory, Singleton ● Structual: Bridge, Decorator ● Behavioral: Interpreter, Visitor
  • 3. What is Design Pattern? ● "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 the same way twice"
  • 4. What is Design Pattern? Four Essential Elements ● Pattern name ● A handle used to describe a design problem, its solutions, and consequences in a word or two ● Problem ● When to apply the pattern. ● Solution ● The elements that make up the design, their relationships, responsibilities, and collaborations ● Consequences ● The results and trade-offs of applying the pattern
  • 5. Priciples of Design Patterns ● Program to an interface, not an implementation. ● Polymorphism ● Favor object composition over class inheritance. ● Delegation
  • 6. Organizing Design Patterns ● Creational Patterns ● Abstract the instantiation process ● Structural Patterns ● How classes and objects are composed to form larger structures ● Behavioral Patterns ● Describe algorithms and the assignment of responsibilities between objects – Not just patterns of objects or classes but also the patterns of communication between them
  • 8. Creational Patterns Factory Method ● Intent ● Define an interface for creating an object, but let subclasses decide which class to instantiate. ● Motivation ● A framework for applications that can present multiple documents to user ● Both classes of application and document are abstract – subclass them to realize their application-specific implementations
  • 9. Creational Patterns Factory Method Application doesn't know which subclass to instantiate. CreateDocument() is the factory method!
  • 10. Creational Patterns Factory Method ● Applicability ● A class can't anticipate the class of objects it must create ● A class wants its subclasses to specify the objects it creates ● Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
  • 15. Factory Method Consequences ● Eliminate the need to bind application-specific classes into your code ● Potential Disadv. : clients might have to subclass the Creator class just to create a particular ConcreteProduct object
  • 16. Creational Patterns Abstract Factory ● Intent ● Provide an interface for creating families of related or dependent objects without specifying their concrete classes. ● Motivation ● A UI toolkit that supports multiple look-and-feel standards ● To be portable, should not hard-code widgets for a particular look and feel
  • 17. Creational Patterns Abstract Factory This is the Abstract Factory!
  • 18. Abstract Factory Applicability ● A system should be independent of how its products are created, composed, and represented ● A system should be configured with one of multiple families of products. ● A family of related product objects is designed to be used together ● Only want to reveal the interface of a class library of products, not the implementations
  • 20. Abstract Factory Sample Code New interface! An abstract factory. Factory Methods are defined!
  • 21. Pass in abstract factory. Create objects with the factory Usage of the new interface:
  • 22. Abstract Factory Consequences ● Isolates concrete classes ● Makes exchanging product families easy ● Promotes consistency among products ● Application use objects from only one family at a time ● Difficult to support new kinds of products ● Change the AbstractFactory class and all of its subclasses
  • 23. Creational Patterns Singleton ● Intent ● Ensure a class only has one instance, and provide a global point of access to it. ● Motivation ● Important for some classes to have exactly one instance – Only one printer spooler, one file system, one window manager... ● Solution: make the class itself responsible for keeping track of its sole instance – Ensure that no other instance can be created – Provide a way to access the instance
  • 24. Singleton Applicability ● Must be exactly one instance of a class and must be accessible to clients from a well-known access point ● When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code
  • 27.
  • 28. Singleton Consequences ● Controlled access to sole instance ● Reduced name space ● An improvement over global variables ● Permits refinement of operations and representation ● By subclassing ● Permits a variable number of instances ● More flexible than class operations (Static member functions)
  • 29. Structural Patterns Bridge ● Intent ● Decouple an abstraction from its implementation so that the two can vary independently. ● Motivation ● An abstract class defines the interface and concrete subclasses implement it in different ways. – Not always flexible enough ● Inheritance binds an implementation to the abstraction permanently – Difficult to modify, extend, and reuse abstractions and implementations independently
  • 30. Bridge Motivation c.1 ● Case: Implement a portable Window abstract ● Work on both XWindow and IBM's Presentation Manager(PM)
  • 32. Bridge Applicability ● Avoid permanent binding between abstraction and implementation ● EX: Select or switch implementation at run-time ● Both the abstractions and their implementations should be extensible by subclassing ● Changes in the implementation of an abstraction should have no impact on clients
  • 36. Bridge Consequences ● Decoupling interface and implementation ● Even possible for an object to change implementation at run-time ● Eliminate compile-time dependencies on the implementation ● Improved extensibility ● Extend the Abstraction and Implementor hierarchies independently ● Hiding implementation details from clients
  • 37. Structural Patterns Decorator ● Intent ● Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. ● Motivation ● Add responsibilities to individual objects, not to an entire class – EX: Add properties like borders or behaviors like scrolling to any UI component ● Inheritance is inflexible
  • 40. Decorator Applicability ● Add responsibilities to individual objects dynamically and transparently ● For responsibilities that can be withdrawn ● When extension by subclassing is impractical ● Produce an explosion of subclasses ● Class definition may be hidden or unavailable for subclassing
  • 44. Decorator Consequences ● More flexibility than static inheritance ● Responsibilities can be added and removed at run- time ● Avoid feature-laden classes high up in the hierarchy ● Inheritance requires creating a new class for each additional responsibility ● A decorator and its component aren't identical ● Lots of little objects ● Easy to customize but can be hard to learn and debug
  • 45. Behavioral Patterns Visitor ● Intent ● Represent an operation to be performed on the elements of an object structure ● Define a new operation without changing the classes of the elements on which it operates ● Motivation ● 2D CAD System: Simple geometries compose a model ● Basic functions – Save model to file, drawing, dragging, scale ...
  • 46. Visitor Motivation Difficult to extend the ability of Shape!
  • 48. Visitor Applicability ● Perform operations on object structure containig many classes of objects with differing interfaces ● Avoid polluting classes with distinct and unrelated operations ● Visitor keep related operations together ● Classes defining object structure rarely change, but often define new operations ● Change class need redefining interface to all visitors
  • 53. Visitor Consequences ● Visitor makes adding new operations easy ● Visitor gathers related operations and separates unrelated ones ● Adding new ConcreteElement classes is hard ● Corresponding implementation in every ConcreteVisitor class
  • 54. Visitor Consequences c.1 ● Visiting across class hierarchies ● Iterator works for classes of the same parent ● Visitor works for classes of different parent ● Accumulating state while visiting elements ● Breaking encapsulation ● Must provide public operations that access an element's internal state
  • 55. Behavioral Patterns Interpreter ● Intent ● Given a language, define a representation for its grammar along with an interpreter using the representation to interpret sentences in the language ● Motivation ● Search for strings matching a pattern decribed by Regular Expression ● Search algorithms could interpret a regular expression rather than building custom algorithm
  • 56. Interpreter Motivation c.1 ● Suppose the following grammar defines the regular expressions
  • 58. Interpreter Motivation c.3 ● raining & (dogs | cats) * EX: Context = It is raining cats and dogs.
  • 59. Interpreter Applicability ● Grammar is simple ● For complex grammars, the class hierarchy for the grammar becomes large and unmanageable ● Efficiency is not a critical concern
  • 61. Interpreter Consequences ● Easy to change and extend the grammar ● Implementing the grammar is easy, too ● Complex grammars are hard to maintain. ● Adding new ways to interpret expressions (Easier) ● EX: Support pretty printing or type-checking an expression by defining a new operation – Use Visitor to avoid changing grammar classes