Java Design Pattern Story for Proxy – A Slutty Lady

This work is translated from a foreign website which uses ancient stories to explain design patterns.

1. What is Proxy/Agent pattern?

I’m too busy to response your request, so you go to my proxy. Proxy should know what the delegator can do. That is, they have the same interface. The proxy can not do the job, but the delegator can do. The characters you do not understand can be totally ignored!

2. Proxy pattern story

Here is a funny story I translated from “Water Margin”. It may not sounds funny any more after I translate it. But any way, you get the idea of proxy design pattern.

Here is the situation:

Some bad man, for whatever reasons, always wants to sleep with some good man’s wife. Among those wives, some want to sleep with those bad men, but others do not. The bad men can not ask directly to those wives. Because they are not sure whether the one being asked would like to do bad things. It would be a very bad situation if he makes a bad judgement. So there should be an agent/proxy to do this kind of business for those bad men.

We have the following roles in this situation.

CheatingWife/SluttyWife: a interface which define what they usually do, such as seduce men and happy with men.
HouseWifeOne: she is a slutty wife at home.
Mike: who wants to sleep with other men’s wives.
Business Agent: do this kind of consulting business.

3. Proxy pattern class diagram
proxy-pattern-class-diagram

4. Java code

1. Define the cheating wife’s type
2. define a cheating wife No. 1
3. Define the bad agent
4. Let the bad men start doing bad things

interface CheatingWife {
	// think about what this kind of women can do
	public void seduceMan(); // such as eye contact with men
 
	public void happyWithMan(); // happy what? You know that.
}
 
class HouseWifeOne implements CheatingWife {
 
	public void seduceMan() {
		System.out
				.println("HouseWifeOne secude men, such as making some sexy poses ...");
	}
 
	public void happyWithMan() {
		System.out.println("HouseWifeOne is happy with man ...");
	}
}
 
class BusinessAgent implements CheatingWife {
	private CheatingWife cheatingWife;
 
	public BusinessAgent() {
 
		this.cheatingWife = new HouseWifeOne();
	}
 
	public BusinessAgent(CheatingWife cheatingWife) {
		this.cheatingWife = cheatingWife;
	}
 
	public void seduceMan() {
		this.cheatingWife.seduceMan();
	}
 
	public void happyWithMan() {
		this.cheatingWife.happyWithMan();
	}
 
}
 
// see? it looks that agent/proxy is doing
public class Mike {
 
	public static void main(String[] args) {
		BusinessAgent businessAgent = new BusinessAgent();
		businessAgent.seduceMan();
		businessAgent.happyWithMan();
	}
}

3 thoughts on “Java Design Pattern Story for Proxy – A Slutty Lady”

  1. Proxy Design Pattern in Java, lazy loading using Proxy Design Pattern

    What is Proxy Design Pattern

    1. Proxy design patten works on the principal of exposing an Java Instance through a proxy instead of actual object.

    2. Client would never know anything about actual object and through Proxy only relevant behavior of actual object will be exposed to client.

    3. Proxy Pattern can be used for applying security on actual Object. Service provider does not want actual class to be visible to any client Instead It would be shared as per Client contract agreement . Service provider may agree to share only a part of Service with it’s client and for that It may expose a different contract in the form of interface in java .

    4. This concept is very useful for lazily loading an instance . Data will be loaded only when it is actually required in an operation .

    Learn more about proxy design pattern here — Proxy Design Pattern

    http://efectivejava.blogspot.in/2013/09/proxy-design-pattern-in-java-lazy.html?utm_source=BP_recent

Leave a Comment