您的位置:首页 > 编程语言 > Java开发

java自学日记10

2015-09-22 10:36 344 查看



------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------


代理模式与Java中的动态代理

代理模式

在任何时刻,只要你想要将额外的操作从“实际”对象中分离到不同的地方,特别是当你希望能够很容易地做出修改,从没有使用额外操作转为使用这些操作,或者反过来时,代理就显得很有用(设计模式的关键是封装修改)。例如,如果你希望跟踪对某个类中方法的调用,或者希望度量这些调用的开销,那么你应该怎样做呢?这些代码肯定是你不希望将其合并到应用中的代码,因此代理使得你可以很容易地添加或移除它们。

interface Interface {
void doSomething();
void somethingElse(String arg);
}

class RealObject implements Interface {

@Override
public void doSomething() {
System.out.println("doSomething.");
}

@Override
public void somethingElse(String arg) {
System.out.println("somethingElse " + arg);
}
}

class SimpleProxy implements Interface {

private Interface proxy;

public SimpleProxy(Interface proxy) {
this.proxy = proxy;
}

@Override
public void doSomething() {
System.out.println("SimpleProxy doSomething.");
proxy.doSomething();
}

@Override
public void somethingElse(String arg) {
System.out.println("SimpleProxy somethingElse " + arg);
proxy.somethingElse(arg);
}
}

public class SimpleProxyDemo {

public static void consumer(Interface iface) {
iface.doSomething();
iface.somethingElse("bonobo");
}

public static void main(String[] args) {
consumer(new RealObject());
consumer(new SimpleProxy(new RealObject()));
}

}


输出:

doSomething.

somethingElse bonobo

SimpleProxy doSomething.

doSomething.

SimpleProxy somethingElse bonobo

somethingElse bonobo

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: