SlideShare a Scribd company logo
1
SOFTWARE
SOFTWARE
DESIGN
DESIGN
PATTERNS
PATTERNS
“Descriptions of communicating
objects and classes that are
customized to solve a general
design problem”
-- Gamma, et. al.
2
 The Pattern Name
 The Problem it solves
 The Solution it provides
 The Consequences (good and bad) of using it
3
4
CHANGES…
Patterns should be able to deal with changes.
Changes can come from many different sources:
- New vendor
- New Technology
- New views
-New complexity
of the application domain.
- Errors
There are 3 types of pattern …
 Creational: address problems of creating an object
in a flexible way. Separate creation, from
operation/use.
 Structural: address problems of using O-O
constructs like inheritance to organize classes and
objects
 Behavioral: address problems of assigning
responsibilities to classes. Suggest both static
relationships and patterns of communication
(use cases)
A modifiable design enables
• An iterative and incremental development cycle,
which allows
 Concurrent (aka simultaneous) development
 Risk management
 Flexibility to change
• The software developer to minimize the
introduction of new problems when fixing old
ones.
• The software developer to deliver more
functionality after initial delivery.
6
 Low coupling and high cohesion
 Clear dependencies between classes
 Explicit assumptions.
How do design patterns help?
 They are generalized from existing, i.e. proven,
systems.
 They provide a shared vocabulary to designers.
 They provide examples of modifiable designs.
 They use, principally,
 Abstract classes
 Delegation & inheritance
7
8
Common architectural patterns
Façade Strategy
Proxy Abstract Factory
Bridge Command
Adapter Composite
9
Patterns Explained
Singleton
Façade
Factory
Adapter
Bridge
Name: Singleton
Problem:
How can we guarantee that one and only one
instance of a class can be created?
Context: In some applications it is important
to have exactly one instance of a class, e.g. sales of
one company.
Forces: Can make an object globally accessible as a
global variable, but this violates encapsulation.
Could use class (static) operations and attributes, but
polymorphic redefinition is not always possible.
Solution:
Create a class with a class operation getInstance().
When class is first accessed, this creates relevant
object instance and returns object identity to client.
On subsequent calls of getInstance(), no new
instance is created, but identity of existing object is
returned.
Singleton
-uniqueInstance
-singletonData
+getInstance( )
+getSingletonData( )
+singletonOperation( )
-Singleton( )
Object identifier for singleton
instance, class scope or static
Returns object identifier for
unique instance, class-scope
or static
Private constructor only accessible
via getInstance()
getInstance( ) {
if ( uniqueInstance == null )
{ uniqueInstance = new Singleton( ) }
return uniqueInstance
}
Class Singleton {
private static Singleton uniqueInstance = null;
private Singleton( ) { .. } // private constructor
public static Singleton getInstance( ) {
if (uniqueInstance == null)
uniqueInstance = new Singleton();
// call constructor
return uniqueInstance;
}
}
Example: Code
 To specify a class has only one instance, we
make it inherit from Singleton.
+ controlled access to single object instance
through Singleton encapsulation
+ Can tailor for any finite number of instances
+ namespace not extended by global variables
- access requires additional message passing
- Pattern limits flexibility, significant redesign if
singleton class later gets many instances
15
A sculpture is singleton
Name: Façade
Problem:
How can we access a large number of classes
with a complex internal interaction in a simple
but safe way?
Solution: Introduce a dedicated interface class
that simplifies the view of the class collection.
 Provides a unified interface to a set of objects in a
subsystem.
 A façade defines a higher-level interface that makes the
subsystem easier to use (i.e. it abstracts out all the
“messy” details)
 Façades allow us to provide a closed architecture.
17
Bad!
Good!
Facade
subsystem classes
19
Façade interface to
client provides one (or
few) method(s) that
allows the client to
invoke the subsystems
services.
<<façade>>
SecurityManager
+addAccessRight()
+addActor()
+addActorRole()
+removeActor()
AccessRight
+addAccessRight()
Actor
+addActor()
+removeActor()
+changeSalary()
ActorRole
+addActorRole()
Method not in Facade
Pattern Name
21
Home Theatre system and compiler are examples of
Façade implementation
22
Compiler
compile(s)
Parse Node
create()
Lexical Analysis
getToken()
Code Generator
create()
Parser
generateParseTree()
Optimizer
create()
Compiler
23
Example – Home Theater
Lots of classes
and interactions,
plus many interfaces.
 Clients communicate with the subsystem by sending
requests to Façade which forwards them to the
appropriate subsystem object(s).
 Although subsystem objects perform actual work,
Façade may have to translate its interface to subsystem
interfaces.
 Clients that use the Façade don’t have to access its
subsystem objects directly.
 Usually only one Façade object is required. Thus a
Façade object is often a singleton.
 Factory pattern can be used with Façade to provide an
interface for creating subsystem objects in a subsystem
independent way.
 Factory can also be used as an alternative to Façade to
hide platform-specific classes.
 Mediator pattern is similar to Façade in abstracting
functionality of classes.
Name: (Abstract) Factory
Problem: Provide an interface for creating
families of related or dependent objects
without specifying their concrete classes.
 Control instantiation
 Singleton is a special case of Factory
where
only one object can be created.
AbstractProduct
ConcreteProduct1
AbstractFactory
ConcreteFactory
FactoryMethod()
AnOperation()
FactoryMethod()
Product =
FactoryMethod()
return new
ConcreteProduct()
 Normally, a single instance of a
ConcreteFactory class is created at
runtime.
 This creates product objects having a
particular implementation.
 To create different product objects, clients
should use a different concrete factory.
 AbstractFactory defers creation of
product objects to its ConcreteFactory
subclasses.
MyClass
createObjectOfRequiredClass():RequiredClass
Factory design pattern
Client RequiredClass
Factory Class Model
create objects
 E.g. create objects of different types
 We can order a Car and get an
Aston Martin, MG, Jaguar etc
 We don’t know which factory builds the
car, just that they implement CarFactory.
Owner has created the actual factory
instance.
// we have a reference to owner
CarFactory aFactory = owner.makefactory();
Car myCar =aFactory.makeCar(“AstonMartin”);
class BritishFactory implements CarFactory{
public Car makeCar(String b) throws Exception {
if(b.equals(“AstonMartin”)) return new
AstonMartin();
else if (..) return ..;
else throw new Exception(b + “doesn’t
exist”);
}
}
Class AstonMartin extends Car {}
Class Jaguar extends Car {}
Interface CarFactory {
Public Car makeCar(String b) throws Exception;
}
33
Aston Martin Jaguar
 Abstract Factory classes are often
implemented with the Factory Method
pattern.
 Can also be implemented using the
Prototype pattern.
 A concrete factory is often a Singleton.
Comments
35
Pattern: Adapter
Suppose a client objects expect a certain interface to be
provided by called object.
However, the interface of the called object differs from
what the clients expect, and cannot be changed.
How may this situation be improved, i.e. how may the
interface satisfy the clients?
36
 Transforms the interface of the target class into another
interface that the client classes expect.
 Adapter lets classes work together that could not
otherwise because of incompatible interfaces.
 Used to provide a new interface to existing legacy (i.e.
old) software components (aka interface engineering,
re-engineering).
 Adapter also known as a wrapper
37
38
Adapter Patten
Context:
1. Want to use an existing class without modifying it – call it the
“legacy” class
2. But context in which you want to use the legacy class requires
conformance to a target (i.e. new) interface that is different from
that which the legacy provides.
3. However, the target interface and legacy interface are
conceptually related.
Solution:
1. Define an adapter class that implements the target interface.
2. The adapter class holds a reference (delegates) to the legacy
class.
It translates (new) target requests (or method calls) from client
classes to (old) legacy requests (or method calls).
39
Participants of the Adapter Pattern
• Target: Defines the application-specific interface that
clients use.
• Client: Collaborates with objects conforming to the
target interface.
• Legacy: Defines an existing interface that needs
adapting.
• Adapter: Adapts the interface of the legacy class to the
target interface.
Bind an Adapter class with the Legacy class
 The Adapter class implements the ClientInterface expected by
the client. It then delegates requests from the client to the Legacy
class and performs any necessary conversion.
 ClientInterface could be a Java/C# interface, or an abstract
class
40
Client
Target
ClientInterface
Request()
Legacy
ExistingRequest()
Adapter
Request()
Legacy class
delegationinheritance
41
Interface inheritance is use to specify the interface of the
Adapter class – and then delegation is used to reference
the Legacy class
Legacy class
supporting old
interface
Class supporting new
interface
Inheritance
Delegation
42
Example
Turkeys do not quack, they
gobble. Turkeys can fly but for
only short distances.
How can we adapt the Turkey class to behave like a
Duck class?
43
duck quack becomes turkey gobble
duck fly becomes five
times turkey fly
Adapt the Turkey class to have the same interface as Duck.
i.e. have methods quack() and fly()
44
Pattern: Bridge
Client objects expect a constant interface to be provided
by some called object.
However, the actual implementation of the interface may
vary – there may be lots of different implementations,
depending on the circumstances.
How may this situation be satisfied?
 Use a Bridge pattern to decouple an
abstraction from its implementation
so that both can vary independently.
 Also known as “handle/body” pattern
 Allows different implementations of an interface
to be decided upon dynamically.
45
46
Problem
- Want to decouple an abstraction (i.e. class interface) from its
implementation, allowing them to vary separately.
Context
- Want to change implementation details at run-time without impact to
clients classes.
Solution
- Use delegation from an interface class to an implementation class.
Consequences
- Can change (or add) interface without re-implementing.
- Can change implementation, but not impact interface for clients.
47
Inheritance
ImplementorAbstraction
implements
ImplementorA ImplementorB
Client
The Abstraction class defines the interface visible to client.
The Implementor class is an abstract implementation that defines the
lower-level methods available to Abstraction – namely different
“concrete” implementations such as ImplementorA or ImplementorB
Delegation
48
Example
Supporting multiple Database Vendors with a Bridge
LeagueStoreImplementorLeagueStore
implements
XML Store
Implementor
SQL Server Store
Implementor
JDBC Store
Implementor
Arena
LeagueStore is the interface class to the pattern
Abstract interface
provides common
interface
 The bridge pattern is used to provide multiple
implementations under the same interface.
 Decouples an interface from the implementation so that
implementation can be substituted, possibly at runtime.
 Example: Interface to a component that is incomplete,
not yet known, or unavailable during testing
49
50
Consider problem of incrementally developing, testing
and integrating subsystems realized by different project
teams.
Subsystems may be completed at different times,
delaying the integration of all subsystems until the last
one has been completed.
To avoid delay, projects often use a stub
implementations in place of a specific subsystem so
that the integration tests can start even before the
subsystems are completed.
51
Example of the bridge pattern
 Similarities:
 Both are used to hide the details of the underlying
implementation.
 Difference:
 The adapter pattern is geared towards making
unrelated components work together
 Applied to systems after they are designed (reengineering,
interface engineering).
 A bridge, on the other hand, is used up-front in a
design to let abstractions and implementations vary
independently.
 Engineering of an “extensible system”.
 New components can be added to the system, even if these
are not known at analysis or system design time.
52

More Related Content

What's hot

Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
Mindfire Solutions
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
Asma CHERIF
 
Introduction to design patterns
Introduction to design patternsIntroduction to design patterns
Introduction to design patterns
Amit Kabra
 
Design Pattern in Software Engineering
Design Pattern in Software EngineeringDesign Pattern in Software Engineering
Design Pattern in Software Engineering
Manish Kumar
 
Design Patterns
Design PatternsDesign Patterns
Design Patternssoms_1
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
Shahzad
 
Pipes & Filters Architectural Pattern
Pipes & Filters Architectural PatternPipes & Filters Architectural Pattern
Pipes & Filters Architectural Pattern
Fredrik Kivi
 
Software architecture Unit 1 notes
Software architecture Unit 1 notesSoftware architecture Unit 1 notes
Software architecture Unit 1 notesSudarshan Dhondaley
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns pptAman Jain
 
Slides chapter 9
Slides chapter 9Slides chapter 9
Slides chapter 9
Priyanka Shetty
 
Oomd unit1
Oomd unit1Oomd unit1
Oomd unit1
VivekChaudhary93
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
11prasoon
 
DESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy PatternsDESIGN PATTERNS: Strategy Patterns
CS6201 Software Reuse - Design Patterns
CS6201 Software Reuse - Design PatternsCS6201 Software Reuse - Design Patterns
CS6201 Software Reuse - Design Patterns
Kwangshin Oh
 
Uml in software engineering
Uml in software engineeringUml in software engineering
Uml in software engineering
Mubashir Jutt
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and DesignHaitham El-Ghareeb
 

What's hot (20)

Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
 
Introduction to design patterns
Introduction to design patternsIntroduction to design patterns
Introduction to design patterns
 
Design Pattern in Software Engineering
Design Pattern in Software EngineeringDesign Pattern in Software Engineering
Design Pattern in Software Engineering
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
 
Gof design patterns
Gof design patternsGof design patterns
Gof design patterns
 
Pipes & Filters Architectural Pattern
Pipes & Filters Architectural PatternPipes & Filters Architectural Pattern
Pipes & Filters Architectural Pattern
 
Software architecture Unit 1 notes
Software architecture Unit 1 notesSoftware architecture Unit 1 notes
Software architecture Unit 1 notes
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
 
Design patterns tutorials
Design patterns tutorialsDesign patterns tutorials
Design patterns tutorials
 
Slides chapter 9
Slides chapter 9Slides chapter 9
Slides chapter 9
 
Design Pattern
Design PatternDesign Pattern
Design Pattern
 
Oomd unit1
Oomd unit1Oomd unit1
Oomd unit1
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
 
DESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy PatternsDESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy Patterns
 
CS6201 Software Reuse - Design Patterns
CS6201 Software Reuse - Design PatternsCS6201 Software Reuse - Design Patterns
CS6201 Software Reuse - Design Patterns
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
 
Uml in software engineering
Uml in software engineeringUml in software engineering
Uml in software engineering
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and Design
 

Viewers also liked

Software Design Patterns in Practice
Software Design Patterns in PracticeSoftware Design Patterns in Practice
Software Design Patterns in Practice
Ptidej Team
 
Software design patterns ppt
Software design patterns pptSoftware design patterns ppt
Software design patterns ppt
mkruthika
 
Style & Design Principles 02 - Design Patterns
Style & Design Principles 02 - Design PatternsStyle & Design Principles 02 - Design Patterns
Style & Design Principles 02 - Design Patterns
Nick Pruehs
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
Satheesh Sukumaran
 
Recommendation System for Design Patterns in Software Development
Recommendation System for Design Patterns in Software DevelopmentRecommendation System for Design Patterns in Software Development
Recommendation System for Design Patterns in Software Development
Francis Palma
 
PRDCW-advanced-design-patterns
PRDCW-advanced-design-patternsPRDCW-advanced-design-patterns
PRDCW-advanced-design-patterns
Amir Barylko
 
Architecture Description Languages: An Overview
Architecture Description Languages: An OverviewArchitecture Description Languages: An Overview
Architecture Description Languages: An Overviewelliando dias
 
PRDC12 advanced design patterns
PRDC12 advanced design patternsPRDC12 advanced design patterns
PRDC12 advanced design patterns
Amir Barylko
 
Unity - Software Design Patterns
Unity - Software Design PatternsUnity - Software Design Patterns
Unity - Software Design Patterns
David Baron
 
Implementing advanced integration patterns with WSO2 ESB
Implementing advanced integration patterns with WSO2 ESBImplementing advanced integration patterns with WSO2 ESB
Implementing advanced integration patterns with WSO2 ESB
WSO2
 
Software Architecture: Architecture Description Languages
Software Architecture: Architecture Description LanguagesSoftware Architecture: Architecture Description Languages
Software Architecture: Architecture Description Languages
Henry Muccini
 
Advanced Patterns with io.ReadWriter
Advanced Patterns with io.ReadWriterAdvanced Patterns with io.ReadWriter
Advanced Patterns with io.ReadWriter
Weaveworks
 
Architectural Design Pattern: Android
Architectural Design Pattern: AndroidArchitectural Design Pattern: Android
Architectural Design Pattern: Android
Jitendra Kumar
 
Architecture evaluation
Architecture evaluationArchitecture evaluation
Architecture evaluation
Alexandru Chica
 

Viewers also liked (14)

Software Design Patterns in Practice
Software Design Patterns in PracticeSoftware Design Patterns in Practice
Software Design Patterns in Practice
 
Software design patterns ppt
Software design patterns pptSoftware design patterns ppt
Software design patterns ppt
 
Style & Design Principles 02 - Design Patterns
Style & Design Principles 02 - Design PatternsStyle & Design Principles 02 - Design Patterns
Style & Design Principles 02 - Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Recommendation System for Design Patterns in Software Development
Recommendation System for Design Patterns in Software DevelopmentRecommendation System for Design Patterns in Software Development
Recommendation System for Design Patterns in Software Development
 
PRDCW-advanced-design-patterns
PRDCW-advanced-design-patternsPRDCW-advanced-design-patterns
PRDCW-advanced-design-patterns
 
Architecture Description Languages: An Overview
Architecture Description Languages: An OverviewArchitecture Description Languages: An Overview
Architecture Description Languages: An Overview
 
PRDC12 advanced design patterns
PRDC12 advanced design patternsPRDC12 advanced design patterns
PRDC12 advanced design patterns
 
Unity - Software Design Patterns
Unity - Software Design PatternsUnity - Software Design Patterns
Unity - Software Design Patterns
 
Implementing advanced integration patterns with WSO2 ESB
Implementing advanced integration patterns with WSO2 ESBImplementing advanced integration patterns with WSO2 ESB
Implementing advanced integration patterns with WSO2 ESB
 
Software Architecture: Architecture Description Languages
Software Architecture: Architecture Description LanguagesSoftware Architecture: Architecture Description Languages
Software Architecture: Architecture Description Languages
 
Advanced Patterns with io.ReadWriter
Advanced Patterns with io.ReadWriterAdvanced Patterns with io.ReadWriter
Advanced Patterns with io.ReadWriter
 
Architectural Design Pattern: Android
Architectural Design Pattern: AndroidArchitectural Design Pattern: Android
Architectural Design Pattern: Android
 
Architecture evaluation
Architecture evaluationArchitecture evaluation
Architecture evaluation
 

Similar to Software Design Patterns

P Training Presentation
P Training PresentationP Training Presentation
P Training PresentationGaurav Tyagi
 
Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder
paramisoft
 
27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examplesQuang Suma
 
Design patterns
Design patternsDesign patterns
Design patterns
Anas Alpure
 
Software System Architecture-Lecture 6.pptx
Software System Architecture-Lecture 6.pptxSoftware System Architecture-Lecture 6.pptx
Software System Architecture-Lecture 6.pptx
ssuser9a23691
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley45372
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Paulo Clavijo
 
Creational pattern 2
Creational pattern 2Creational pattern 2
Creational pattern 2
Naga Muruga
 
Creating and destroying objects
Creating and destroying objectsCreating and destroying objects
Creating and destroying objects
Sandeep Chawla
 
Factory method pattern (Virtual Constructor)
Factory method pattern (Virtual Constructor)Factory method pattern (Virtual Constructor)
Factory method pattern (Virtual Constructor)
Sameer Rathoud
 
Unit 2-Design Patterns.ppt
Unit 2-Design Patterns.pptUnit 2-Design Patterns.ppt
Unit 2-Design Patterns.ppt
MsRAMYACSE
 
Prophecy Of Design Patterns
Prophecy Of Design PatternsProphecy Of Design Patterns
Prophecy Of Design Patterns
pradeepkothiyal
 
Abstract factory
Abstract factoryAbstract factory
Abstract factory
Muthukumar P
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
Ankit.Rustagi
 
Design Pattern Cheatsheet
Design Pattern CheatsheetDesign Pattern Cheatsheet
Design Pattern Cheatsheet
Rachanee Saengkrajai
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
akreyi
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
Sergii Stets
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
Naga Muruga
 

Similar to Software Design Patterns (20)

Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder
 
27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Software System Architecture-Lecture 6.pptx
Software System Architecture-Lecture 6.pptxSoftware System Architecture-Lecture 6.pptx
Software System Architecture-Lecture 6.pptx
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
 
Creational pattern 2
Creational pattern 2Creational pattern 2
Creational pattern 2
 
Creating and destroying objects
Creating and destroying objectsCreating and destroying objects
Creating and destroying objects
 
Factory method pattern (Virtual Constructor)
Factory method pattern (Virtual Constructor)Factory method pattern (Virtual Constructor)
Factory method pattern (Virtual Constructor)
 
Unit 2-Design Patterns.ppt
Unit 2-Design Patterns.pptUnit 2-Design Patterns.ppt
Unit 2-Design Patterns.ppt
 
Prophecy Of Design Patterns
Prophecy Of Design PatternsProphecy Of Design Patterns
Prophecy Of Design Patterns
 
Abstract factory
Abstract factoryAbstract factory
Abstract factory
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design Pattern Cheatsheet
Design Pattern CheatsheetDesign Pattern Cheatsheet
Design Pattern Cheatsheet
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 

Recently uploaded

1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 

Recently uploaded (20)

1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 

Software Design Patterns

  • 2. “Descriptions of communicating objects and classes that are customized to solve a general design problem” -- Gamma, et. al. 2
  • 3.  The Pattern Name  The Problem it solves  The Solution it provides  The Consequences (good and bad) of using it 3
  • 4. 4 CHANGES… Patterns should be able to deal with changes. Changes can come from many different sources: - New vendor - New Technology - New views -New complexity of the application domain. - Errors
  • 5. There are 3 types of pattern …  Creational: address problems of creating an object in a flexible way. Separate creation, from operation/use.  Structural: address problems of using O-O constructs like inheritance to organize classes and objects  Behavioral: address problems of assigning responsibilities to classes. Suggest both static relationships and patterns of communication (use cases)
  • 6. A modifiable design enables • An iterative and incremental development cycle, which allows  Concurrent (aka simultaneous) development  Risk management  Flexibility to change • The software developer to minimize the introduction of new problems when fixing old ones. • The software developer to deliver more functionality after initial delivery. 6
  • 7.  Low coupling and high cohesion  Clear dependencies between classes  Explicit assumptions. How do design patterns help?  They are generalized from existing, i.e. proven, systems.  They provide a shared vocabulary to designers.  They provide examples of modifiable designs.  They use, principally,  Abstract classes  Delegation & inheritance 7
  • 8. 8 Common architectural patterns Façade Strategy Proxy Abstract Factory Bridge Command Adapter Composite
  • 10. Name: Singleton Problem: How can we guarantee that one and only one instance of a class can be created? Context: In some applications it is important to have exactly one instance of a class, e.g. sales of one company.
  • 11. Forces: Can make an object globally accessible as a global variable, but this violates encapsulation. Could use class (static) operations and attributes, but polymorphic redefinition is not always possible. Solution: Create a class with a class operation getInstance(). When class is first accessed, this creates relevant object instance and returns object identity to client. On subsequent calls of getInstance(), no new instance is created, but identity of existing object is returned.
  • 12. Singleton -uniqueInstance -singletonData +getInstance( ) +getSingletonData( ) +singletonOperation( ) -Singleton( ) Object identifier for singleton instance, class scope or static Returns object identifier for unique instance, class-scope or static Private constructor only accessible via getInstance() getInstance( ) { if ( uniqueInstance == null ) { uniqueInstance = new Singleton( ) } return uniqueInstance }
  • 13. Class Singleton { private static Singleton uniqueInstance = null; private Singleton( ) { .. } // private constructor public static Singleton getInstance( ) { if (uniqueInstance == null) uniqueInstance = new Singleton(); // call constructor return uniqueInstance; } } Example: Code
  • 14.  To specify a class has only one instance, we make it inherit from Singleton. + controlled access to single object instance through Singleton encapsulation + Can tailor for any finite number of instances + namespace not extended by global variables - access requires additional message passing - Pattern limits flexibility, significant redesign if singleton class later gets many instances
  • 15. 15 A sculpture is singleton
  • 16. Name: Façade Problem: How can we access a large number of classes with a complex internal interaction in a simple but safe way? Solution: Introduce a dedicated interface class that simplifies the view of the class collection.
  • 17.  Provides a unified interface to a set of objects in a subsystem.  A façade defines a higher-level interface that makes the subsystem easier to use (i.e. it abstracts out all the “messy” details)  Façades allow us to provide a closed architecture. 17 Bad! Good!
  • 19. 19 Façade interface to client provides one (or few) method(s) that allows the client to invoke the subsystems services.
  • 21. 21 Home Theatre system and compiler are examples of Façade implementation
  • 22. 22 Compiler compile(s) Parse Node create() Lexical Analysis getToken() Code Generator create() Parser generateParseTree() Optimizer create() Compiler
  • 23. 23 Example – Home Theater Lots of classes and interactions, plus many interfaces.
  • 24.  Clients communicate with the subsystem by sending requests to Façade which forwards them to the appropriate subsystem object(s).  Although subsystem objects perform actual work, Façade may have to translate its interface to subsystem interfaces.  Clients that use the Façade don’t have to access its subsystem objects directly.
  • 25.  Usually only one Façade object is required. Thus a Façade object is often a singleton.  Factory pattern can be used with Façade to provide an interface for creating subsystem objects in a subsystem independent way.  Factory can also be used as an alternative to Façade to hide platform-specific classes.  Mediator pattern is similar to Façade in abstracting functionality of classes.
  • 26. Name: (Abstract) Factory Problem: Provide an interface for creating families of related or dependent objects without specifying their concrete classes.  Control instantiation  Singleton is a special case of Factory where only one object can be created.
  • 28.  Normally, a single instance of a ConcreteFactory class is created at runtime.  This creates product objects having a particular implementation.  To create different product objects, clients should use a different concrete factory.  AbstractFactory defers creation of product objects to its ConcreteFactory subclasses.
  • 30.  E.g. create objects of different types  We can order a Car and get an Aston Martin, MG, Jaguar etc  We don’t know which factory builds the car, just that they implement CarFactory. Owner has created the actual factory instance.
  • 31. // we have a reference to owner CarFactory aFactory = owner.makefactory(); Car myCar =aFactory.makeCar(“AstonMartin”); class BritishFactory implements CarFactory{ public Car makeCar(String b) throws Exception { if(b.equals(“AstonMartin”)) return new AstonMartin(); else if (..) return ..; else throw new Exception(b + “doesn’t exist”); } }
  • 32. Class AstonMartin extends Car {} Class Jaguar extends Car {} Interface CarFactory { Public Car makeCar(String b) throws Exception; }
  • 34.  Abstract Factory classes are often implemented with the Factory Method pattern.  Can also be implemented using the Prototype pattern.  A concrete factory is often a Singleton. Comments
  • 35. 35 Pattern: Adapter Suppose a client objects expect a certain interface to be provided by called object. However, the interface of the called object differs from what the clients expect, and cannot be changed. How may this situation be improved, i.e. how may the interface satisfy the clients?
  • 36. 36
  • 37.  Transforms the interface of the target class into another interface that the client classes expect.  Adapter lets classes work together that could not otherwise because of incompatible interfaces.  Used to provide a new interface to existing legacy (i.e. old) software components (aka interface engineering, re-engineering).  Adapter also known as a wrapper 37
  • 38. 38 Adapter Patten Context: 1. Want to use an existing class without modifying it – call it the “legacy” class 2. But context in which you want to use the legacy class requires conformance to a target (i.e. new) interface that is different from that which the legacy provides. 3. However, the target interface and legacy interface are conceptually related. Solution: 1. Define an adapter class that implements the target interface. 2. The adapter class holds a reference (delegates) to the legacy class. It translates (new) target requests (or method calls) from client classes to (old) legacy requests (or method calls).
  • 39. 39 Participants of the Adapter Pattern • Target: Defines the application-specific interface that clients use. • Client: Collaborates with objects conforming to the target interface. • Legacy: Defines an existing interface that needs adapting. • Adapter: Adapts the interface of the legacy class to the target interface.
  • 40. Bind an Adapter class with the Legacy class  The Adapter class implements the ClientInterface expected by the client. It then delegates requests from the client to the Legacy class and performs any necessary conversion.  ClientInterface could be a Java/C# interface, or an abstract class 40 Client Target ClientInterface Request() Legacy ExistingRequest() Adapter Request() Legacy class delegationinheritance
  • 41. 41 Interface inheritance is use to specify the interface of the Adapter class – and then delegation is used to reference the Legacy class Legacy class supporting old interface Class supporting new interface Inheritance Delegation
  • 42. 42 Example Turkeys do not quack, they gobble. Turkeys can fly but for only short distances. How can we adapt the Turkey class to behave like a Duck class?
  • 43. 43 duck quack becomes turkey gobble duck fly becomes five times turkey fly Adapt the Turkey class to have the same interface as Duck. i.e. have methods quack() and fly()
  • 44. 44 Pattern: Bridge Client objects expect a constant interface to be provided by some called object. However, the actual implementation of the interface may vary – there may be lots of different implementations, depending on the circumstances. How may this situation be satisfied?
  • 45.  Use a Bridge pattern to decouple an abstraction from its implementation so that both can vary independently.  Also known as “handle/body” pattern  Allows different implementations of an interface to be decided upon dynamically. 45
  • 46. 46 Problem - Want to decouple an abstraction (i.e. class interface) from its implementation, allowing them to vary separately. Context - Want to change implementation details at run-time without impact to clients classes. Solution - Use delegation from an interface class to an implementation class. Consequences - Can change (or add) interface without re-implementing. - Can change implementation, but not impact interface for clients.
  • 47. 47 Inheritance ImplementorAbstraction implements ImplementorA ImplementorB Client The Abstraction class defines the interface visible to client. The Implementor class is an abstract implementation that defines the lower-level methods available to Abstraction – namely different “concrete” implementations such as ImplementorA or ImplementorB Delegation
  • 48. 48 Example Supporting multiple Database Vendors with a Bridge LeagueStoreImplementorLeagueStore implements XML Store Implementor SQL Server Store Implementor JDBC Store Implementor Arena LeagueStore is the interface class to the pattern Abstract interface provides common interface
  • 49.  The bridge pattern is used to provide multiple implementations under the same interface.  Decouples an interface from the implementation so that implementation can be substituted, possibly at runtime.  Example: Interface to a component that is incomplete, not yet known, or unavailable during testing 49
  • 50. 50 Consider problem of incrementally developing, testing and integrating subsystems realized by different project teams. Subsystems may be completed at different times, delaying the integration of all subsystems until the last one has been completed. To avoid delay, projects often use a stub implementations in place of a specific subsystem so that the integration tests can start even before the subsystems are completed.
  • 51. 51 Example of the bridge pattern
  • 52.  Similarities:  Both are used to hide the details of the underlying implementation.  Difference:  The adapter pattern is geared towards making unrelated components work together  Applied to systems after they are designed (reengineering, interface engineering).  A bridge, on the other hand, is used up-front in a design to let abstractions and implementations vary independently.  Engineering of an “extensible system”.  New components can be added to the system, even if these are not known at analysis or system design time. 52