[JAVA] Proxy Pattern

Proxy Let the Proxy class take over the tasks of a specific class Have a method to delegate tasks that cannot be processed by Proxy class to a specific class Instances of a specific class will not be created until the task is delegated to a specific class </ b> </ font>

The following is described on this page -Check the structure of Proxy Pattern ·Implementation

Design Pattarm MENU

Checking the structure of Proxy Pattern

Check with the following class structure

class Explanation
sam.class Class on behalf of Proxy
samProxy.class sam.Acting for class tasks

sam.class


class sam{
  void sm1(){}
  void sm2(){}}

samProxy.class


class samProxy{
  sam  sam;
  void sm1(){}
  void sm2(){sam.sm2();}  //Delegate the task to the sam class. Create a sam instance just before this
}

Implementation

sm1 () in interface is a task that can be processed by samProxy class sm2 () in interface is a task that the samProxy class cannot handle will do

s.interface


interface s{
  void sm1();
  void sm2();
}

sam.class


class sam implements s{
  public void sm1(){System.out.println("sam1");}
  public void sm2(){System.out.println("sam2");}
}

samProxy.class


class samProxy implements s{
  sam  sam;
  public void sm1(){System.out.println("proxy1");}
  public void sm2(){        //A method that delegates tasks that cannot be processed by samProxy to sam
         if(sam == null){   //Create a sam instance when you need it
            sam =  new sam();
            sam.sm2();}
}}

user(Main.class)


public static void main(String[] args){
  s s = new samProxy();
  s.sm2();
}}

Recommended Posts