How to Use Proxy Pattern in Java


In this example, we will write an example to implement Proxy Pattern in Java.

Source Code

package com.beginner.examples;

// interface 
interface Fetch {  
    public void fetch();  
}  
  
// custom
class Custom implements Fetch{  
    @Override 
    public void fetch() { 
    	System.out.println("custom fetch"); 
    }  
}  
  
// agent 
class Agent implements Fetch{  
    @Override 
    public void fetch() {
    	this.callCustom(); 
        Custom custom = new Custom();  
        custom.fetch();
    }
  
    public void callCustom() { System.out.println("call custom"); }  
}  
 
class ProxyExample {  
    public static void main(String[] args) {  
        Agent agent = new Agent();  
        agent.fetch();  
    }  
} 

Output:

Current State: #01
First saved State: #00
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments