简体   繁体   English

门面设计模式和紧密耦合

[英]Facade design pattern and close coupling

When learning Facade design pattern, everywhere I find this kind of examples: 学习Facade设计模式时,无处不在我找到这样的例子:

public class SystemA
{
 public void CreateCreditCard(){//implementation}
 //other methods here
}

public class SystemB
{
 public void CheckCreditCard(){//implementation}
//other methods here
}

public class Facade
{
 SystemA subsystemA = new SystemA();
 SystemB subsystemB = new SystemB();

 public void CreateAccount()
 {
   subsystemA.CreateCreditCard();
   subsystemB.CheckCreditCard();
 }
}

I don't know if I'm wrong but doesn't it create close coupling between the Facade class and the Systems(SystemA and SystemB), even if SystemA and SystemB are inherited from some abstract class or interface. 我不知道我是否错了,但它不会在Facade类和Systems(SystemA和SystemB)之间创建紧密耦合,即使SystemA和SystemB是从某个抽象类或接口继承的。

In a way you wrote your example, yes it will tightly couple your code. 在某种程度上你编写了你的​​例子,是的,它将紧密地结合你的代码。 Mainly due to new keyword which acts like a glue to your dependencies. 主要是由于new关键字对你的依赖关系起着glue作用。

Keep in mind that Facade pattern will not prevent you from creating tightly coupled dependencies or code. 请记住,Facade模式不会阻止您创建紧密耦合的依赖项或代码。 Main purpose of using it is to make your software component easier to use, more readable and maintainable and last but not least more testable. 使用它的主要目的是使您的软件组件更易于使用,更易读和可维护,并且最后但并非最不可测试的更多。

If you want to avoid tightly coupling, you need to pass abstract dependencies in your Facade class: 如果要避免紧密耦合,则需要在Facade类中传递抽象依赖项:

public class Facade
{
 private readonly ISystemA subsystemA;
 private readonly ISystemB subsystemB;

 public Facade(ISystemA subsystemA, ISystemB subsystemB)
 {
    this.subsystemA = subsystemA;
    this.subsystemB = subsystemB;
 }

 public void CreateAccount()
 {      
   this.subsystemA.CreateCreditCard();
   this.subsystemB.CheckCreditCard();
 }
}

You need to create interfaces (or abstract classes): 您需要创建接口(或抽象类):

public interface ISystemA
{
 void CreateCreditCard();
 //other methods here
}

public interface ISystemB
{
  void CheckCreditCard();
  //other methods here
}

In that way you ensured that your Facade does not depend upon implementation, rather it depends upon abstraction. 通过这种方式,您可以确保Facade不依赖于实现,而是依赖于抽象。 You'll be able to pass any implementation which implements ISystemA or ISystemB intefaces. 您将能够传递任何实现ISystemAISystemB接口的实现。

I suggest you to read more about Dependency Injection and containers which will greatly help you to wrap up classes' dependency graphs and automate constructor injection in that classes. 我建议你阅读更多关于依赖注入和容器的内容,这将极大地帮助你包装类的依赖图并自动化该类中的构造函数注入。

Facade patterns , hides the complexity of the system and provides an interface to the client from where the client can access the system. 外观模式隐藏了系统的复杂性,并为客户端提供了访问系统的接口。
Typical example is compiler while as a client you can invoke compile method, but you don't see internal steps like scanning, parsing, ... 典型的例子是编译器,而作为客户端,您可以调用编译方法,但是您没有看到内部步骤,如扫描,解析,...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM