define

Provide a proxy for other objects to control access to this object.

nature

Control object access.

His role

  • This is the Subject of my study.

    Target interface, an interface that defines the proxy and the specific target object, so that the proxy object can be used wherever the specific target object is used.

  • Proxy

    A proxy object typically implements the same interface as a specific target object, so that a proxy can be used instead of a specific target object, a reference to a specific target object can be saved, and the specific target object can be invoked as needed.

  • RealSubject (actual subject)

    A specific target object that actually implements the functionality required by the target interface.

  • Client (requester)
  • Roles that use proxy mode.

The sample code

Public void submit() {public void submit(); Public void burden(); Public void defend(); Public void Finish (); /** * public implements myawsuiit {@override public void submit() {/** * public implements myawsuiit { System.out.println(" The boss is in arrears." ); } @override public void burden() {system.out.println (" this is the contract and the salary of the past year." ); } @override public void defend() {system.out.println () {Override public void defend() {system.out.println (); ); } @override public void finish() {system.out.println (" successful "); ); }} /** * public implements ILawsuiit{private ILawsuiit lawsuiit; public Laywer(ILawsuiit lawsuiit) { this.lawsuiit = lawsuiit; } @Override public void submit() { lawsuiit.submit(); } @Override public void burden() { lawsuiit.burden(); } @Override public void defend() { lawsuiit.defend(); } @Override public void finish() { lawsuiit.finish(); }} public class Client {public static void main(String[] args){public static void main(String[] args){ ILawsuiit lawer = new Laywer(xiaoming); // submit(); // The lawyer takes the evidence lawer.burden(); // The lawyer took xiaoming's place lawer.defend(); // The lawyer finishes the lawsuit lawer.finish(); }}Copy the code

The results

The boss is behind in his salary. Here is the contract and the salary statement for the past year. There's no need to say more. The lawsuit was successful.Copy the code

function

The proxy mode is used by creating a proxy object and using it to represent the real object. Once the client obtains the proxy object, it has no effect on the client and is used as if it had obtained the real object.

When the client operates on the proxy object, the actual function is ultimately performed by the real object, but through the proxy, that is, the client operates on the proxy, and the proxy operates on the real object.