The proxy mode of the java interface


The content of the article is selected from Shang Silicon Valley, jdk8, eclipse environment

Agent mode introduction

One of the advantages of the proxy model is security. Take circumventing the wall as an example. The proxy server used when circumventing the wall is to use an overseas server to access external network data instead of us, and does not expose our real IP. What is exposed is The IP of the proxy server.

In the Java implementation of the proxy model, if B is to proxy A, A is called the proxy class, and B is the proxy class. What we want to protect is the proxy class.

Is typically created in a proxy class B interface properties and a to interface constructor parameter , Polymorphism interface address assigned to the proxy class constructor when creating the proxy class.

Example one

The demo code is as follows:

package com.atguigu.java1;

public class NetWorkTest {
    
    
	public static void main(String[] args) {
    
    
		Server server = new Server();
		ProxyServer proxyserver = new ProxyServer(server);
		proxyserver.browse();
	}
}

interface NetWork{
    
    
	void browse();
}

class Server implements NetWork{
    
    

	@Override
	public void browse() {
    
    
		System.out.println("真实服务器来访问");
		
	}
	
}

class ProxyServer implements NetWork{
    
    

	private NetWork work;
	
	public ProxyServer(NetWork work){
    
    
		this.work = work;
	}
	
	public void check(){
    
    
		System.out.println("检查服务器");
	}
	
	@Override
	public void browse() {
    
    
		check();
		work.browse();
	}
	
}

The result of the operation is

Check the server
Real server to visit

It can be seen that using a proxy server to access data, in fact, the browser that accesses the data is still a real browser, but only the proxy server is exposed to the outside.

The core of the proxy server is to create an interface constructor. When assigning a value to the proxy class, assign the address of the object of the proxy class to the constructor, and then the proxy server can call the method of the proxy server.

In the rewriting method of the proxy server, not only the method of the proxy server itself can be called, but also the implementation method of the proxy server can be called. Both proxy and non-proxy servers have rewritten the browse method of the interface. In the browse method of the non-proxy server implementation (rewrite), both the check method specific to the proxy server itself and the browse method of the proxy server are called. The main manifestation here is the polymorphism of the interface.

Example two

Shang Silicon Valley also gives another example

public class StaticProxyTest {
    
    

	public static void main(String[] args) {
    
    
		Proxy s = new Proxy(new RealStar());
		s.confer();
		s.signContract();
		s.bookTicket();
		s.sing();
		s.collectMoney();
	}
}

interface Star {
    
    
	void confer();// 面谈

	void signContract();// 签合同

	void bookTicket();// 订票

	void sing();// 唱歌

	void collectMoney();// 收钱
}
//被代理类
class RealStar implements Star {
    
    

	public void confer() {
    
    
	}

	public void signContract() {
    
    
	}

	public void bookTicket() {
    
    
	}

	public void sing() {
    
    
		System.out.println("明星:歌唱~~~");
	}

	public void collectMoney() {
    
    
	}
}

//代理类
class Proxy implements Star {
    
    
	private Star real;

	public Proxy(Star real) {
    
    
		this.real = real;
	}

	public void confer() {
    
    
		System.out.println("经纪人面谈");
	}

	public void signContract() {
    
    
		System.out.println("经纪人签合同");
	}

	public void bookTicket() {
    
    
		System.out.println("经纪人订票");
	}

	public void sing() {
    
    
		real.sing();
	}

	public void collectMoney() {
    
    
		System.out.println("经纪人收钱");
	}
}

The result of the operation is

Interview with the
agent The agent signs the contract The
agent orders the tickets
Star: Singing~~~The
agent collects the money

For the celebrity and agent example, create a celebrity interface, declare some common methods of the real celebrity and the agent, and then create two implementation classes of the real celebrity and the agent, and implement the interface methods in the class. The agent acts as an agent, and the real star acts as the agent. The agent assists the real star in all the methods except singing. These methods are unique to the agent, but the stars have to sing by themselves, but through the agent. Acting on behalf of.

Guess you like

Origin blog.csdn.net/Meloneating/article/details/114249981