Proxy Design Pattern

Proxy Design Pattern

  • Comments 1

One of the useful design patterns is the proxy design pattern, it allows you to control access to an object via a proxy and also saves you the startup and cleanup overheads as you instantiate only what you use upon request (lazy initialization). Take a look at the following example:

#include<iostream>

#include<string>

 

using namespace std;

 

class Base { // Interface

public:

    virtual string run() = 0;

};

 

class Worker : public Base {

public:

    string run() { return "Worker"; }

};

 

class Proxy : public Base {

private:

    Worker* ptr;

 

    Worker* getWorker() { return ptr ? ptr : (ptr = new Worker()); }

 

public:

    Proxy() : ptr(NULL) {}

    string run() { return getWorker()->run() + " via Proxy"; }

    Worker * operator->() { return getWorker(); }

};

 

int main() {

    Proxy obj;

 

    cout << obj.run() << endl;  // Worker via Proxy

    cout << obj->run() << endl; // Worker

}

 

There are some points we need to note about the example. There’s an extra layer (one more class) that you need to add to be the proxy/controller. This proxy has a pointer to the actual worker object, it’s initialized to null and only instantiated upon request. That request is to be delegated to the worker to be processed while the proxy can add some logic such as pre-processing or post-processing, but it doesn’t have to because its main goal is to instantiate the worker object upon request. Using this pattern saves you the overhead of construction and destruction. Let’s assume that we have an array of 100 items, this will call the constructor 100 times and will call the destructor 100 times even if you didn’t use them. Using a proxy, you only create the object when it’s used, however there’s a slight overhead by doing that check every time you use it (whether it’s NULL or not), so it’s recommended to just create the objects and drop the check if you know that all of them are going to be used.

Leave a Comment
  • Please add 4 and 1 and type the answer here:
  • Post
  • Proxy Design Pattern in Java, lazy loading using Proxy Design Pattern

    What is Proxy Design Pattern

    1. Proxy design patten works on the principal of exposing an Java Instance through a proxy instead of actual object.

    2. Client would never know anything about actual object and through Proxy only relevant behavior of actual object will be exposed to client.

    3. Proxy Pattern can be used for applying security on actual Object. Service provider does not want actual class to be visible to any client Instead It would be shared as per Client contract agreement . Service provider may agree to share only a part of Service with it's client and for that It may expose a different contract in the form of interface in java .

    4. This concept is very useful for lazily loading an instance . Data will be loaded only when it is actually required in an operation .

    Learn more about proxy design pattern here  -- <a href="efectivejava.blogspot.in/.../proxy-design-pattern-in-java-lazy.html Design Pattern</a>

    efectivejava.blogspot.in/.../proxy-design-pattern-in-java-lazy.html

Page 1 of 1 (1 items)