对于Facade Design Pattern,在书中出版了“可重复使用的面向对象软件的元素”,由Erich Gamma撰写的关于渐晕模式的书,实现部分讨论了将facade类设为抽象类,因为它减少了客户端和子系统的耦合。
我无法理解这一点。如何使类抽象有助于减少耦合。有人请帮助我理解这一点。
没有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();
}
}
}
将Facade类作为抽象类的代码:
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 :(得分:3)
抽象类将接口与实现分开,至少在将方法标记为抽象或虚拟时。接口是抽象类的逻辑极端,因为它们是100%纯粹的虚拟抽象方法。
当客户端处理接口类型时,他们不知道它是如何实现的。根本不了解抽象方法如何工作,因此更加分离。