0

我指的是 Eric Gamma 所著的 Elements of Reusable Object Oriented Software 关于设计模式的书。然而,我理解了外观模式的概念,但仍然无法理解书中给出的实现点,因为我对实现部分特别是有点差。

以下是书中提到的2点:

  1. 减少客户端子系统耦合:通过使 Facade 类成为抽象类。

  2. 公共与私有子系统类。

有人可以用一些例子或我的代码来解释我吗:

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

    }
}
4

1 回答 1

2

Facade用于减少程序之间的耦合。

如示例 ProcessA 调用 3 个方法 -
_one.MethodOne();
_two.MethodTwo();
_four.MethodFour();

客户端只调用 ProcessA 方法。门面只是用来减少耦合、依赖。

如果没有门面,客户端将是调用这些方法的人。

所以 Facade 类提供以下内容 -

  1. 隐藏多个调用。这很有帮助,因为客户只需拨打一个电话。防止紧耦合。例如仅 ProcessA
  2. 如果任何方法更改添加或删除参数,则客户端代码需要更改。但是,在外观的情况下,更改不会影响客户端。
  3. 客户端只有几个到服务器端的公共访问点。并且服务器端可以封装它的代码。故障点少。
于 2011-09-24T18:54:06.087 回答