Sunday, December 13, 2015

Headfirst Design Patter Ch 11: Proxy Pattern

In this chapter we will talk about proxy pattern, the given example is not from the book

we have different types of proxy pattern,
Remote Proxy: you use this proxy to access remote objects, you define a stub and skeleton and start calling the remote object.

the other type is Virtual Proxy, in Virtual proxy you define as a proxy for another class, usually this class is expensive to create, so we use the proxy instead

example, an image class, the image needs to be loaded from the disk, this class will be considered as heavy, so we will define a proxy to be used instead of the class.

as you can see the RealImage class load the file from the disk

now we define a ProxyImage, which implements the same Image class.

The ProxyImage firstly load the image using RealImage object, after loading for the first time we start using this image through realImage.display().

now to use the code

NOTE: there is a difference between Facade and Proxy, in proxy you and the object has the same interface, however in facade you have totally a different interface.

No comments:

Post a Comment