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
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
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
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
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
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