2011-09-25 89 views
3

對於門面設計模式在書中的書Erich Gamma的可重用面向對象軟件的元素在degign模式上,實現部分討論了使Facade類爲抽象類,因爲它減少了客戶端和子系統耦合。設計模式中的抽象類

我無法理解這一點。如何讓課堂抽象幫助減少耦合。有人請幫助我理解這一點。

而不門面類原始代碼爲抽象類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Facade_CSharp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Facade facade = new Facade(); 

      facade.ProcessA(); 
      facade.ProcessB(); 

      // Wait for user 
      Console.ReadKey(); 
     } 
    } 

    /// <summary> 
    /// The 'Subsystem ClassA' class 
    /// </summary> 
    class SubSystemOne 
    { 
    public void MethodOne() 
    { 
     Console.WriteLine(" SubSystem One"); 
    } 
    } 

    /// <summary> 
    /// The 'Subsystem ClassB' class 
    /// </summary> 
    class SubSystemTwo 
    { 
    public void MethodTwo() 
    { 
     Console.WriteLine(" SubSystem Two"); 
    } 
    } 

    /// <summary> 
    /// The 'Subsystem ClassC' class 
    /// </summary> 
    class SubSystemThree 
    { 
    public void MethodThree() 
    { 
     Console.WriteLine(" SubSystem Three"); 
    } 
    } 

    /// <summary> 
    /// The 'Subsystem ClassD' class 
    /// </summary> 
    class SubSystemFour 
    { 
    public void MethodFour() 
    { 
     Console.WriteLine(" SubSystem Four"); 
    } 
    } 

    /// <summary> 
    /// The 'Facade' class 
    /// </summary> 
    class Facade 
    { 
    private SubSystemOne _one; 
    private SubSystemTwo _two; 
    private SubSystemThree _three; 
    private SubSystemFour _four; 

    public Facade() 
    { 
     Console.WriteLine("\nRequests received from Client System and Facade is in execution... "); 

     _one = new SubSystemOne(); 
     _two = new SubSystemTwo(); 
     _three = new SubSystemThree(); 
     _four = new SubSystemFour(); 
    } 

    public void ProcessA() 
    { 
     Console.WriteLine("\nProcessA of Facade uses the following subsystems to accomplish the task:"); 
     _one.MethodOne(); 
     _two.MethodTwo(); 
     _four.MethodFour(); 
    } 

    public void ProcessB() 
    { 
     Console.WriteLine("\nProcessB of Facade uses the following subsystems to accomplish the task:"); 
     _two.MethodTwo(); 
     _three.MethodThree(); 
    } 
    } 
} 

代碼與門面類作爲一個抽象類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Facade_abstract 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      FacadeAbs facade = new FacadeAbs(); 

      facade.ProcessA(); 
      facade.ProcessB(); 

      // Wait for user 
      Console.ReadKey(); 

     } 
    } 

    class SubSystemOne 
    { 
     public void MethodOne() 
     { 
      Console.WriteLine(" SubSystem One"); 
     } 
    } 

    /// <summary> 
    /// The 'Subsystem ClassB' class 
    /// </summary> 
    class SubSystemTwo 
    { 
     public void MethodTwo() 
     { 
      Console.WriteLine(" SubSystem Two"); 
     } 
    } 

    /// <summary> 
    /// The 'Subsystem ClassC' class 
    /// </summary> 
    class SubSystemThree 
    { 
     public void MethodThree() 
     { 
      Console.WriteLine(" SubSystem Three"); 
     } 
    } 

    /// <summary> 
    /// The 'Subsystem ClassD' class 
    /// </summary> 
    class SubSystemFour 
    { 
     public void MethodFour() 
     { 
      Console.WriteLine(" SubSystem Four"); 
     } 
    } 

    /// <summary> 
    /// The 'Facade' class 
    /// </summary> 
    public abstract class Facade 
    { 
     //public abstract Facade(); 

     public abstract void ProcessA(); 

     public abstract void ProcessB(); 

    } 

    public class FacadeAbs : Facade 
    { 
     private SubSystemOne _one; 
     private SubSystemTwo _two; 
     private SubSystemThree _three; 
     private SubSystemFour _four; 

     public FacadeAbs() 
     { 
      Console.WriteLine("\nRequests received from Client System and Facade is in execution... "); 

      _one = new SubSystemOne(); 
      _two = new SubSystemTwo(); 
      _three = new SubSystemThree(); 
      _four = new SubSystemFour(); 
     } 


     public override void ProcessA() 
     { 
      Console.WriteLine("\nProcessA of Facade uses the following subsystems to accomplish the task:"); 
      _one.MethodOne(); 
      _two.MethodTwo(); 
      _four.MethodFour(); 
     } 

     public override void ProcessB() 
     { 
      Console.WriteLine("\nProcessB of Facade uses the following subsystems to accomplish the task:"); 
      _two.MethodTwo(); 
      _three.MethodThree(); 
     } 

    } 
} 

兩個給輸出如:

Requests received from Client System and Facade is in execution... 

ProcessA of Facade uses the following subsystems to accomplish the task: 
SubSystem One 
SubSystem Two 
SubSystem Four 

ProcessB of Facade uses the following subsystems to accomplish the task: 
SubSystem Two 
SubSystem Three 
+0

我有一些想法,但我並不完全確定 - 你能解釋/引用更多關於上下文,因爲我不知道這本書/來源? – Carsten

+0

立面是一個假面,隱藏在它後面的血淋淋的細節。 – kenny

回答

3

抽象類從執行單獨的接口,至少當一個方法周圍限定爲抽象的或虛擬的。接口是抽象類的邏輯極端,因爲它們是100%純粹的虛擬抽象方法。

當客戶端處理接口類型時,他們不知道它是如何實現的。關於抽象方法是如何工作的,並沒有任何知識,因此解耦更大。

+0

理解謝謝了一噸... :) –

+0

嘿,你能再請幫助我嗎?在抽象類實現上很糟糕。儘管在線閱讀教程,但我已經使用抽象類實現了代碼。你能否使用抽象類驗證上面發佈的代碼,並告訴我是否正確實現了它。 –

+0

**當客戶端代碼從工廠或服務定位器獲得實際實現時,最有意義**如果您的客戶端代碼選擇並創建具體實現,那麼它沒有完全解耦 - 儘管它可能是一個步驟方式。 –