面朝大海,春暖花開

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

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

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

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

          使用 Proxy 模式的動(dòng)機(jī):在 OO 的開發(fā)過程中,某些對象的開銷很大(如: new 的開銷大、某些對象因?yàn)榘踩脑虿荒鼙豢蛻糁苯拥恼{(diào)用等),如果直接操作這些對象會(huì)破壞系統(tǒng)結(jié)構(gòu)。那么,我們就用代理對象來控制對象的訪問。

          例子,?? 一個(gè)常見的 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)在同一個(gè)地址空間中 */ )
          ????
          {
          ????????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();
          }

          // 運(yùn)行在internet上面的某臺(tái)機(jī)器
          class ?Employee?:?IEmployee
          {
          ????
          public ? double ?GetSalary()
          ????
          {
          ????????..
          ????}

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

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

          }

          // 運(yùn)行在本地的程序中
          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 使用的要點(diǎn):

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

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

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

          Copy-on-write 技術(shù)

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

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

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

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

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

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 阜新市| 安阳市| 阳原县| 泗阳县| 缙云县| 靖江市| 皮山县| 寿光市| 项城市| 宁阳县| 来凤县| 五莲县| 敦化市| 抚远县| 高阳县| 拜城县| 龙山县| 平阴县| 奈曼旗| 黄平县| 扬中市| 无锡市| 巴彦县| 丽江市| 宁德市| 竹北市| 莲花县| 陇西县| 鄂托克旗| 岑溪市| 宁安市| 淄博市| 滦南县| 连江县| 鹤峰县| 南丹县| 赤壁市| 昂仁县| 昭平县| 南丰县| 肇州县|