gpt4 book ai didi

design-patterns - 设计模式中的抽象类

转载 作者:行者123 更新时间:2023-12-04 06:56:41 26 4
gpt4 key购买 nike

对于 Erich Gamma 关于设计模式的可重用面向对象软件的元素一书中的外观设计模式,实现部分讨论了使外观类成为抽象类,因为它减少了客户端和子系统的耦合。

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

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

最佳答案

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

当客户处理接口(interface)类型时,他们不知道它是如何实现的。完全不知道抽象方法是如何工作的,因此解耦更大。

关于design-patterns - 设计模式中的抽象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7546221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com