posts - 51, comments - 17, trackbacks - 0, articles - 9
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          代理模式一般有三個角色:
          1。抽象主題角色(interface or abstract class)
          package ProxyPattern;

          /**
          ?* 抽象主題角色,定義了公共接口
          ?* @author chenweicai
          ?*
          ?*/
          public interface Merchant {

          ?public void treat();
          }

          2。真實主題角色


          package ProxyPattern;

          /**
          ?* 真實主題角色,實現了抽象主題接口
          ?*
          ?* @author?chenweicai
          ?*
          ?*/
          public class Director implements Merchant {

          ?public Director() {

          ?}

          ?public void treat() {
          ??// TODO Auto-generated method stub
          ??System.out.println("董事長請大家吃飯!");
          ?}

          }


          3。代理主題角色
          package ProxyPattern;

          import java.lang.reflect.InvocationHandler;
          import java.lang.reflect.Method;

          /**
          ?* 代理主題角色
          ?*
          ?* @author?chenweicai?
          ?*/
          public class Secretory implements InvocationHandler {

          ?/**
          ? * 定義真實主題
          ? */
          ?private Director director;
          ?
          ?public Secretory(Director director) {
          ??this.director = director;
          ?}

          ?public Object invoke(Object proxy, Method method, Object[] args)
          ???throws Throwable {
          ??// TODO Auto-generated method stub
          ??director.treat();
          ??System.out.println("由秘書來結帳哦!");
          ??return null;
          ?}

          }

          //---------------------------------------------------
          ??Director director = new Director();
          ??InvocationHandler secretory = new Secretory(director);
          ??Merchant merchant = (Merchant) Proxy.newProxyInstance(director
          ????.getClass().getClassLoader(), director.getClass()
          ????.getInterfaces(), secretory);
          ??merchant.treat();

          posted @ 2007-01-05 20:43 chenweicai 閱讀(194) | 評論 (0)編輯 收藏

          僅列出標題
          共6頁: 上一頁 1 2 3 4 5 6 
          主站蜘蛛池模板: 庆城县| 区。| 固原市| 犍为县| 朝阳区| 汝城县| 正定县| 林西县| 龙江县| 兰溪市| 武乡县| 河北省| 漾濞| 会宁县| 台北县| 林周县| 许昌市| 岳西县| 尉犁县| 乌鲁木齐市| 安庆市| 临漳县| 天峨县| 池州市| 南涧| 安远县| 民乐县| 苏尼特左旗| 雷山县| 辽宁省| 海伦市| 烟台市| 武陟县| 会同县| 益阳市| 延津县| 汶上县| 淮阳县| 浦江县| 北票市| 油尖旺区|