【问题标题】:Abstract Class in Design Pattern设计模式中的抽象类
【发布时间】:2011-09-25 14:41:49
【问题描述】:

对于外观设计模式,Erich Gamma 所著的 Elements of Reusable Object Oriented Software 一书中关于设计模式的内容,实现部分谈到了使外观类成为抽象类,因为它减少了客户端和子系统的耦合。

我无法理解这一点。使类抽象如何有助于减少耦合。有人请帮助我理解这一点。

没有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

【问题讨论】:

  • 我有一些想法,但我不完全确定 - 由于我不知道这本书/来源,您能否解释/引用更多上下文?
  • 外观是一个假的正面,隐藏着它背后的血腥细节。

标签: design-patterns abstract-class


【解决方案1】:

抽象类将接口与实现分开,至少当方法被标记为抽象或虚拟时。接口是抽象类的逻辑极端,因为它们是 100% 纯的、虚拟的抽象方法。

当客户处理接口类型时,他们不知道它是如何实现的。根本不知道抽象方法是如何工作的,因此解耦程度更高。

【讨论】:

  • 嘿,你能再帮我一次吗...我不擅长抽象类的实现。虽然通过在线教程我已经使用抽象类实现了代码。您能否使用抽象类验证上面发布的代码,并让我知道我是否正确实现了它。
  • 最有意义的是,当客户端代码从工厂或服务定位器获取实际实现时。如果您的客户端代码选择并创建具体实现,那么它并不完全解耦——尽管这可能是前进的一步。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多