开发者

Abstract Class in Design Pattern

For Facade Design Pattern in the book the book Elements of Reusable Object Oriented Software by Erich Gamma on degign patterns, the implementation part talks about making the facade class an abstract class as it reduces the client and subsystem coupling.

I am not able to understand this point. How can making a class abstract help in reducing the coupling. Somebody please help me in understanding this.

Original Code without Facade class as an abstract class:

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

Code with Facade class as an Abstract Class:

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 abstra开发者_运维问答ct 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();
        }

    }
}

Both giving output as:

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


Abstract classes separate interface from implementation, at least when a method is demarked as abstract or virtual. Interfaces are the logical extreme for abstract classes, because they are 100% pure, virtual, abstract methods.

When a client deals with an interface type, they have no idea about how it's implemented. There's no knowledge at all about how the abstract methods work, hence greater decoupling.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜