Design patterns

Elyes Mejri
Elyes MejriFull Stack developer em Invia travel group / Leipzig
Design patterns are known as best practices that the programmer can use to solve common problems when designing an
application or system.
Classification of Design Patterns
Creational
Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of
existing code.
Structural
Structural patterns explain how to assemble objects and classes into larger structures while keeping these
structures flexible and efficient.
Behavioral
Behavioral patterns take care of effective communication and the assignment of responsibilities between
objects.
Singleton Design Pattern
Creational(1/4)
Is a creational design pattern that lets you ensure
that a class has only one instance, while providing
a global access point to this instance.
A Singleton encapsulates a unique resource and
makes it readily available throughout the
application
Singleton Design Pattern
Creational(1/4)When we use it ?
- When a class in your program should have just a single
instance available to all clients; for example, a single
database object shared by different parts of the program.
-Similarly, there can be a single configuration manager or
error manager in an application that handles all problems
instead of creating multiple managers.
-Centralise logging staff
Singleton Design Pattern
Creational(1/4)Bad things?
-Create a dependance for other classes who create the
instance
-Classes that depend on singletons are relatively harder
to unit test in isolation.Directly impacting testability and
maintainability
-The Singleton design pattern promotes tight coupling
between the classes in your application
The pattern requires special treatment in a
multithreaded environment so that multiple threads
won’t create a singleton object several times.
Singleton Design Pattern
Creational(1/4)Example
Singleton Design Pattern
Factory Design Pattern
Creational(2/4)
Factory is a creational design pattern that provides an
interface for creating objects in a superclass, but allows
subclasses to alter the type of objects that will be created.
-Allows the consumer to create new objects without having
to know the details of how they're created, or what their
dependencies are -. they only have to give the information
they actually want.
Factory Design Pattern
Creational(2/4)When we use it ?
- You don’t know beforehand the exact types and
dependencies of the objects your code should work with.
-Construction is very complex and you need to reuse it.
- You want to save system resources by reusing existing
objects instead of rebuilding them each time.
Factory Design Pattern
Creational(2/4)When NOT use it ?
- We don’t have any complex creation logic.
if the factory doesn’t make any decision, the creation logic
is very simple or is used only in a single place, the factory
will be a needless abstraction.
Factory Design Pattern
Creational(2/4)
Example
Builder Design Pattern Creational(3/4)
Builder pattern builds a complex object using simple objects
and using a step by step approach.The pattern allows you to
produce different types and representations of an object
using the same construction code.
This builder is independent of other objects.
Builder Design Pattern Creational(3/4)
When we use it ?
-When you want your code to be able to create different
representations of some product.
-When you want isolate complex construction code from the
business logic of the product.
Builder Design Pattern Creational(3/4)
Example
Prototype Design Pattern
Creational(4/4)
Prototype is a creational design pattern that lets you copy
existing objects without making your code dependent on
their classes.
Prototype Design Pattern
Creational(4/4)When we use it ?
-when creation of object directly is costly (heavy process).
For example, an object is to be created after a costly
database operation. We can cache the object, returns its
clone on next request and update the database as and when
needed thus reducing database calls.
Your code shouldn’t depend on the concrete classes of
objects that you need to copy.
Prototype Design Pattern
Creational(4/4)
Example
Decorator Design Pattern Structural(1/3)
Decorator is a structural design pattern that lets you attach
new behaviors to objects by placing these objects inside
special wrapper objects that contain the behaviors.
The decorator pattern allows a user to add new functionality
to an existing object without altering its structure.
Decorator Design Pattern Structural(1/3)
When we use it ?
-When you need to be able to assign extra behaviors to
objects at runtime without breaking the code that uses these
objects.
-When it’s difficult or not possible to extend an object’s
behavior using inheritance.
Decorator Design Pattern Structural(1/3)
Example
Proxy Design Pattern Structural(2/3)
Proxy is a structural design pattern that lets you provide a
substitute or placeholder for another object.
A proxy controls access to the original object, allowing
you to perform something either before or after the
request gets through to the original object.
The proxy could interface to anything: a network
connection, a large object in memory, a file, or some other
resource that is expensive or impossible to duplicate
Proxy Design Pattern Structural(2/3)
When we use it (1/2) ?
you want only specific clients to be able to use the
service object; for instance, when your objects are central
parts of an operating system and clients are various
launched applications (including malicious ones).
The proxy can pass the request to the service object only
if the client’s credentials match some criteria.(control
proxy)
Logging requests (logging proxy). This is when you want
to keep a history of requests to the service object.
The proxy can log each request before passing it to the
service.
Proxy Design Pattern Structural(2/3)
When we use it (2/2) ?
Caching request results (caching proxy). This is when you
need to cache results of client requests and manage the
life cycle of this cache, especially if results are quite large.
The proxy can implement caching for recurring requests
that always generate the same results
Proxy Design Pattern Structural(2/3)
Example
Adapter Design Pattern Structural(3/3)
Adapter is a structural design pattern that allows objects with
incompatible interfaces to collaborate.
This pattern involves a single class which is responsible to join
functionalities of independent or incompatible interfaces.
The Adapter pattern lets you create a middle-layer class that
serves as a translator between your code and a legacy class, a
3rd-party class or any other class with a weird interface.
Adapter Design Pattern Structural(3/3)
When we use it ?
-Use the Adapter class when you want to use some existing
class, but its interface isn’t compatible with the rest of your
code.
-Use the pattern when you want to reuse several existing
subclasses that lack some functionality that can’t be added to
the superclass.
-
Adapter Design Pattern Structural(3/3)
Example
Iterator Design Pattern
Behavioral(1/4)
This pattern is used to get a way to access the elements of a
collection object in a sequential manner without any need to know
its underlying representation. (list, stack, tree, complex data
structures.).
As name implies, iterator helps in traversing the collection of objects
in a defined manner which is useful the client applications.
Iterator Design Pattern
Behavioral(1/4)When we use it ?
-when your collection has a complex data structure under the hood,
but you want to hide its complexity from clients (either for
convenience or security reasons).
-when you want your code to be able to traverse different data
structures or when types of these structures are unknown
beforehand.
Iterator Design Pattern
Behavioral(1/4)
Example
Observer Design Pattern Behavioral(2/4)
Observer is a behavioral design pattern that lets you define a
subscription mechanism to notify multiple objects about any events
that happen to the object they’re observing.
Observer Design Pattern Behavioral(2/4)
When we use it ?
-When some objects in your app must observe others, but only for a
limited time or in specific cases.
-When changes to the state of one object may require changing
other objects, and the actual set of objects is unknown beforehand or
changes dynamically.
Observer Design Pattern Behavioral(2/4)
Example
Command Design Pattern Behavioral(3/4)
Command is a behavioral design pattern that turns a
request into a stand-alone object that contains all
information about the request. This transformation lets
you parameterize methods with different requests, delay
or queue a request’s execution, and support undoable
operations.
Command Design Pattern Behavioral(3/4)
-When you want to queue operations, schedule their
execution, or execute them remotely.
-when you want to implement reversible operations.
(undo/redo, the Command pattern is perhaps the most
popular of all.)
-Use the Command pattern when you want to parametrize
objects with operations.
( you can pass commands as method arguments, store
them inside other objects, switch linked commands at
runtime, etc.)
Command Design Pattern Behavioral(3/4)
Example
Strategy Design Pattern Behavioral (4/4)
Strategy is a behavioral design pattern that lets you
define a family of algorithms, put each of them into a
separate class, and make their objects
interchangeable.
Strategy Design Pattern Behavioral (4/4)
When we use it ?
-you want to use different variants of an algorithm
within an object and be able to switch from one
algorithm to another during runtime.
-you have a lot of similar classes that only differ in
the way they execute some behavior.
Strategy Design Pattern Behavioral (4/4)
Example
1 de 38

Recomendados

Software Design Patterns por
Software Design PatternsSoftware Design Patterns
Software Design PatternsSatheesh Sukumaran
1.2K visualizações26 slides
Introduction to design patterns por
Introduction to design patternsIntroduction to design patterns
Introduction to design patternsAmit Kabra
1.7K visualizações44 slides
Software Design Patterns por
Software Design PatternsSoftware Design Patterns
Software Design PatternsPankhuree Srivastava
2.6K visualizações52 slides
Introduction to Design Pattern por
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design PatternSanae BEKKAR
17K visualizações31 slides
Design pattern-presentation por
Design pattern-presentationDesign pattern-presentation
Design pattern-presentationRana Muhammad Asif
3.3K visualizações47 slides
Design patterns por
Design patternsDesign patterns
Design patternsabhisheksagi
14.9K visualizações28 slides

Mais conteúdo relacionado

Mais procurados

Let us understand design pattern por
Let us understand design patternLet us understand design pattern
Let us understand design patternMindfire Solutions
35.8K visualizações45 slides
Design Pattern - Singleton Pattern por
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton PatternMudasir Qazi
5.7K visualizações19 slides
Design pattern por
Design patternDesign pattern
Design patternThibaut De Broca
3.8K visualizações61 slides
The Singleton Pattern Presentation por
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern PresentationJAINIK PATEL
3.9K visualizações14 slides
Design Patterns Presentation - Chetan Gole por
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan GoleChetan Gole
12.1K visualizações24 slides
Design Patterns por
Design PatternsDesign Patterns
Design PatternsAnuja Arosha
13.5K visualizações56 slides

Mais procurados(20)

Let us understand design pattern por Mindfire Solutions
Let us understand design patternLet us understand design pattern
Let us understand design pattern
Mindfire Solutions35.8K visualizações
Design Pattern - Singleton Pattern por Mudasir Qazi
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton Pattern
Mudasir Qazi5.7K visualizações
Design pattern por Thibaut De Broca
Design patternDesign pattern
Design pattern
Thibaut De Broca3.8K visualizações
The Singleton Pattern Presentation por JAINIK PATEL
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern Presentation
JAINIK PATEL3.9K visualizações
Design Patterns Presentation - Chetan Gole por Chetan Gole
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan Gole
Chetan Gole12.1K visualizações
Design Patterns por Anuja Arosha
Design PatternsDesign Patterns
Design Patterns
Anuja Arosha13.5K visualizações
Design pattern & categories por Himanshu
Design pattern & categoriesDesign pattern & categories
Design pattern & categories
Himanshu 2.5K visualizações
Design Pattern por wiradikusuma
Design PatternDesign Pattern
Design Pattern
wiradikusuma1.7K visualizações
Gof design patterns por Srikanth R Vaka
Gof design patternsGof design patterns
Gof design patterns
Srikanth R Vaka19.1K visualizações
Design patterns ppt por Aman Jain
Design patterns pptDesign patterns ppt
Design patterns ppt
Aman Jain28.7K visualizações
Solid principles, Design Patterns, and Domain Driven Design por Irwansyah Irwansyah
Solid principles, Design Patterns, and Domain Driven DesignSolid principles, Design Patterns, and Domain Driven Design
Solid principles, Design Patterns, and Domain Driven Design
Irwansyah Irwansyah2.6K visualizações
Builder pattern por Shakil Ahmed
Builder patternBuilder pattern
Builder pattern
Shakil Ahmed6.7K visualizações
Introduction to Spring's Dependency Injection por Richard Paul
Introduction to Spring's Dependency InjectionIntroduction to Spring's Dependency Injection
Introduction to Spring's Dependency Injection
Richard Paul4K visualizações
Java Design Patterns Tutorial | Edureka por Edureka!
Java Design Patterns Tutorial | EdurekaJava Design Patterns Tutorial | Edureka
Java Design Patterns Tutorial | Edureka
Edureka!1.3K visualizações
Software design patterns ppt por mkruthika
Software design patterns pptSoftware design patterns ppt
Software design patterns ppt
mkruthika29.9K visualizações
Creational pattern por Himanshu
Creational patternCreational pattern
Creational pattern
Himanshu 3.3K visualizações
DDD In Agile por Skills Matter
DDD In Agile   DDD In Agile
DDD In Agile
Skills Matter7.5K visualizações
Design pattern (Abstract Factory & Singleton) por paramisoft
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
paramisoft9.4K visualizações
Mediator pattern por Shakil Ahmed
Mediator patternMediator pattern
Mediator pattern
Shakil Ahmed3.7K visualizações
Observer Software Design Pattern por Nirthika Rajendran
Observer Software Design Pattern Observer Software Design Pattern
Observer Software Design Pattern
Nirthika Rajendran949 visualizações

Similar a Design patterns

Design patterns por
Design patternsDesign patterns
Design patternsVignesh Nethaji
132 visualizações5 slides
Software Design Patterns por
Software Design PatternsSoftware Design Patterns
Software Design PatternsSatheesh Sukumaran
504 visualizações26 slides
OOP design patterns por
OOP design patternsOOP design patterns
OOP design patternsIgor Talevski
1.1K visualizações14 slides
Design Pattern Notes: Nagpur University por
Design Pattern Notes: Nagpur UniversityDesign Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur UniversityShubham Narkhede
1.4K visualizações28 slides
Design patterns por
Design patternsDesign patterns
Design patternsBinu Bhasuran
341 visualizações34 slides
Software Patterns por
Software PatternsSoftware Patterns
Software Patternsbonej010
1.2K visualizações44 slides

Similar a Design patterns(20)

Design patterns por Vignesh Nethaji
Design patternsDesign patterns
Design patterns
Vignesh Nethaji132 visualizações
Software Design Patterns por Satheesh Sukumaran
Software Design PatternsSoftware Design Patterns
Software Design Patterns
Satheesh Sukumaran504 visualizações
OOP design patterns por Igor Talevski
OOP design patternsOOP design patterns
OOP design patterns
Igor Talevski1.1K visualizações
Design Pattern Notes: Nagpur University por Shubham Narkhede
Design Pattern Notes: Nagpur UniversityDesign Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur University
Shubham Narkhede1.4K visualizações
Design patterns por Binu Bhasuran
Design patternsDesign patterns
Design patterns
Binu Bhasuran341 visualizações
Software Patterns por bonej010
Software PatternsSoftware Patterns
Software Patterns
bonej0101.2K visualizações
Design patterns por Ahmed Elharouny
Design patternsDesign patterns
Design patterns
Ahmed Elharouny1.1K visualizações
Typescript design patterns applied to sharepoint framework - Sharepoint Satur... por Luis Valencia
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Luis Valencia603 visualizações
Bartlesville Dot Net User Group Design Patterns por Jason Townsend, MBA
Bartlesville Dot Net User Group Design PatternsBartlesville Dot Net User Group Design Patterns
Bartlesville Dot Net User Group Design Patterns
Jason Townsend, MBA1.8K visualizações
Software design and Architecture.pptx por SHAHZAIBABBAS13
Software design and Architecture.pptxSoftware design and Architecture.pptx
Software design and Architecture.pptx
SHAHZAIBABBAS131 visão
ap assignmnet presentation.pptx por AwanAdhikari
ap assignmnet presentation.pptxap assignmnet presentation.pptx
ap assignmnet presentation.pptx
AwanAdhikari1 visão
UML Design Class Diagrams (2014) por Miriam Ruiz
UML Design Class Diagrams (2014)UML Design Class Diagrams (2014)
UML Design Class Diagrams (2014)
Miriam Ruiz2K visualizações
Design Pattern in Software Engineering por Bilal Hassan
Design Pattern in Software Engineering Design Pattern in Software Engineering
Design Pattern in Software Engineering
Bilal Hassan286 visualizações
Gof design pattern por naveen kumar
Gof design patternGof design pattern
Gof design pattern
naveen kumar388 visualizações
CSS422 Week2 individual por 19eric76
CSS422 Week2 individualCSS422 Week2 individual
CSS422 Week2 individual
19eric76210 visualizações
7 software design patterns you should know in 2022 por Growth Natives
7 software design patterns you should know in 20227 software design patterns you should know in 2022
7 software design patterns you should know in 2022
Growth Natives111 visualizações
Software Architecture and Project Management module III : PATTERN OF ENTERPRISE por sreeja_rajesh
Software Architecture and Project Management module III : PATTERN OF ENTERPRISESoftware Architecture and Project Management module III : PATTERN OF ENTERPRISE
Software Architecture and Project Management module III : PATTERN OF ENTERPRISE
sreeja_rajesh3.7K visualizações
The 23 gof design patterns in java ,the summary por guestebd714
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
guestebd714846 visualizações
The 23 gof design patterns in java ,the summary por achraf_ing
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
achraf_ing4.9K visualizações

Último

WebAssembly por
WebAssemblyWebAssembly
WebAssemblyJens Siebert
48 visualizações18 slides
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ... por
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Donato Onofri
825 visualizações34 slides
DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h... por
DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h...DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h...
DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h...Deltares
5 visualizações31 slides
Headless JS UG Presentation.pptx por
Headless JS UG Presentation.pptxHeadless JS UG Presentation.pptx
Headless JS UG Presentation.pptxJack Spektor
7 visualizações24 slides
EV Charging App Case por
EV Charging App Case EV Charging App Case
EV Charging App Case iCoderz Solutions
5 visualizações1 slide
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra... por
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra....NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...Marc Müller
38 visualizações62 slides

Último(20)

WebAssembly por Jens Siebert
WebAssemblyWebAssembly
WebAssembly
Jens Siebert48 visualizações
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ... por Donato Onofri
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Donato Onofri825 visualizações
DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h... por Deltares
DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h...DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h...
DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h...
Deltares5 visualizações
Headless JS UG Presentation.pptx por Jack Spektor
Headless JS UG Presentation.pptxHeadless JS UG Presentation.pptx
Headless JS UG Presentation.pptx
Jack Spektor7 visualizações
EV Charging App Case por iCoderz Solutions
EV Charging App Case EV Charging App Case
EV Charging App Case
iCoderz Solutions5 visualizações
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra... por Marc Müller
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra....NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
Marc Müller38 visualizações
nintendo_64.pptx por paiga02016
nintendo_64.pptxnintendo_64.pptx
nintendo_64.pptx
paiga020165 visualizações
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... por Deltares
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
Deltares9 visualizações
Advanced API Mocking Techniques por Dimpy Adhikary
Advanced API Mocking TechniquesAdvanced API Mocking Techniques
Advanced API Mocking Techniques
Dimpy Adhikary19 visualizações
Airline Booking Software por SharmiMehta
Airline Booking SoftwareAirline Booking Software
Airline Booking Software
SharmiMehta6 visualizações
FIMA 2023 Neo4j & FS - Entity Resolution.pptx por Neo4j
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptx
Neo4j7 visualizações
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI... por Marc Müller
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...
Marc Müller37 visualizações
20231129 - Platform @ localhost 2023 - Application-driven infrastructure with... por sparkfabrik
20231129 - Platform @ localhost 2023 - Application-driven infrastructure with...20231129 - Platform @ localhost 2023 - Application-driven infrastructure with...
20231129 - Platform @ localhost 2023 - Application-driven infrastructure with...
sparkfabrik5 visualizações
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P... por NimaTorabi2
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...
NimaTorabi28 visualizações
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx por animuscrm
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
animuscrm14 visualizações
Quality Engineer: A Day in the Life por John Valentino
Quality Engineer: A Day in the LifeQuality Engineer: A Day in the Life
Quality Engineer: A Day in the Life
John Valentino6 visualizações
Unleash The Monkeys por Jacob Duijzer
Unleash The MonkeysUnleash The Monkeys
Unleash The Monkeys
Jacob Duijzer7 visualizações
The Era of Large Language Models.pptx por AbdulVahedShaik
The Era of Large Language Models.pptxThe Era of Large Language Models.pptx
The Era of Large Language Models.pptx
AbdulVahedShaik5 visualizações
Dapr Unleashed: Accelerating Microservice Development por Miroslav Janeski
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice Development
Miroslav Janeski10 visualizações

Design patterns

  • 1. Design patterns are known as best practices that the programmer can use to solve common problems when designing an application or system.
  • 2. Classification of Design Patterns Creational Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code. Structural Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient. Behavioral Behavioral patterns take care of effective communication and the assignment of responsibilities between objects.
  • 3. Singleton Design Pattern Creational(1/4) Is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance. A Singleton encapsulates a unique resource and makes it readily available throughout the application
  • 4. Singleton Design Pattern Creational(1/4)When we use it ? - When a class in your program should have just a single instance available to all clients; for example, a single database object shared by different parts of the program. -Similarly, there can be a single configuration manager or error manager in an application that handles all problems instead of creating multiple managers. -Centralise logging staff
  • 5. Singleton Design Pattern Creational(1/4)Bad things? -Create a dependance for other classes who create the instance -Classes that depend on singletons are relatively harder to unit test in isolation.Directly impacting testability and maintainability -The Singleton design pattern promotes tight coupling between the classes in your application The pattern requires special treatment in a multithreaded environment so that multiple threads won’t create a singleton object several times.
  • 7. Factory Design Pattern Creational(2/4) Factory is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. -Allows the consumer to create new objects without having to know the details of how they're created, or what their dependencies are -. they only have to give the information they actually want.
  • 8. Factory Design Pattern Creational(2/4)When we use it ? - You don’t know beforehand the exact types and dependencies of the objects your code should work with. -Construction is very complex and you need to reuse it. - You want to save system resources by reusing existing objects instead of rebuilding them each time.
  • 9. Factory Design Pattern Creational(2/4)When NOT use it ? - We don’t have any complex creation logic. if the factory doesn’t make any decision, the creation logic is very simple or is used only in a single place, the factory will be a needless abstraction.
  • 11. Builder Design Pattern Creational(3/4) Builder pattern builds a complex object using simple objects and using a step by step approach.The pattern allows you to produce different types and representations of an object using the same construction code. This builder is independent of other objects.
  • 12. Builder Design Pattern Creational(3/4) When we use it ? -When you want your code to be able to create different representations of some product. -When you want isolate complex construction code from the business logic of the product.
  • 13. Builder Design Pattern Creational(3/4) Example
  • 14. Prototype Design Pattern Creational(4/4) Prototype is a creational design pattern that lets you copy existing objects without making your code dependent on their classes.
  • 15. Prototype Design Pattern Creational(4/4)When we use it ? -when creation of object directly is costly (heavy process). For example, an object is to be created after a costly database operation. We can cache the object, returns its clone on next request and update the database as and when needed thus reducing database calls. Your code shouldn’t depend on the concrete classes of objects that you need to copy.
  • 17. Decorator Design Pattern Structural(1/3) Decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors. The decorator pattern allows a user to add new functionality to an existing object without altering its structure.
  • 18. Decorator Design Pattern Structural(1/3) When we use it ? -When you need to be able to assign extra behaviors to objects at runtime without breaking the code that uses these objects. -When it’s difficult or not possible to extend an object’s behavior using inheritance.
  • 19. Decorator Design Pattern Structural(1/3) Example
  • 20. Proxy Design Pattern Structural(2/3) Proxy is a structural design pattern that lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate
  • 21. Proxy Design Pattern Structural(2/3) When we use it (1/2) ? you want only specific clients to be able to use the service object; for instance, when your objects are central parts of an operating system and clients are various launched applications (including malicious ones). The proxy can pass the request to the service object only if the client’s credentials match some criteria.(control proxy) Logging requests (logging proxy). This is when you want to keep a history of requests to the service object. The proxy can log each request before passing it to the service.
  • 22. Proxy Design Pattern Structural(2/3) When we use it (2/2) ? Caching request results (caching proxy). This is when you need to cache results of client requests and manage the life cycle of this cache, especially if results are quite large. The proxy can implement caching for recurring requests that always generate the same results
  • 23. Proxy Design Pattern Structural(2/3) Example
  • 24. Adapter Design Pattern Structural(3/3) Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate. This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces. The Adapter pattern lets you create a middle-layer class that serves as a translator between your code and a legacy class, a 3rd-party class or any other class with a weird interface.
  • 25. Adapter Design Pattern Structural(3/3) When we use it ? -Use the Adapter class when you want to use some existing class, but its interface isn’t compatible with the rest of your code. -Use the pattern when you want to reuse several existing subclasses that lack some functionality that can’t be added to the superclass. -
  • 26. Adapter Design Pattern Structural(3/3) Example
  • 27. Iterator Design Pattern Behavioral(1/4) This pattern is used to get a way to access the elements of a collection object in a sequential manner without any need to know its underlying representation. (list, stack, tree, complex data structures.). As name implies, iterator helps in traversing the collection of objects in a defined manner which is useful the client applications.
  • 28. Iterator Design Pattern Behavioral(1/4)When we use it ? -when your collection has a complex data structure under the hood, but you want to hide its complexity from clients (either for convenience or security reasons). -when you want your code to be able to traverse different data structures or when types of these structures are unknown beforehand.
  • 30. Observer Design Pattern Behavioral(2/4) Observer is a behavioral design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.
  • 31. Observer Design Pattern Behavioral(2/4) When we use it ? -When some objects in your app must observe others, but only for a limited time or in specific cases. -When changes to the state of one object may require changing other objects, and the actual set of objects is unknown beforehand or changes dynamically.
  • 32. Observer Design Pattern Behavioral(2/4) Example
  • 33. Command Design Pattern Behavioral(3/4) Command is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request. This transformation lets you parameterize methods with different requests, delay or queue a request’s execution, and support undoable operations.
  • 34. Command Design Pattern Behavioral(3/4) -When you want to queue operations, schedule their execution, or execute them remotely. -when you want to implement reversible operations. (undo/redo, the Command pattern is perhaps the most popular of all.) -Use the Command pattern when you want to parametrize objects with operations. ( you can pass commands as method arguments, store them inside other objects, switch linked commands at runtime, etc.)
  • 35. Command Design Pattern Behavioral(3/4) Example
  • 36. Strategy Design Pattern Behavioral (4/4) Strategy is a behavioral design pattern that lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.
  • 37. Strategy Design Pattern Behavioral (4/4) When we use it ? -you want to use different variants of an algorithm within an object and be able to switch from one algorithm to another during runtime. -you have a lot of similar classes that only differ in the way they execute some behavior.
  • 38. Strategy Design Pattern Behavioral (4/4) Example

Notas do Editor

  1. A toolkit of typical and tested solutions to common problems in software design. - Even if you never encounter these problems, knowing patterns is still useful because it teaches you how to solve all sorts of problems using principles of object-oriented design. - Design patterns define a common language that you and your teammates can use to communicate more efficiently. You can say, “Oh, just use a Singleton for that,” and everyone will understand the idea behind your suggestion. No need to explain what a singleton is if you know the pattern and its name.
  2. are all about class instantiation or object creation. [Singelton / Factory /builder / Prototype ] are about organizing different classes and objects to form larger structures and provide new functionality. [ Decorator / Proxy /Adapter ] about identifying common communication patterns between objects [iterator / Observer /Command /Strategy]