接上一篇小作文,上次讲了一个贱人中层领导,还有一个牛马的故事。
这次的故事中,将登场一位超级无敌贱人,暂且叫他超级贱人中层领导。
来请看表演。
下面代表打工人必须都得会啥,必须会这些武功,才能进厂打工。
进厂必须会点啥
interface SomeMethods {
void boring1();
void boring2();
void interesting(String arg);
void boring3();
}
↓↓↓
然后隆重介绍一下打工人
Implementation类(打工人类)
class Implementation implements SomeMethods {
@Override
public void boring1() {
System.out.println("Implementation boring1");
}
@Override
public void boring2() {
System.out.println("Implementation boring2");
}
@Override
public void interesting(String arg) {
System.out.println("Implementation interesting : " + arg);
}
@Override
public void boring3() {
System.out.println("Implementation boring3");
}
}
打工人有了,配套上线超级贱人领导。
超级无敌贱人领导类
class MethodSelector implements InvocationHandler {
private Object proxied;
public MethodSelector(Object proxied) {
this.proxied = proxied;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("interesting"))
System.out.println("Proxy detected the interesting method");
return method.invoke(proxied, args);
}
}
main
public class SelectingMethods {
public static void main(String[] args) {
SomeMethods proxy = (SomeMethods) Proxy.newProxyInstance(
SomeMethods.class.getClassLoader(),
new Class[] {SomeMethods.class},
new MethodSelector(new Implementation())
);
proxy.boring1();
proxy.boring2();
proxy.interesting("bonobo");
proxy.boring3();
}
}
输出
Implementation boring1
Implementation boring2
Proxy detected the interesting method
Implementation interesting : bonobo
Implementation boring3
注意看输出的第三行与第四行
Proxy detected the interesting method 这是超级贱人加上的,因为他只对这条感兴趣
Implementation interesting : bonobo
这里就是最贱的地方了,超级无敌贱人中层领导比上一片的普通贱人领导,更贱。
因为,超级贱人只对有工作成绩的工作向上级汇报,并且加上自己的功劳。嗯
作案现场还原
class MethodSelector implements InvocationHandler {
private Object proxied;
public MethodSelector(Object proxied) {
this.proxied = proxied;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//--------------------------------------------------------------------------
if (method.getName().equals("interesting"))
System.out.println("Proxy detected the interesting method");
//--------------------------------------------------------------------------
return method.invoke(proxied, args);
}
}
if (method.getName().equals("interesting"))
System.out.println("Proxy detected the interesting method");
翻译一下就是,如果打工人做了这个方法名为interesting的事,那么就,你懂的。
天选打工人也是没有办法,因为他是被领导管理的,持有的。都是领导通知他做什么。所以对领导来说,全程监控打工人做了什么,太容易了。
好了,打工人你学废了吗。快去干活吧。