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

          java模式之代理模式

          Posted on 2007-01-05 20:43 chenweicai 閱讀(193) 評論(0)  編輯  收藏

          代理模式一般有三個角色:
          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();


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 仙居县| 七台河市| 大渡口区| 黄冈市| 乐亭县| 肇庆市| 海南省| 镇远县| 苍梧县| 慈利县| 柳州市| 抚松县| 阿克陶县| 兴义市| 西峡县| 包头市| 阿瓦提县| 花垣县| 阿勒泰市| 永城市| 雷波县| 巴林右旗| 昭觉县| 集贤县| 咸丰县| 靖远县| 普兰店市| 荣昌县| 理塘县| 南华县| 连山| 平山县| 循化| 肥西县| 阳新县| 唐河县| 卢湾区| 拜城县| 赫章县| 明水县| 石楼县|