Simple understanding of proxy design pattern (Proxy)

        The proxy design pattern is one of the most commonly used design patterns in Java development. The so-called proxy design pattern refers to a proxy subject to operate the real subject, the real subject performs specific business operations, and the proxy subject is responsible for the processing of other related services. For example, the proxy that is often used in life is used to surf the Internet. The customer connects to the network through the network proxy, and the proxy server completes the operations related to the Internet, such as user rights and access restrictions.




interface Network {
	public void browse();
}
class Real implements Network {
	public void browse() {
		System.out.println("Browse information on the Internet.");
	}	
}
class Proxy implements Network { //Proxy Internet access
	private Network network;
	public Proxy(Network network){ //Set the real operation of the proxy
		this.network = network;
	}
	public void check() { //Operations related to specific Internet access
		System.out.println("Check if the user is legal");
	}
	public void browse() {
		this.check(); //Can call multiple operations related to specific business
		this.network.browse; //Call the real Internet operation
	}
}
public class TestProxy {
	public static void main(String args[]) {
		Network net = null;
		net = new Proxy(new Real()); //Instantiate the proxy and pass in the real operation of the proxy
		net.browse(); //The client only cares about the Internet browsing function, the other is done by the agent
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325366173&siteId=291194637