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

          日歷

          <2007年1月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          文章分類

          收藏夾

          搜索

          •  

          最新評論

          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();


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


          網站導航:
           
          主站蜘蛛池模板: 乃东县| 务川| 石泉县| 浦县| 弋阳县| 长乐市| 赞皇县| 红河县| 教育| 蒙自县| 长兴县| 石景山区| 改则县| 江西省| 龙门县| 黔南| 盐城市| 济源市| 雅安市| 延川县| 前郭尔| 喜德县| 普定县| 凤山县| 巫溪县| 儋州市| 全椒县| 衡水市| 石家庄市| 古浪县| 鄂州市| 黎城县| 巴南区| 河西区| 大田县| 大足县| 尚义县| 绥芬河市| 南漳县| 洛川县| 丹凤县|