Development

프록시 패턴이란 무엇입니까 : Java 디자인 패턴.

sonpro 2023. 3. 11. 02:10
반응형

Proxy Pattern

What is Proxy Pattern: JAVA Design Pattern

Proxy pattern is a structural design pattern that provides an interface to an object, which is used to control access to the object. The proxy object acts as a surrogate for the real object and provides a way to control access to it. In this article, we will discuss the Proxy pattern in detail, its types, and how it is implemented in Java.

Types of Proxy Pattern

There are three types of Proxy pattern:

1. Virtual Proxy

A virtual proxy is a placeholder for an expensive object. It creates an object only when it is required. For example, imagine a web page that contains a lot of images. Loading all the images at once can take a lot of time. In this case, a virtual proxy can be used to load the images only when they are required.

2. Remote Proxy

A remote proxy provides a local representation of an object that is located in a different address space. It is used to communicate with remote objects. For example, imagine a client-server application. The client sends a request to the server, and the server sends a response back to the client. In this case, a remote proxy can be used to communicate with the server.

3. Protection Proxy

A protection proxy controls access to an object. It is used to restrict access to sensitive objects. For example, imagine a bank account. The account holder can access the account, but others cannot. In this case, a protection proxy can be used to restrict access to the account.

Implementation of Proxy Pattern in Java

The Proxy pattern can be implemented in Java using the following steps:

1. Create an interface

Create an interface that defines the methods that the real object and the proxy object will implement.

public interface Image {
   void display();
}

2. Create a real object

Create a real object that implements the interface.

public class RealImage implements Image {

   private String fileName;

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

   @Override
   public void display() {
      System.out.println("Displaying " + fileName);
   }

   private void loadFromDisk(String fileName){
      System.out.println("Loading " + fileName);
   }
}

3. Create a proxy object

Create a proxy object that implements the interface. The proxy object should have a reference to the real object.

public class ProxyImage implements Image{

   private RealImage realImage;
   private String fileName;

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

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

4. Use the proxy object

Use the proxy object to access the real object.

public class ProxyPatternDemo {

   public static void main(String[] args) {
      Image image = new ProxyImage("test.jpg");

      //image will be loaded from disk
      image.display(); 
      System.out.println("");

      //image will not be loaded from disk
      image.display();  
   }
}

Conclusion

The Proxy pattern is a powerful design pattern that provides a way to control access to an object. It is used to create a surrogate for the real object and provides a way to control access to it. In this article, we discussed the Proxy pattern in detail, its types, and how it is implemented in Java. We hope this article has helped you understand the Proxy pattern better.

반응형