面朝大海,春暖花開

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            12 Posts :: 1 Stories :: 3 Comments :: 0 Trackbacks

          近來很忙,一直沒寫 blog 了,今天晚上有點時間,聽了節(jié) MSDN 上面的一節(jié) webCast ,講的就是 Proxy 模式,本就就是這節(jié)課的筆記。

          學習設計模式也有段時間了,很早也見過 Proxy 模式的,記得最開始接觸 Proxy 模式還是在著名的 JIVE 論壇( JIVE 中用 Proxy 來做權限控制)。今天算是一遍復習吧。

          對復雜的軟件系統(tǒng),人們常常用的一種處理手法是在系統(tǒng)增加一層間接層,得到對系統(tǒng)靈活的、滿足特殊要求的解決方案。

          使用 Proxy 模式的動機:在 OO 的開發(fā)過程中,某些對象的開銷很大(如: new 的開銷大、某些對象因為安全的原因不能被客戶直接的調(diào)用等),如果直接操作這些對象會破壞系統(tǒng)結構。那么,我們就用代理對象來控制對象的訪問。

          例子,?? 一個常見的 HR 系統(tǒng):

          using ?System;

          class ?Employee
          {
          ????
          public ? double ?GetSalary()
          ????
          {
          ????????..
          ????}

          ????
          public ? void ?Report()
          ????
          {
          ????????.
          ????}

          ????
          public ? void ?ApplyVacation()
          ????
          {
          ????????
          ????}

          }


          class ?HrSys
          {
          ????
          public ? void ?ProcessEmployee
          ????????(Employee?employee
          /* 該對象和HR系統(tǒng)在同一個地址空間中 */ )
          ????
          {
          ????????employee.Report();
          ????????..
          ????????employee.ApplyVacation();
          ????}

          }

          現(xiàn)在要求把 Employee 做成 webService HR 系統(tǒng)通過 webService 來調(diào)用 Employee 對象,代碼修改如下:

          using ?System;

          interface ?IEmployee
          {
          ????
          public ? double ?GetSalary();
          ????
          public ? void ?Report();
          ????
          public ? void ?ApplyVacation();
          }

          // 運行在internet上面的某臺機器
          class ?Employee?:?IEmployee
          {
          ????
          public ? double ?GetSalary()
          ????
          {
          ????????..
          ????}

          ????
          public ? void ?Report()
          ????
          {
          ????????.
          ????}

          ????
          public ? void ?ApplyVacation()
          ????
          {
          ????????
          ????}

          }

          // 運行在本地的程序中
          class ?EmployeeProxy?:?IEmployee
          {
          ????
          public ? double ?GetSalary()
          ????
          {
          ????????
          // 對對象創(chuàng)建/訪問的SOAP封裝
          ????
          ?????
          // 發(fā)送SOAP數(shù)據(jù)
          ????
          ?????
          // 如果有返回值,就對SOAP分析,得到C#數(shù)據(jù)
          ????}

          ????
          ????
          public ? void ?Report()
          ????
          {
          ????????
          // 對對象創(chuàng)建/訪問的SOAP封裝
          ????
          ?????
          // 發(fā)送SOAP數(shù)據(jù)
          ????
          ?????
          // 如果有返回值,就對SOAP分析,得到C#數(shù)據(jù)
          ????}

          ????
          ????
          public ? void ?ApplyVacation()
          ????
          {
          ????????
          // 對對象創(chuàng)建/訪問的SOAP封裝
          ????
          ?????
          // 發(fā)送SOAP數(shù)據(jù)
          ????
          ?????
          // 如果有返回值,就對SOAP分析,得到C#數(shù)據(jù)
          ????}

          }

          class ?HrSys
          {
          ????
          public ? void ?ProcessEmployee(IEmployee?employeeProxy)
          ????
          {
          ????????employeeProxy.Report();
          ????????..
          ????????employeeProxy.ApplyVacation();
          ????}

          }

          Proxy 使用的要點:

          1 、“增加一層間接層”,是軟件系統(tǒng)中常用的手段之一。

          2 、具體的實現(xiàn)中, Proxy 有很大差別,有的是簡單的“ copy-on-write ”,有的是對組件模塊的抽象代理。在 JAVA 中常見的 SSH 架構模式中( struts+spring+hibernate )中,我們可以把 spring 所在的服務層看成對 hiberate 的代理。

          具體的實現(xiàn)可以參考 .NET 中的 WebService 的實現(xiàn)。

          Copy-on-write 技術

          class ?App
          {
          ????
          // 系統(tǒng)在內(nèi)存中其實是指向同一塊內(nèi)存
          ???? string ?s1? = ? " Hello " ;? // 不可在修改
          ???? string ?s2? = ? " Hello " ;? // 不可在修改

          ????
          // s1.ToUpper();? // s1不會產(chǎn)生任何改變

          ????stringBulider?sb1?
          = ? new ?stringBulider( " Hello " );
          ????stringBulider?sb2?
          = ? new ?stringBulider( " Hello " );
          ????
          ????sb1.replace(
          " H " , " L " );? // 可以修改成功
          }

          sb1.replace("H","L"); 系統(tǒng)做的動作是, sb1 的代理對象先拷貝 ”Hello” 字符串,然后用“ L ”替換“ H ”, sb1 的代理對象重新指向新的對象。

          posted on 2006-04-27 20:33 面朝大海 閱讀(300) 評論(0)  編輯  收藏 所屬分類: 軟件工程

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 巴里| 乌兰县| 兰州市| 宜兴市| 沅陵县| 宝山区| 师宗县| 孟津县| 漾濞| 定日县| 大田县| 富平县| 盖州市| 湛江市| 临猗县| 郎溪县| 北海市| 桂东县| 宜君县| 柳州市| 宣汉县| 宜宾县| 东明县| 仪征市| 大港区| 榕江县| 鲁山县| 南开区| 沙坪坝区| 祁门县| 凯里市| 嘉荫县| 双流县| 庄河市| 望谟县| 本溪| 宜阳县| 富民县| 白河县| 交口县| 四平市|