小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

外觀模式(Facade Pattern)

 漂在北方的狼 2007-03-28

一、 門(mén)面(Facade)模式

外部與一個(gè)子系統(tǒng)的通信必須通過(guò)一個(gè)統(tǒng)一的門(mén)面(Facade)對(duì)象進(jìn)行,這就是門(mén)面模式。

醫(yī)院的例子

用一個(gè)例子進(jìn)行說(shuō)明,如果把醫(yī)院作為一個(gè)子系統(tǒng),按照部門(mén)職能,這個(gè)系統(tǒng)可以劃分為掛號(hào)、門(mén)診、劃價(jià)、化驗(yàn)、收費(fèi)、取藥等??床〉牟∪艘c這些部門(mén)打交道,就如同一個(gè)子系統(tǒng)的客戶端與一個(gè)子系統(tǒng)的各個(gè)類打交道一樣,不是一件容易的事情。

首先病人必須先掛號(hào),然后門(mén)診。如果醫(yī)生要求化驗(yàn),病人必須首先劃價(jià),然后繳款,才能到化驗(yàn)部門(mén)做化驗(yàn)?;?yàn)后,再回到門(mén)診室。

解決這種不便的方法便是引進(jìn)門(mén)面模式。可以設(shè)置一個(gè)接待員的位置,由接待員負(fù)責(zé)代為掛號(hào)、劃價(jià)、繳費(fèi)、取藥等。這個(gè)接待員就是門(mén)面模式的體現(xiàn),病人只接觸接待員,由接待員負(fù)責(zé)與醫(yī)院的各個(gè)部門(mén)打交道。

什么是門(mén)面模式

門(mén)面模式要求一個(gè)子系統(tǒng)的外部與其內(nèi)部的通信必須通過(guò)一個(gè)統(tǒng)一的門(mén)面(Facade)對(duì)象進(jìn)行。門(mén)面模式提供一個(gè)高層次的接口,使得子系統(tǒng)更易于使用。

就如同醫(yī)院的接待員一樣,門(mén)面模式的門(mén)面類將客戶端與子系統(tǒng)的內(nèi)部復(fù)雜性分隔開(kāi),使得客戶端只需要與門(mén)面對(duì)象打交道,而不需要與子系統(tǒng)內(nèi)部的很多對(duì)象打交道。


二、 門(mén)面模式的結(jié)構(gòu)

門(mén)面模式是對(duì)象的結(jié)構(gòu)模式。門(mén)面模式?jīng)]有一個(gè)一般化的類圖描述,下圖演示了一個(gè)門(mén)面模式的示意性對(duì)象圖:

 

在這個(gè)對(duì)象圖中,出現(xiàn)了兩個(gè)角色:

門(mén)面(Facade)角色:客戶端可以調(diào)用這個(gè)角色的方法。此角色知曉相關(guān)的(一個(gè)或者多個(gè))子系統(tǒng)的功能和責(zé)任。在正常情況下,本角色會(huì)將所有從客戶端發(fā)來(lái)的請(qǐng)求委派到相應(yīng)的子系統(tǒng)去。

子系統(tǒng)(subsystem)角色:可以同時(shí)有一個(gè)或者多個(gè)子系統(tǒng)。每一個(gè)子系統(tǒng)都不是一個(gè)單獨(dú)的類,而是一個(gè)類的集合。每一個(gè)子系統(tǒng)都可以被客戶端直接調(diào)用,或者被門(mén)面角色調(diào)用。子系統(tǒng)并不知道門(mén)面的存在,對(duì)于子系統(tǒng)而言,門(mén)面僅僅是另外一個(gè)客戶端而已。


三、 門(mén)面模式的實(shí)現(xiàn)

一個(gè)系統(tǒng)可以有幾個(gè)門(mén)面類

【GOF】的書(shū)中指出:在門(mén)面模式中,通常只需要一個(gè)門(mén)面類,并且此門(mén)面類只有一個(gè)實(shí)例,換言之它是一個(gè)單例類。當(dāng)然這并不意味著在整個(gè)系統(tǒng)里只能 有一個(gè)門(mén)面類,而僅僅是說(shuō)對(duì)每一個(gè)子系統(tǒng)只有一個(gè)門(mén)面類?;蛘哒f(shuō),如果一個(gè)系統(tǒng)有好幾個(gè)子系統(tǒng)的話,每一個(gè)子系統(tǒng)有一個(gè)門(mén)面類,整個(gè)系統(tǒng)可以有數(shù)個(gè)門(mén)面 類。

為子系統(tǒng)增加新行為

初學(xué)者往往以為通過(guò)繼承一個(gè)門(mén)面類便可在子系統(tǒng)中加入新的行為,這是錯(cuò)誤的。門(mén)面模式的用意是為子系統(tǒng)提供一個(gè)集中化和簡(jiǎn)化的溝通管道,而不能向子系統(tǒng)加入新的行為。


四、 在什么情況下使用門(mén)面模式

  • 為一個(gè)復(fù)雜子系統(tǒng)提供一個(gè)簡(jiǎn)單接口
  • 提高子系統(tǒng)的獨(dú)立性
  • 在層次化結(jié)構(gòu)中,可以使用Facade模式定義系統(tǒng)中每一層的入口。


五、 一個(gè)例子

我們考察一個(gè)保安系統(tǒng)的例子,以說(shuō)明門(mén)面模式的功效。一個(gè)保安系統(tǒng)由兩個(gè)錄像機(jī)、三個(gè)電燈、一個(gè)遙感器和一個(gè)警報(bào)器組成。保安系統(tǒng)的操作人員需要經(jīng)常將這些儀器啟動(dòng)和關(guān)閉。

不使用門(mén)面模式的設(shè)計(jì)

首先,在不使用門(mén)面模式的情況下,操作這個(gè)保安系統(tǒng)的操作員必須直接操作所有的這些部件。下圖所示就是在不使用門(mén)面模式的情況下系統(tǒng)的設(shè)計(jì)圖。

 


可以看出,Client對(duì)象需要引用到所有的錄像機(jī)(Camera)、電燈(Light)、感應(yīng)器(Sensor)和警報(bào)器(Alarm)對(duì)象。代碼如下:

 

using System;

public class Camera
{
  
public void TurnOn()
  
{
    Console.WriteLine(
"Turning on the camera.");
  }


  
public void TurnOff()
  
{
    Console.WriteLine(
"Turning off the camera.");
  }


  
public void Rotate(int degrees)
  
{
    Console.WriteLine(
"Rotating the camera by {0} degrees.", degrees);
  }

}


public class Light
{

  
public void TurnOff()
  
{
    Console.WriteLine(
"Turning on the light.");
  }


  
public void TurnOn()
  
{
    Console.WriteLine(
"Turning off the light.");
  }


  
public void ChangeBulb()
  
{
    Console.WriteLine(
"changing the light-bulb.");
  }

}


public class Sensor
{
  
public void Activate()
  
{
    Console.WriteLine(
"Activating the sensor.");
  }


  
public void Deactivate()
  
{
    Console.WriteLine(
"Deactivating the sensor.");
  }


  
public void Trigger()
  
{
    Console.WriteLine(
"The sensor has triggered.");
  }

}


public class Alarm
{

  
public void Activate()
  
{
    Console.WriteLine(
"Activating the alarm.");
  }


  
public void Deactivate()
  
{
    Console.WriteLine(
"Deactivating the alarm.");
  }


  
public void Ring()
  
{
    Console.WriteLine(
"Ringing the alarm.");
  }


  
public void StopRing()
  
{
    Console.WriteLine(
"Stop the alarm.");
  }

}


public class Client
{
  
private static Camera camera1, camera2;
  
private static Light light1, light2, light3;
  
private static Sensor sensor;
  
private static Alarm alarm;

  
static Client()
  
{
    camera1 
= new Camera();
    camera2 
= new Camera();
    light1 
= new Light();
    light2 
= new Light();
    light3 
= new Light();
    sensor 
= new Sensor();
    alarm 
= new Alarm();
  }
  

  
public static void Main( string[] args )
  
{
    camera1.TurnOn();
    camera2.TurnOn();
    light1.TurnOn();
    light2.TurnOn();
    light3.TurnOn();
    sensor.Activate();
    alarm.Activate();
  }

}

 

 

六、 使用門(mén)面模式的設(shè)計(jì)

一個(gè)合情合理的改進(jìn)方法就是準(zhǔn)備一個(gè)系統(tǒng)的控制臺(tái),作為保安系統(tǒng)的用戶界面。如下圖所示:

 

程序代碼如下:

 

using System;

public class Camera
{
  
public void TurnOn()
  
{
    Console.WriteLine(
"Turning on the camera.");
  }


  
public void TurnOff()
  
{
    Console.WriteLine(
"Turning off the camera.");
  }


  
public void Rotate(int degrees)
  
{
    Console.WriteLine(
"Rotating the camera by {0} degrees.", degrees);
  }

}


public class Light
{

  
public void TurnOff()
  
{
    Console.WriteLine(
"Turning on the light.");
  }


  
public void TurnOn()
  
{
    Console.WriteLine(
"Turning off the light.");
  }


  
public void ChangeBulb()
  
{
    Console.WriteLine(
"changing the light-bulb.");
  }

}


public class Sensor
{
  
public void Activate()
  
{
    Console.WriteLine(
"Activating the sensor.");
  }


  
public void Deactivate()
  
{
    Console.WriteLine(
"Deactivating the sensor.");
  }


  
public void Trigger()
  
{
    Console.WriteLine(
"The sensor has triggered.");
  }

}


public class Alarm
{

  
public void Activate()
  
{
    Console.WriteLine(
"Activating the alarm.");
  }


  
public void Deactivate()
  
{
    Console.WriteLine(
"Deactivating the alarm.");
  }


  
public void Ring()
  
{
    Console.WriteLine(
"Ringing the alarm.");
  }


  
public void StopRing()
  
{
    Console.WriteLine(
"Stop the alarm.");
  }

}


public class SecurityFacade
{
  
private static Camera camera1, camera2;
  
private static Light light1, light2, light3;
  
private static Sensor sensor;
  
private static Alarm alarm;

  
static SecurityFacade()
  
{
    camera1 
= new Camera();
    camera2 
= new Camera();
    light1 
= new Light();
    light2 
= new Light();
    light3 
= new Light();
    sensor 
= new Sensor();
    alarm 
= new Alarm();
  }

  
  
public void Activate()
  
{
    camera1.TurnOn();
    camera2.TurnOn();
    light1.TurnOn();
    light2.TurnOn();
    light3.TurnOn();
    sensor.Activate();
    alarm.Activate();
  }


  
public void Deactivate()
  
{
    camera1.TurnOff();
    camera2.TurnOff();
    light1.TurnOff();
    light2.TurnOff();
    light3.TurnOff();
    sensor.Deactivate();
    alarm.Deactivate();
  }

}


public class Client
{
  
private static SecurityFacade security;

  
public static void Main( string[] args )
  
{
    security 
= new SecurityFacade();
    security.Activate();
    Console.WriteLine(
"\n--------------------\n");
    security.Deactivate();
  }

}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多