Sec understand design patterns - proxy mode (proxy)

There are two kinds of static and dynamic proxy agent. Let me talk about the static agent, a lot of theoretical things I do not speak, even if I say, you can not read. What is the real role, abstract role, the role of the agent entrusted roles. . . Mess, I do not understand. Time before the agency model school, go online to brief look, a lot of information, open the link look, basically you have analyzed what role, a lot of theory, it seems very hard, I do not believe you can go to look, I do not understand what they say. They do not come empty, to speak directly with a life examples. (Note: I am not here to deny the theory of knowledge, theoretical knowledge I just think sometimes obscure, like the prick of people to one side, you've come to learn, not to prick)
to a certain age, we're getting married marriage is a very troublesome thing, (including those who have been to marry the parents). Wealthy families may be looking for master of ceremonies presided over the wedding, looked lively, Western style ~ Well, now the company's business to the wedding, we just need to give money, it will help us arrange wedding set wedding process. The whole process is probably this: the family to marry -> men and women of both families agreed to marry ecliptic today -> find wedding companies a fly -> marriage ceremony at the appointed time -> Wedding finish
wedding plan how to arrange the wedding program, after the completion of the wedding wedding company will do, we knew nothing. . . Do not worry, not black intermediary, we just give money to people, people will do things to us. So, here's the equivalent of wedding agent role, the agent role now understand what is right.

Look at the code to achieve:

 

//代理接口
public interface ProxyInterface {
//需要代理的是结婚这件事,如果还有其他事情需要代理,比如吃饭睡觉上厕所,也可以写
void marry();
//代理吃饭(自己的饭,让别人吃去吧)
//void eat();
//代理拉屎,自己的屎,让别人拉去吧
//void shit();
}

 

Civilized society, agents eat, the proxy shit what I do not write, have hurt public decency ~~~ can understand just fine

Well, we look wedding company code:

 

public class WeddingCompany implements ProxyInterface {

private ProxyInterface proxyInterface;

public WeddingCompany(ProxyInterface proxyInterface) {
 this.proxyInterface = proxyInterface;
}

@Override
public void marry() {
 System.out.println("我们是婚庆公司的");
 System.out.println("我们在做结婚前的准备工作");
 System.out.println("节目彩排...");
 System.out.println("礼物购买...");
 System.out.println("工作人员分工...");
 System.out.println("可以开始结婚了");
 proxyInterface.marry();
 System.out.println("结婚完毕,我们需要做后续处理,你们可以回家了,其余的事情我们公司来做");
}

}

 

See no, wedding companies need to do a lot of things, we look at marriage family code:

 

public class NormalHome implements ProxyInterface{

@Override
public void marry() {
 System.out.println("我们结婚啦~");
}

}

 

This is already evident, married family only married, but the wedding company to do everything, all the ins and outs of things to do wedding, wedding companies now hear a lot of money, this is the reason, do live and more, can not making money?

Take a look at the test class code:

 

public class Test {
public static void main(String[] args) {
 ProxyInterface proxyInterface = new WeddingCompany(new NormalHome());
 proxyInterface.marry();
}
}



Results are as follows:

 

Guess you like

Origin www.cnblogs.com/wuwuyong/p/12470270.html