Proxy Pattern

From Logic Wiki
Jump to: navigation, search


Video

https://www.youtube.com/watch?v=NwaabHqPHeM

Definition

In proxy pattern, a class represents functionality of another class. This type of design pattern comes under structural pattern.

In proxy pattern, we create object having original object to interface its functionality to outer world.

Proxy Types

  • Remote
  • Virtual : instead of calling an expensive resource, we call proxy and proxy caches the resources and returns a cheap result.
  • Protection

Proxy.gif

Implementation

Interface

interface IBookParser{
  int getNumPages();
}

Concrete

class BookParser: IBookParser{
  public BookParser(string book){
  // expensive parsing
  }
  public int getNumPages(){
    ...
  }
  }

Concrete Proxy

public class LazyBookParserProxy : IBookParser{
  private string _book;
  public LazyBookParserProxy(string book){
  _book = book;
  }
   private BookParser parser = null;
   public int getNumPages(){
       if(this.parser == null)
    {
         this.parser = new BookParser(_book)
    }
    return this.parser.getNumPages();
}