SlideShare a Scribd company logo
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

More Related Content

PPTX
Proxy Design Pattern
Anjan Kumar Bollam
 
PPTX
Proxy Pattern
Hüseyin Ergin
 
PPSX
Proxy design pattern
Sase Kleckovski
 
PPTX
Proxy Design Patterns
Zafer Genc
 
PPT
Proxy pattern
Chester Hartin
 
PDF
Proxy design pattern (Class Ambassador)
Sameer Rathoud
 
PDF
Design patterns - Proxy & Composite
Sarath C
 
PDF
Dependency Injection
Giovanni Scerra ☃
 
Proxy Design Pattern
Anjan Kumar Bollam
 
Proxy Pattern
Hüseyin Ergin
 
Proxy design pattern
Sase Kleckovski
 
Proxy Design Patterns
Zafer Genc
 
Proxy pattern
Chester Hartin
 
Proxy design pattern (Class Ambassador)
Sameer Rathoud
 
Design patterns - Proxy & Composite
Sarath C
 
Dependency Injection
Giovanni Scerra ☃
 

What's hot (20)

PPT
Proxy pattern
Shakil Ahmed
 
PPTX
Desing pattern prototype-Factory Method, Prototype and Builder
paramisoft
 
PPTX
Dependency Injection Lightning Talk
Jon Bachelor
 
KEY
Cocoa Design Patterns
sgleadow
 
PPTX
IoC and Mapper in C#
Huy Hoàng Phạm
 
PPT
Design pattern
Mallikarjuna G D
 
PPTX
SOLID Software Principles with C#
Ken Burkhardt
 
PDF
Large-Scale JavaScript Development
Addy Osmani
 
PPTX
Factory Design Pattern
Jaswant Singh
 
PDF
Factory Design Pattern
Jyaasa Technologies
 
PDF
Design Patterns in iOS
Yi-Shou Chen
 
PDF
jQquerysummit - Large-scale JavaScript Application Architecture
Jiby John
 
PPT
Jump Start To Ooad And Design Patterns
Lalit Kale
 
PDF
Building Large Scale Javascript Application
Anis Ahmad
 
PPTX
Scalable JavaScript Application Architecture 2012
Nicholas Zakas
 
PPTX
Dependency Injection in Drupal 8
valuebound
 
PPTX
Factory method pattern
Shahriar Iqbal Chowdhury
 
PPTX
Implementing The Open/Closed Principle
Sam Hennessy
 
PPTX
Creating and destroying objects
Sandeep Chawla
 
PPTX
Spring FrameWork Tutorials Java Language
Mahika Tutorials
 
Proxy pattern
Shakil Ahmed
 
Desing pattern prototype-Factory Method, Prototype and Builder
paramisoft
 
Dependency Injection Lightning Talk
Jon Bachelor
 
Cocoa Design Patterns
sgleadow
 
IoC and Mapper in C#
Huy Hoàng Phạm
 
Design pattern
Mallikarjuna G D
 
SOLID Software Principles with C#
Ken Burkhardt
 
Large-Scale JavaScript Development
Addy Osmani
 
Factory Design Pattern
Jaswant Singh
 
Factory Design Pattern
Jyaasa Technologies
 
Design Patterns in iOS
Yi-Shou Chen
 
jQquerysummit - Large-scale JavaScript Application Architecture
Jiby John
 
Jump Start To Ooad And Design Patterns
Lalit Kale
 
Building Large Scale Javascript Application
Anis Ahmad
 
Scalable JavaScript Application Architecture 2012
Nicholas Zakas
 
Dependency Injection in Drupal 8
valuebound
 
Factory method pattern
Shahriar Iqbal Chowdhury
 
Implementing The Open/Closed Principle
Sam Hennessy
 
Creating and destroying objects
Sandeep Chawla
 
Spring FrameWork Tutorials Java Language
Mahika Tutorials
 
Ad

Similar to Design Patterns (20)

PPTX
UNIT IV DESIGN PATTERNS.pptx
anguraju1
 
PPTX
Common ASP.NET Design Patterns - Telerik India DevCon 2013
Steven Smith
 
PPTX
Go f designpatterns 130116024923-phpapp02
Jagath Bandara Senanayaka
 
PDF
Design patterns difference between interview questions
Umar Ali
 
PPTX
Design pattern-presentation
Rana Muhammad Asif
 
PDF
The 23 gof design patterns in java ,the summary
achraf_ing
 
PDF
The 23 gof design patterns in java ,the summary
guestebd714
 
PPT
JAVA design patterns and Basic OOp concepts
Rahul Malhotra
 
PPTX
Gof design patterns
Srikanth R Vaka
 
PPTX
Most Useful Design Patterns
Steven Smith
 
PPTX
Design patterns
Alok Guha
 
PDF
UML Design Class Diagrams (2014)
Miriam Ruiz
 
PPTX
Design patterns
Akhilesh Joshi
 
PDF
Lecture12
artgreen
 
PPTX
Design patterns
Ahmed Elharouny
 
PPTX
OOP design patterns
Igor Talevski
 
PDF
software engineering Design Patterns.pdf
mulugetaberihun3
 
PDF
Style & Design Principles 02 - Design Patterns
Nick Pruehs
 
PDF
Design Pattern Cheatsheet
Rachanee Saengkrajai
 
PPTX
Design pattern and their application
Hiệp Tiến
 
UNIT IV DESIGN PATTERNS.pptx
anguraju1
 
Common ASP.NET Design Patterns - Telerik India DevCon 2013
Steven Smith
 
Go f designpatterns 130116024923-phpapp02
Jagath Bandara Senanayaka
 
Design patterns difference between interview questions
Umar Ali
 
Design pattern-presentation
Rana Muhammad Asif
 
The 23 gof design patterns in java ,the summary
achraf_ing
 
The 23 gof design patterns in java ,the summary
guestebd714
 
JAVA design patterns and Basic OOp concepts
Rahul Malhotra
 
Gof design patterns
Srikanth R Vaka
 
Most Useful Design Patterns
Steven Smith
 
Design patterns
Alok Guha
 
UML Design Class Diagrams (2014)
Miriam Ruiz
 
Design patterns
Akhilesh Joshi
 
Lecture12
artgreen
 
Design patterns
Ahmed Elharouny
 
OOP design patterns
Igor Talevski
 
software engineering Design Patterns.pdf
mulugetaberihun3
 
Style & Design Principles 02 - Design Patterns
Nick Pruehs
 
Design Pattern Cheatsheet
Rachanee Saengkrajai
 
Design pattern and their application
Hiệp Tiến
 
Ad

More from Ahmad Hussein (7)

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

Recently uploaded (20)

PDF
Exploring AI Agents in Process Industries
amoreira6
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PDF
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PDF
Immersive experiences: what Pharo users do!
ESUG
 
DOCX
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PPTX
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
PDF
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
Exploring AI Agents in Process Industries
amoreira6
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Activate_Methodology_Summary presentatio
annapureddyn
 
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
Immersive experiences: what Pharo users do!
ESUG
 
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
Explanation about Structures in C language.pptx
Veeral Rathod
 

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