MTI TEK
  • Home
  • About
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All Resources
Java Design Patterns | Proxy
  1. Overview and Definition
  2. Class Diagram
  3. Implementation Example
    • The Subject Interface
    • The Real Subject
    • The Proxy
    • Testing the Proxy Pattern

  1. Overview and Definition
    The Proxy pattern is a structural design pattern that provides a placeholder or surrogate for another object to control access to it. It acts as an interface to something else, whether it's a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate.

    Key Components:
    • Subject: Defines the common interface for Real Subject and Proxy so that a Proxy can be used anywhere a Real Subject is expected.
    • Real Subject: The actual object that the proxy represents and controls access to.
    • Proxy: Maintains a reference to the Real Subject and controls access to it. May create or delete the Real Subject.
    • Client: Works with objects through the Subject interface, unaware of whether it's using a Proxy or Real Subject.

    Benefits:
    • Access Control: Can control access to the real object, providing security or permission checks.
    • Lazy Initialization: Can defer the creation of expensive objects until they are actually needed.
    • Performance Optimization: Can cache results or add additional functionality without modifying the original object.

    Types of Proxies:
    • Virtual Proxy: Controls access to expensive-to-create objects by creating them only when needed (lazy initialization).
    • Protection Proxy: Controls access to objects based on access rights or permissions.
    • Remote Proxy: Provides a local representative for an object in a different address space (e.g., network services).
    • Caching Proxy: Stores results of expensive operations and returns cached results when appropriate.
  2. Class Diagram
    Proxy Pattern Components:
    • Subject: Logger interface - defines the interface for objects that can be accessed through proxy.
    • Real Subject: FileLogger class - the actual object that performs the real work.
    • Proxy: ProxyFileLogger class - controls access to the Real Subject and adds lazy initialization.
    ┌─────────────────────────────┐
    │       <<interface>>         │
    │          Logger             │  ← Subject
    ├─────────────────────────────┤
    │ + log(): void               │
    └─────────────────────────────┘
                 △
                 │ implements
                 │
    ┌─────────────────────────────┐
    │        FileLogger           │  ← Real Subject
    ├─────────────────────────────┤
    │ + log(): void               │
    └─────────────────────────────┘
    
    
    ┌─────────────────────────────┐
    │      ProxyFileLogger        │  ← Proxy
    ├─────────────────────────────┤
    │ - logger: FileLogger        │
    │ + log(): void               │
    └─────────────────────────────┘
                   
  3. Implementation Example
    • The Subject Interface:
      The Subject interface defines the common interface for both Real Subject and Proxy. This allows the proxy to be used anywhere the real subject is expected.
      public interface Logger {
         void log();
      }
    • The Real Subject:
      The Real Subject is the actual object that does the real work. The proxy will control access to this object and may add additional functionality.
      public class FileLogger implements Logger {
         public void log() {
             System.out.println("FileLogger: Writing log to file");
         }
      }
    • The Proxy:
      The Proxy implements the same interface as the Real Subject and maintains a reference to it. It controls access to the real subject and demonstrates lazy initialization by creating the real object only when needed.
      public class ProxyFileLogger implements Logger {
         private FileLogger logger;
      
         public void log() {
             System.out.println("ProxyFileLogger: Checking if real logger is needed");
             if (logger == null) {
                 System.out.println("ProxyFileLogger: Creating FileLogger instance");
                 logger = new FileLogger();
             }
             logger.log();
         }
      }
    • Testing the Proxy Pattern:
      The client code demonstrates how the proxy controls access to the real subject. The client works with the proxy through the same interface as the real subject.
      public class ProxyPatternTest {
         public static void main(String[] args) {
             // Create the proxy
             Logger logger = new ProxyFileLogger();
             
             System.out.println("First call through proxy");
             logger.log();
             
             System.out.println("Second call (real logger already created)");
             logger.log();
         }
      }
© 2025 mtitek