Example of Java implementation of proxy mode

Source: Internet
Author: User
Description: This is "Dahua design pattern" a Book of Learning abstracts and online related information Digest, the original book code examples in C # write, the following Java rewrite.

1. Agent mode: Provides a proxy for other objects to control access to this object. In some cases, an object may not fit or directly refer to another object, and the proxy object can act as a mediator between the client and the target object.

2, the agent mode consists of 4 kinds of roles:
(1) Abstract role: A business method that declares a true role implementation through an interface or abstract class.
(2) Agent role: To achieve abstract role, is the real role of the agent, through the real role of the business logic method to implement the abstract method, and can attach their own operations.
(3) Real role: Implementing abstract roles, defining the business logic to be implemented by the real role for proxy role invocation.

3, the proxy model UML class diagram


4, the application of Agent mode
(1) Remote proxy: Provides local representation for an object in a different address space.
(2) Virtual agent: Create expensive objects as needed. It is used to store real objects that can take a long time to instantiate.
(3) Security agent: Control the Real object access permissions.
(4) Intelligent guidance: When the real object is invoked, the agent handles other things.


5, Code implementation

Package Demo6;
/**
 * Abstract role: A common interface that defines the real role and the proxy role/public
abstract class Subject {public
	abstract void request ();
}


package Demo6;
/**
 * Real role
/public class Realsubject extends Subject {
	@Override public
	Void request () {
		SYSTEM.OUT.PRINTLN ("real Request");
	}


Package Demo6;
/**
 * Agent role
/public class Proxy extends Subject {
	realsubject realsubject;
	@Override public
	void request () {
		if (Realsubject = null) {
			realsubject = new Realsubject ();
		}
		 Realsubject.request ();
	}


Package Demo6;
/**
 * Client code
 *
/public class Demo6 {public
	static void Main (string[] args) {
		proxy proxy = new Pr Oxy ();
		Proxy.request ();
	}

6, an example: "Agent" to help "seekers" to send things to the "pursuer"

Package Demo7;
	/** * Agent interface */interface Givegift {void Givedolls ();
	void Giveflowers ();
void Givechocolate ();
} package Demo7;	
	/** * Seekers/public class Pursuit implements Givegift {schoolgirl mm;
	Public Pursuit (schoolgirl mm) {this.mm = mm;
	@Override public void Givedolls () {System.out.println (Mm.getname () + ", send you A Doll");		
	@Override public void Giveflowers () {System.out.println (Mm.getname () + ", send you Flowers");		
	@Override public void Givechocolate () {System.out.println (Mm.getname () + ", send you Chocolate");
}} package Demo7;	
	/** * Agent/public class Proxy implements Givegift {Pursuit GG;
	Public Proxy (schoolgirl mm) {this.gg = new Pursuit (mm);
	@Override public void Givedolls () {gg.givedolls ();
	@Override public void Giveflowers () {gg.giveflowers ();
	@Override public void Givechocolate () {gg.givechocolate ();
}} package Demo7;
	/** * is being pursued by/public class schoolgirl {private String name; Public String GetName () {return NamE
	public void SetName (String name) {this.name = name;
}} package Demo7;
		/** * Client */public class Demo7 {public static void main (string[] args) {Schoolgirl Jiaojiao = new schoolgirl ();
		
		Jiaojiao.setname ("Lee charming Jiao");
		Proxy proxy = new Proxy (Jiaojiao);
		Proxy.givedolls ();
		Proxy.giveflowers ();
	Proxy.givechocolate ();
 }
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.