SlideShare uma empresa Scribd logo
1 de 37
Baixar para ler offline
DESIGN PATTERNS
AHMAD HUSSEIN
1ahmadhussein.ah7@gmail.com
LAZY LOADING
2
DEFINITION
"Lazy loading is a design pattern commonly used in
computer programming to defer initialization of an
object until the point at which it is needed. It can
contribute to efficiency in the program's operation
if properly and appropriately used. The opposite of
lazy loading is eager loading.“
-- Wikipedia
3
WHAT DOES THAT MEAN?
You want to be as lazy as possible, and hope that
someone else comes along and does all the work for
you.
4
WAIT WHAT?
Lazy loading really means waiting to get data or
process an object until you need it; not before. If you
get data before and it's never used, the time it took
was wasted. Also, getting all your data up front can
make the user think the application is slow or
unresponsive at first. Deferring certain
data/processing to lazy load lets your application
focus on more immediate/important code.
5
SIMPLE EXAMPLE
6
IMPLEMENTATIONS
There are four common implementations:
• Lazy initialization
• Virtual proxy
• Ghost
• Value holder
7
LAZY INITIALIZATION
Constructors are used to construct the member
variables of a class. As soon as the instance of the class is
created the member variables of the class are initialized.
This process seems fine if we have member variables
which don't take huge memory. But suppose if we have a
memory consuming resource and if we are initializing it
as member variable, it can be prove to be memory
hungry process.
To prevent from initializing the member variable as
soon as the instance of class is constructed we can define
member variable as property which can be initialized on
demand(Lazy initialization). Let's check it through code
snippet.
8
LAZY INITIALIZATION
9
LAZY INITIALIZATION
Is the code at the previous slide is thread safe?
Definitely not. What if two threads enter if block at
the same time, we will end up getting different
instance of Hungry for the ResourceConsumer class
which is not a good thing.
To make it thread safe we need to have a lock
outside if block as shown in the following code
snippet.
10
LAZY INITIALIZATION
11
LAZY INITIALIZATION
Lazy<T>
Starting framework 4.0, Lazy<T> class can be used
to help with lazy initialization. If instantiated with
an argument of true, it implements a thread-
safe initialization pattern just described.
To use the Lazy<T>, we need to instantiate the class
with the delegate that tells it how to instantiate a
new value, and the argument true. We need to
access its value via the Value property
12
LAZY INITIALIZATION
13
VIRTUAL PROXY
A virtual proxy object shares an interface with the
"real" object. The first time a property of the virtual
proxy is accessed, the real object gets initialized.
From that point on, accessing properties of the
virtual object returns the corresponding property of
the real object.
This basically combines the Lazy initialization and
Proxy patterns
14
VIRTUAL PROXY
Example:
15
VIRTUAL PROXY
16
VIRTUAL PROXY
17
GHOST
The object is partially loaded, usually only with its
identifier. The rest of the data is loaded the first
time a property is accessed.
18
GHOST
Continue
19
GHOST
20
VALUE HOLDER
A value holder is a generic object that handles the
lazy loading behavior and appears in place of the
object’s data fields. When the user needs to access it,
they simply ask the value holder for its value by
calling the GetValue method. At that time (and only
then), the value gets loaded from a database or
from a service.(this is not always needed).
21
VALUE HOLDER
22
DIFFERENCE BETWEEN ADAPTER
PATTERN, PROXY PATTERN AND
BRIDGE PATTERN
23
ADAPTER PATTERN
• The primary purpose of the adapter pattern is to
change the interface of class/library A to the
expectations of client B.
• The typical implementation is a wrapper class or
set of classes.
• The purpose is not to facilitate future interface
changes, but current interface incompatibilities.
24
STRUCTURE
25
PROXY PATTERN
• The purpose of the proxy pattern is to create a
stand-in for a real resource. There are common
situations in which the Proxy pattern is applicable:
• A virtual proxy is a placeholder for "expensive to
create" objects. The real object is only created when a
client first requests/accesses the object.
• A remote proxy provides a local representative for an
object that resides in a different address space.
• A protective proxy controls access to a sensitive
master object.
26
STRUCTURE
27
KEY DIFFERENCES
• Adapter provides a different interface to its
subject. Proxy provides the same interface.
• Adapter is meant to change the interface of an
existing object.
28
BRIDGE PATTERN
• The Bridge pattern is something you implement up
front - if you know you have two orthogonal
hierarchies, it provides a way to decouple the
interface and the implementation in such a way
that you don't get an insane number of classes.
29
BRIDGE PATTERN
• Let's say you have:
30
STRUCTURE
31
KEY DIFFERENCES
• Adapter makes things work after they're designed;
Bridge makes them work before they are.
• Bridge is designed up-front to let the abstraction
and the implementation vary independently.
Adapter is retrofitted to make unrelated classes
work together.
32
DIFFERENCE BETWEEN FACTORY
PATTERN AND BUILDER PATTERN
33
FACTORY PATTERN
• The factory is in charge of creating various
subtypes of an object depending on the needs.
• The user of a factory method doesn't need to know
the exact subtype of that object. An example of a
factory method createCar might return a Ford or a
Honda typed object.
34
STRUCTURE
35
BUILDER PATTERN
• In the Builder pattern, different subtypes are also
created by a builder method, but the composition
of the objects might differ within the same
subclass.
• To continue the car example you might have a
createCar builder method which creates a Honda
typed object with a 4 cylinder engine, or a Honda
typed object with 6 cylinders. The builder pattern
allows for this finer granularity.
36
STRUCTURE
37

Mais conteúdo relacionado

Mais procurados

AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere
Miklos Csere
 
Implementing The Open/Closed Principle
Implementing The Open/Closed PrincipleImplementing The Open/Closed Principle
Implementing The Open/Closed Principle
Sam Hennessy
 

Mais procurados (20)

Proxy pattern
Proxy patternProxy pattern
Proxy pattern
 
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
 
Dependency Injection Lightning Talk
Dependency Injection Lightning TalkDependency Injection Lightning Talk
Dependency Injection Lightning Talk
 
Cocoa Design Patterns
Cocoa Design PatternsCocoa Design Patterns
Cocoa Design Patterns
 
IoC and Mapper in C#
IoC and Mapper in C#IoC and Mapper in C#
IoC and Mapper in C#
 
Design pattern
Design patternDesign pattern
Design pattern
 
SOLID Software Principles with C#
SOLID Software Principles with C#SOLID Software Principles with C#
SOLID Software Principles with C#
 
Large-Scale JavaScript Development
Large-Scale JavaScript DevelopmentLarge-Scale JavaScript Development
Large-Scale JavaScript Development
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
 
Design Patterns in iOS
Design Patterns in iOSDesign Patterns in iOS
Design Patterns in iOS
 
jQquerysummit - Large-scale JavaScript Application Architecture
jQquerysummit - Large-scale JavaScript Application Architecture jQquerysummit - Large-scale JavaScript Application Architecture
jQquerysummit - Large-scale JavaScript Application Architecture
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
 
Building Large Scale Javascript Application
Building Large Scale Javascript ApplicationBuilding Large Scale Javascript Application
Building Large Scale Javascript Application
 
Scalable JavaScript Application Architecture 2012
Scalable JavaScript Application Architecture 2012Scalable JavaScript Application Architecture 2012
Scalable JavaScript Application Architecture 2012
 
Dependency Injection in Drupal 8
Dependency Injection in Drupal 8Dependency Injection in Drupal 8
Dependency Injection in Drupal 8
 
Factory method pattern
Factory method patternFactory method pattern
Factory method pattern
 
AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere
 
Implementing The Open/Closed Principle
Implementing The Open/Closed PrincipleImplementing The Open/Closed Principle
Implementing The Open/Closed Principle
 
Creating and destroying objects
Creating and destroying objectsCreating and destroying objects
Creating and destroying objects
 

Semelhante a Design Patterns

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
 
Ratpack and Grails 3 (and Spring Boot) SpringOne 2GX 2014
Ratpack and Grails 3 (and Spring Boot) SpringOne 2GX 2014Ratpack and Grails 3 (and Spring Boot) SpringOne 2GX 2014
Ratpack and Grails 3 (and Spring Boot) SpringOne 2GX 2014
Lari Hotari
 
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
cNguyn506241
 

Semelhante a Design Patterns (20)

Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural Patterns
 
Android Dagger 2
Android  Dagger 2Android  Dagger 2
Android Dagger 2
 
50 common web developer interview questions [2020 updated] [www.full stack....
50 common web developer interview questions [2020 updated]   [www.full stack....50 common web developer interview questions [2020 updated]   [www.full stack....
50 common web developer interview questions [2020 updated] [www.full stack....
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofac
 
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)
 
Ratpack and Grails 3 (and Spring Boot) SpringOne 2GX 2014
Ratpack and Grails 3 (and Spring Boot) SpringOne 2GX 2014Ratpack and Grails 3 (and Spring Boot) SpringOne 2GX 2014
Ratpack and Grails 3 (and Spring Boot) SpringOne 2GX 2014
 
AngularJS 1.x - your first application (problems and solutions)
AngularJS 1.x - your first application (problems and solutions)AngularJS 1.x - your first application (problems and solutions)
AngularJS 1.x - your first application (problems and solutions)
 
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckCut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
 
Gof design patterns
Gof design patternsGof design patterns
Gof design patterns
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 
Dependency injection Drupal Camp Wrocław 2014
Dependency injection Drupal Camp Wrocław 2014Dependency injection Drupal Camp Wrocław 2014
Dependency injection Drupal Camp Wrocław 2014
 
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
 
Polaris presentation ioc - code conference
Polaris presentation   ioc - code conferencePolaris presentation   ioc - code conference
Polaris presentation ioc - code conference
 
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
 
Decorator design pattern
Decorator design patternDecorator design pattern
Decorator design pattern
 
Tcs NQTExam technical questions
Tcs NQTExam technical questionsTcs NQTExam technical questions
Tcs NQTExam technical questions
 
JavaFX in Action Part I
JavaFX in Action Part IJavaFX in Action Part I
JavaFX in Action Part I
 
cf.Objective() 2017 - Design patterns - Brad Wood
cf.Objective() 2017 - Design patterns - Brad Woodcf.Objective() 2017 - Design patterns - Brad Wood
cf.Objective() 2017 - Design patterns - Brad Wood
 
Introduction to Behavior Driven Development
Introduction to Behavior Driven Development Introduction to Behavior Driven Development
Introduction to Behavior Driven Development
 
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
Cut your Dependencies - Dependency Injection at Silicon Valley Code CampCut your Dependencies - Dependency Injection at Silicon Valley Code Camp
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
 

Mais de Ahmad Hussein

Mais de Ahmad Hussein (7)

Knowledge Representation Methods
Knowledge Representation MethodsKnowledge Representation Methods
Knowledge Representation Methods
 
Knowledge-Base Systems Homework - 2019
Knowledge-Base Systems Homework - 2019Knowledge-Base Systems Homework - 2019
Knowledge-Base Systems Homework - 2019
 
Knowledge based systems homework
Knowledge based systems homeworkKnowledge based systems homework
Knowledge based systems homework
 
Expert system with python -2
Expert system with python  -2Expert system with python  -2
Expert system with python -2
 
Expert System With Python -1
Expert System With Python -1Expert System With Python -1
Expert System With Python -1
 
Python introduction 2
Python introduction 2Python introduction 2
Python introduction 2
 
Python introduction 1
Python introduction 1  Python introduction 1
Python introduction 1
 

Último

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Último (20)

WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 

Design Patterns

  • 3. DEFINITION "Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program's operation if properly and appropriately used. The opposite of lazy loading is eager loading.“ -- Wikipedia 3
  • 4. WHAT DOES THAT MEAN? You want to be as lazy as possible, and hope that someone else comes along and does all the work for you. 4
  • 5. WAIT WHAT? Lazy loading really means waiting to get data or process an object until you need it; not before. If you get data before and it's never used, the time it took was wasted. Also, getting all your data up front can make the user think the application is slow or unresponsive at first. Deferring certain data/processing to lazy load lets your application focus on more immediate/important code. 5
  • 7. IMPLEMENTATIONS There are four common implementations: • Lazy initialization • Virtual proxy • Ghost • Value holder 7
  • 8. LAZY INITIALIZATION Constructors are used to construct the member variables of a class. As soon as the instance of the class is created the member variables of the class are initialized. This process seems fine if we have member variables which don't take huge memory. But suppose if we have a memory consuming resource and if we are initializing it as member variable, it can be prove to be memory hungry process. To prevent from initializing the member variable as soon as the instance of class is constructed we can define member variable as property which can be initialized on demand(Lazy initialization). Let's check it through code snippet. 8
  • 10. LAZY INITIALIZATION Is the code at the previous slide is thread safe? Definitely not. What if two threads enter if block at the same time, we will end up getting different instance of Hungry for the ResourceConsumer class which is not a good thing. To make it thread safe we need to have a lock outside if block as shown in the following code snippet. 10
  • 12. LAZY INITIALIZATION Lazy<T> Starting framework 4.0, Lazy<T> class can be used to help with lazy initialization. If instantiated with an argument of true, it implements a thread- safe initialization pattern just described. To use the Lazy<T>, we need to instantiate the class with the delegate that tells it how to instantiate a new value, and the argument true. We need to access its value via the Value property 12
  • 14. VIRTUAL PROXY A virtual proxy object shares an interface with the "real" object. The first time a property of the virtual proxy is accessed, the real object gets initialized. From that point on, accessing properties of the virtual object returns the corresponding property of the real object. This basically combines the Lazy initialization and Proxy patterns 14
  • 18. GHOST The object is partially loaded, usually only with its identifier. The rest of the data is loaded the first time a property is accessed. 18
  • 21. VALUE HOLDER A value holder is a generic object that handles the lazy loading behavior and appears in place of the object’s data fields. When the user needs to access it, they simply ask the value holder for its value by calling the GetValue method. At that time (and only then), the value gets loaded from a database or from a service.(this is not always needed). 21
  • 23. DIFFERENCE BETWEEN ADAPTER PATTERN, PROXY PATTERN AND BRIDGE PATTERN 23
  • 24. ADAPTER PATTERN • The primary purpose of the adapter pattern is to change the interface of class/library A to the expectations of client B. • The typical implementation is a wrapper class or set of classes. • The purpose is not to facilitate future interface changes, but current interface incompatibilities. 24
  • 26. PROXY PATTERN • The purpose of the proxy pattern is to create a stand-in for a real resource. There are common situations in which the Proxy pattern is applicable: • A virtual proxy is a placeholder for "expensive to create" objects. The real object is only created when a client first requests/accesses the object. • A remote proxy provides a local representative for an object that resides in a different address space. • A protective proxy controls access to a sensitive master object. 26
  • 28. KEY DIFFERENCES • Adapter provides a different interface to its subject. Proxy provides the same interface. • Adapter is meant to change the interface of an existing object. 28
  • 29. BRIDGE PATTERN • The Bridge pattern is something you implement up front - if you know you have two orthogonal hierarchies, it provides a way to decouple the interface and the implementation in such a way that you don't get an insane number of classes. 29
  • 30. BRIDGE PATTERN • Let's say you have: 30
  • 32. KEY DIFFERENCES • Adapter makes things work after they're designed; Bridge makes them work before they are. • Bridge is designed up-front to let the abstraction and the implementation vary independently. Adapter is retrofitted to make unrelated classes work together. 32
  • 33. DIFFERENCE BETWEEN FACTORY PATTERN AND BUILDER PATTERN 33
  • 34. FACTORY PATTERN • The factory is in charge of creating various subtypes of an object depending on the needs. • The user of a factory method doesn't need to know the exact subtype of that object. An example of a factory method createCar might return a Ford or a Honda typed object. 34
  • 36. BUILDER PATTERN • In the Builder pattern, different subtypes are also created by a builder method, but the composition of the objects might differ within the same subclass. • To continue the car example you might have a createCar builder method which creates a Honda typed object with a 4 cylinder engine, or a Honda typed object with 6 cylinders. The builder pattern allows for this finer granularity. 36