রবিবার, ১৭ নভেম্বর, ২০১৩

Proxy Pattern



  • Proxy pattern Ab¨  object Gi substitute/placeholder wnmv‡e KvR K‡i|
  • Bnv D³ object G access control Ges operation related extra logic add Kivi Rb¨ proxy enable K‡i|

Intent
  • ‡h‡nZz Proxy pattern Ab¨ object mg~‡ni properties G access control K‡i †m‡nZz accessing G expensive resources cÖ‡qvRb co‡j †mmKj scenario mg~‡n Proxy pattern LyeB useful. D`vnib ¯^iƒc-
    • Resource-intensive object mg~‡ni Rb¨ virtual proxy placeholder wnmv‡e KvR K‡i|
    • hLb object ev  object Gi methods Gi cÖ‡qvRb c‡o ZLbB call Kiv nq|
    • Remote proxy GKwU object(hvnv different address space G Ae¯’vb K‡i ) Gi Rb¨ local representative provide K‡i|
  • Visual Studio Gi gva¨‡g service G reference add Ki‡j proxy create nq|


UML Diagram



  • Dc‡iv³ UML Proxy pattern represent K‡i Ges mKj collaborating roles define K‡i|
    • The Client depends on the abstract Customer.
    • Client abstract Customer Gi Dci depend K‡i|
    • Both the RealCustomer and the ProxyCustomer implement the same interface, so the client is unaware of which is using.
    • RealCustomer I ProxyCustomer Df‡qB same interface ‡K implement K‡i myZivs †KvbwU cÖK…Zc‡ÿ use n‡”Q †m m¤ú‡K© client unaware _v‡K|
    • The ProxyCustomer has a reference to the RealCustomer and controls the access to the RealCustomer properties.
    • ProxyCustomer Gi Kv‡Q RealCustomer Gi reference _v‡K Ges RealCustomer properties G access Kivi controls _v‡K|
    • The ProxyCustomer can perform extra logic before calling on the RealCustomer properties.
    • RealCustomer properties calling Gi c~‡e© ProxyCustomer extra logic G perform Ki‡Z cv‡i|
    • Customer GKwU  interface define Ki‡e hvnvi against G Client program Ki‡e Ges the RealCustomer I ProxyCustomer interface ‡K  implement Ki‡e|
    • RealCustomer  Customer interface Gi Rb¨ default behavior define Ki‡e|


Real life example:
     ·         Proxy pattern GKwU object G access cÖ`v‡bi Rb¨ surrogate or place holder provide K‡i 
     ·         ‡Kvb account G deposited fund Gi proxy n‡jv check/ bank draft 
     ·         hw` †Kvb account holder ‡Kv_vI payment Gi †ÿ‡Î cash Gi e`‡j check payment K‡i Z‡e D³ check   cash Gi proxy wnmv‡e KvR Ki‡e| 
    ·         G‡ÿ‡Î g~jZ check Gi gva¨‡g issuer's account G access cÖ`vb Kiv n‡q‡Q|




Steps for Implementation:

Step 1: Create an interface.
Image.cs
public interface IImage
{
    void display();
}

Step 2: Create concrete classes implementing the same interface.
RealImage.cs
public class RealImage : IImage
{
    private String fileName;

    public RealImage(String fileName)
    {
        this.fileName = fileName;
        loadFromDisk(fileName);
    }


    public void display()
    {
        Console.WriteLine("Displaying " + fileName);
    }

    private void loadFromDisk(String fileName)
    {
        Console.WriteLine("Loading " + fileName);
    }

}
ProxyImage.cs
public class ProxyImage : IImage
{
    private RealImage realImage;
    private String fileName;

    public ProxyImage(String fileName)
    {
        this.fileName = fileName;
    }

    public void display()
    {
        if (realImage == null)
        {
            realImage = new RealImage(fileName);
        }
        realImage.display();
    }
}


Step 3: Use the ProxyImage to get object of RealImage class when required.
ProxyPatternDemo.java
public class ProxyPatternDemo
{
       public static void main(String[] args) {
      IImage image = new ProxyImage("test_10mb.jpg");
       
      //image will be loaded from disk
      image.display();
      Console.WriteLine("");
      //image will not be loaded from disk
      image.display();    
   }

}

Step 4: Verify the output.
Loading test_10mb.jpg
Displaying test_10mb.jpg

Displaying test_10mb.jpg

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন