2011-09-24 4 views
0

저는 Eric Gamma가 Deign 패턴에 대해 Reusable Object Oriented Software의 Elements라는 요소를 언급하고 있습니다. 그러나 Facade 패턴의 개념을 이해했지만, 여전히 구현 부분을 구현하는 데 약간의 가난함이 없으므로 책에서 제공된 구현 지점을 이해할 수 없습니다.외관 디자인 패턴 - 구현

아래는이 책에서 언급 된 2 점은 다음과 같습니다

  1. 클라이언트 서브 시스템 커플 링을 감소 : 외관 클래스 추상 클래스함으로써.

  2. 공용 v/s 개인 서브 시스템 클래스.

사람은 일부 예제 또는 코드 나에게 이것을 설명시겠습니까 내가 가진 : 추상 클래스와

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

    } 
} 

답변

2

외관에 사용됩니다 프로그램 간의 결합을 줄이십시오.

ProcessA는 다음과 같이 3 개의 메소드를 호출합니다.
_one.MethodOne();
_two.MethodTwo();
_four.MethodFour();

그리고 클라이언트는 ProcessA 메소드를 호출합니다. 외관은 커플 링, 종속성을 줄이기 위해 사용됩니다.

외관이없는 경우 클라이언트가이 메소드를 호출 할 수 있습니다.

  1. 여러 통화를 숨 깁니다 -

    는 그래서 외관 클래스는 다음을 제공합니다. 이것은 클라이언트가 단 한 번의 호출만으로 도움이 될 것입니다. 단단한 결합을 방지합니다. 예 : ProcessA 전용
  2. 메소드 변경으로 인해 인수가 추가되거나 제거되는 경우 클라이언트 코드를 변경해야합니다. 그러나 외관의 경우 변경 사항은 클라이언트에 영향을 미치지 않습니다.
  3. 클라이언트에는 서버 측에 대한 공용 액세스 지점이 몇 개 있습니다. 그리고 서버 측은 코드를 캡슐화 할 수 있습니다. 실패 지점이 적습니다.
+0

감사합니다. 간단하게하기 위해. 하지만 내 주요 질문은 파사드 클래스 추상 만들기의 사용은 무엇입니까. 어떻게 추상적 인 수업이 도움이됩니까? –

+0

외관 클래스를 추상 클래스로 지정하는 것은 필수 사항이 아닙니다. 그들은 또한 구체적인 수업이 될 수 있습니다. 추상적 인 Facade 클래스는 객체가 다른 객체와 기능에 액세스 할 수 있도록하는 기본 인터페이스를 제공하고 실제 구현이이를 준수하도록 도와줍니다. – Jayendra

+0

안녕하세요, 추상 클래스에 대한 내 손을 시도하고 위의 동일한 프로그램에 대한 코드를 게시 ... 확인하시기 바랍니다 제발 알 수있는 경우 제대로 구현 또는 아닙니다. –