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
PPTX
Proxy Pattern
PPSX
Proxy design pattern
PPTX
Proxy Design Patterns
PPT
Proxy pattern
PDF
Proxy design pattern (Class Ambassador)
PDF
Design patterns - Proxy & Composite
PDF
Dependency Injection
Proxy Design Pattern
Proxy Pattern
Proxy design pattern
Proxy Design Patterns
Proxy pattern
Proxy design pattern (Class Ambassador)
Design patterns - Proxy & Composite
Dependency Injection

What's hot (20)

PPT
Proxy pattern
PPTX
Desing pattern prototype-Factory Method, Prototype and Builder
PPTX
Dependency Injection Lightning Talk
KEY
Cocoa Design Patterns
PPTX
IoC and Mapper in C#
PPT
Design pattern
PPTX
SOLID Software Principles with C#
PDF
Large-Scale JavaScript Development
PPTX
Factory Design Pattern
PDF
Factory Design Pattern
PDF
Design Patterns in iOS
PDF
jQquerysummit - Large-scale JavaScript Application Architecture
PPT
Jump Start To Ooad And Design Patterns
PDF
Building Large Scale Javascript Application
PPTX
Scalable JavaScript Application Architecture 2012
PPTX
Dependency Injection in Drupal 8
PPTX
Factory method pattern
PPTX
Implementing The Open/Closed Principle
PPTX
Creating and destroying objects
PPTX
Spring FrameWork Tutorials Java Language
Proxy pattern
Desing pattern prototype-Factory Method, Prototype and Builder
Dependency Injection Lightning Talk
Cocoa Design Patterns
IoC and Mapper in C#
Design pattern
SOLID Software Principles with C#
Large-Scale JavaScript Development
Factory Design Pattern
Factory Design Pattern
Design Patterns in iOS
jQquerysummit - Large-scale JavaScript Application Architecture
Jump Start To Ooad And Design Patterns
Building Large Scale Javascript Application
Scalable JavaScript Application Architecture 2012
Dependency Injection in Drupal 8
Factory method pattern
Implementing The Open/Closed Principle
Creating and destroying objects
Spring FrameWork Tutorials Java Language
Ad

Similar to Design Patterns (20)

PPTX
UNIT IV DESIGN PATTERNS.pptx
PPTX
Common ASP.NET Design Patterns - Telerik India DevCon 2013
PPTX
Go f designpatterns 130116024923-phpapp02
PDF
Design patterns difference between interview questions
PPTX
Design pattern-presentation
PDF
The 23 gof design patterns in java ,the summary
PDF
The 23 gof design patterns in java ,the summary
PPT
JAVA design patterns and Basic OOp concepts
PPTX
Gof design patterns
PPTX
Most Useful Design Patterns
PPTX
Design patterns
PDF
UML Design Class Diagrams (2014)
PPTX
Design patterns
PDF
Lecture12
PPTX
Design patterns
PPTX
OOP design patterns
PDF
software engineering Design Patterns.pdf
PDF
Style & Design Principles 02 - Design Patterns
PDF
Design Pattern Cheatsheet
PPTX
Design pattern and their application
UNIT IV DESIGN PATTERNS.pptx
Common ASP.NET Design Patterns - Telerik India DevCon 2013
Go f designpatterns 130116024923-phpapp02
Design patterns difference between interview questions
Design pattern-presentation
The 23 gof design patterns in java ,the summary
The 23 gof design patterns in java ,the summary
JAVA design patterns and Basic OOp concepts
Gof design patterns
Most Useful Design Patterns
Design patterns
UML Design Class Diagrams (2014)
Design patterns
Lecture12
Design patterns
OOP design patterns
software engineering Design Patterns.pdf
Style & Design Principles 02 - Design Patterns
Design Pattern Cheatsheet
Design pattern and their application
Ad

More from Ahmad Hussein (7)

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

Recently uploaded (20)

PDF
CCleaner 6.39.11548 Crack 2025 License Key
PDF
PDF-XChange Editor Plus 10.7.0.398.0 Crack Free Download Latest 2025
PDF
CapCut PRO for PC Crack New Download (Fully Activated 2025)
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
PPTX
Computer Software - Technology and Livelihood Education
PDF
Guide to Food Delivery App Development.pdf
PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
PDF
Cloud Native Aachen Meetup - Aug 21, 2025
PDF
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
PDF
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
PDF
BoxLang Dynamic AWS Lambda - Japan Edition
PDF
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
PDF
Type Class Derivation in Scala 3 - Jose Luis Pintado Barbero
PPTX
Viber For Windows 25.7.1 Crack + Serial Keygen
PPTX
Airline CRS | Airline CRS Systems | CRS System
PDF
How Tridens DevSecOps Ensures Compliance, Security, and Agility
PPTX
4Seller: The All-in-One Multi-Channel E-Commerce Management Platform for Glob...
PPTX
CNN LeNet5 Architecture: Neural Networks
PPTX
Plex Media Server 1.28.2.6151 With Crac5 2022 Free .
DOC
UTEP毕业证学历认证,宾夕法尼亚克拉里恩大学毕业证未毕业
CCleaner 6.39.11548 Crack 2025 License Key
PDF-XChange Editor Plus 10.7.0.398.0 Crack Free Download Latest 2025
CapCut PRO for PC Crack New Download (Fully Activated 2025)
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
Computer Software - Technology and Livelihood Education
Guide to Food Delivery App Development.pdf
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
Cloud Native Aachen Meetup - Aug 21, 2025
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
BoxLang Dynamic AWS Lambda - Japan Edition
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
Type Class Derivation in Scala 3 - Jose Luis Pintado Barbero
Viber For Windows 25.7.1 Crack + Serial Keygen
Airline CRS | Airline CRS Systems | CRS System
How Tridens DevSecOps Ensures Compliance, Security, and Agility
4Seller: The All-in-One Multi-Channel E-Commerce Management Platform for Glob...
CNN LeNet5 Architecture: Neural Networks
Plex Media Server 1.28.2.6151 With Crac5 2022 Free .
UTEP毕业证学历认证,宾夕法尼亚克拉里恩大学毕业证未毕业

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