外观设计模式 - 实现
我指的是 Eric 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();
}
}
}
带有抽象类的代码:
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();
}
}
}
I am referring the book Elements of Reusable Object Oriented Software by Eric Gamma on degign patterns. I however understood the concept of Facade pattern, but still not able to understand the implementation points which has been given in the book as I am little poor with the implementing part esp.
The below are the 2 points mentioned in the book:
Reduce client subsystem coupling: by making the Facade class an abstract class.
Public v/s Private subsystem classes.
Could someone please explain me this with some example or with the code I have:
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 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 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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Facade用于减少程序之间的耦合。
如示例中 ProcessA 调用 3 个方法 -
_one.MethodOne();
_two.MethodTwo();
_four.MethodFour();
客户端只需调用 ProcessA 方法即可。
使用门面只是为了减少耦合、依赖。
如果没有外观,客户端将是调用这些方法的人。
因此,Facade 类提供了以下功能 -
Facade is used to reduce the coupling between the programs.
As in the example ProcessA calls 3 methods -
_one.MethodOne();
_two.MethodTwo();
_four.MethodFour();
And the client just calls the ProcessA method.
The facade is used just to reduce the coupling, dependency.
If no facade the client will be the one to call these methods.
So the Facade class provides the following -