Basic concepts of the proxy model
Proxy mode provides a proxy for other objects to control access to this object.
In other words, if the client cannot or cannot interact with the object directly, it will interact with its proxy, which can expand the function of the original object without changing the original object. This is an open-closed design principle.
When to use proxy mode
Take two examples.
For example, Zhang San wanted to hit Li Si, but he couldn't, so he found Wang Wu and beat him up. Li Si didn't know it was Zhang San who wanted to hit him.
Another example is that I want to buy Apple 10, but Apple 10 is not sold in China, so I find a country to buy it for me.
Examples may be inappropriate, but the idea is this.
Here are four scenarios for proxy mode:
- Remote proxy: Hide the fact that an object exists in different address spaces.
- Virtual Agent: Stores real objects that take a long time to instantiate through it.
- Security proxy: Control access to real objects.
- Intelligent Guidelines: Agents handle other things when real objects are called.
How to use proxy mode
Usually, with the use of proxy mode, we pull out all the interfaces of classes that need to be proxied. An interface is a specification, and it is unlikely that we will force all programmers to comply with it without an interface.
The proxy mode is very simple, it combines the proxy classes into the proxy classes and implements all the methods of the proxy classes in the same way.
For example, buying things on behalf of others looks like buying things on behalf of others, but inside the method I actually spend money on things.
In the case of proxy shopping, first of all, we need to take out the public interface and buy apples:
/**
* @ClassName: BuyApple
* @Description: Buy apples
* @author cjd
* @date 2017 December 18 4:59:45 p.m.
*/
public interface BuyApple {
void buyApple();
}
Then we need to have a proxy class for people who buy Apple 8:
/**
* @ClassName: Person
* @Description: People who buy apples are famous and rich
* @author cjd
* @date 2017 December 18 5:00:14 p.m.
*/
public class Person implements BuyApple {
private int money;
private String name;
public Person(String name, int money) {
this.setName(name);
this.setMoney(money);
}
@Override
public void buyApple() {
System.out.println("Spent"+money+"Buy Apple 8");
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
But even if I have money, there is no place to buy apples, so we need to find an agent to help us buy apples abroad:
/**
* @ClassName: Proxy
* @Description: Buy Apple for Person
* @author cjd
* @date 2017 December 18 5:02:49 p.m.
*/
public class Proxy implements BuyApple {
private Person person;
public Proxy(Person buyer) {
this.person = buyer;
}
@Override
public void buyApple() {
System.out.println("go abroad");
person.buyApple();
System.out.println("Return to China and hand in Apple 8" + person.getName());
}
}
The proxy is simple, it encapsulates the Person to be proxy inside. The proxy to buy Apple actually implements Person to buy Apple, while the proxy to expand Person only extends abroad and returns to China.
The main function calls this way:
/**
* @ClassName: Main
* @Description: TODO(Here is a sentence describing the function of this class)
* @author cjd
* @date 2017 December 18 5:06:55 p.m.
*/
public class Main {
public static void main(String[] args)
Person cjd = new Person("Chi Jiandi", 10000);
Proxy proxy = new Proxy(cjd);
proxy.buyApple();
}
}
The result of running the agent is
go abroad Spend 10,000 on Apple 8 Return to China and give Apple 8 to Chi Jiandi
And going abroad and returning home are functions that Person does not have and Person is not modified, which implements the principle of open-close.This is also a basic application of proxy.
Postnote
Proxy mode is actually a representation of real objects, but at the same time because of the generation of proxy objects, it also increases the complexity and memory overhead. When designing, it is just as overwhelming as other modes.
If there is a reason error, thank you for pointing out!