代理类和被代理类的解释及案例
package com.eduask.dl;
首先定義一個(gè)接口
interface ClothFactory {
void productCloth();
}
創(chuàng)建被代理類
class NikeClothFactory implements ClothFactory{
@Override
? ? ?public void productCloth() {
? ? ? ? ? ? ?System.out.println("NIKE工廠生產(chǎn)了一批衣服");
? ? ? ? ?}
}
創(chuàng)建代理類
? ? ? ? ? ? class ProxyFactory implements ClothFactory{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ClothFactory cf;
? ? ? ? ? ? ? ? ? ? ? ?將被代理類放入代理類
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public ProxyFactory(ClothFactory cf){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?this.cf = cf;
? ? ? ? ? ? ? ?}
@Override
? ? ? ? ?public void productCloth() {
? ? ? ? ? ? ? ? ? System.out.println("代理類開始執(zhí)行,代理費(fèi)$1000");
? ? ? ? ? ? ? ? ? ? ? ? cf.productCloth();
? ? ? ? ? ?}
}
public class TestClothProduct {
? ? ? ? ? ? ? ? ?public static void main(String[] args) {
? ? ? ? ? ? ? ? ? ? ? ? ?NikeClothFactory nike = new NikeClothFactory();
? ? ? ? ? ? ? ? ? ? ? ? ? ProxyFactory proxy = new ProxyFactory(nike);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? proxy.productCloth();
}
}
轉(zhuǎn)載于:https://www.cnblogs.com/SKTT1/p/7954024.html
總結(jié)
以上是生活随笔為你收集整理的代理类和被代理类的解释及案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: centos7下端口映射
- 下一篇: Oracle 11g忘记SYS和SYST