简体   繁体   English

避免模​​块之间的依赖

[英]Avoid dependencies between modules

For the sake of the question let's say I have a cryptographic module opensslCryptographicModule that have the following methods:为了这个问题,假设我有一个加密模块opensslCryptographicModule ,它具有以下方法:

public String encrypt(String content, String key);
public String decrypt(String encrypted, String key);

And an other module, let's call it MainModule that needs a cryptographic module to work.还有一个模块,我们称之为MainModule ,它需要一个加密模块才能工作。

Since the cryptographic module used in MainModule may change I created an interface as follow:由于MainModule使用的加密模块可能会发生变化,因此我创建了一个界面如下:

public interface CryptographicModule {
    public String encrypt(String content, String key);
    public String decrypt(String encrypted, String key);
}

And the MainModule takes a CryptographicModule in its constructor. MainModule在其构造函数中采用CryptographicModule

My question is how do I keep the cryptographic module totally independant ?我的问题是如何保持加密模块完全独立?

If I put the interface in the MainModule then the opensslCryptographicModule will need to implements an interface declared in an other project and therefore will become dependant of that project.如果我将接口放在MainModuleopensslCryptographicModule将需要实现在其他项目中声明的接口,因此将依赖于该项目。

But if I put the interface in the opensslCryptographicModule then the other cryptographic modules will need opensslCryptographicModule to get the interface.但是如果我将接口放在opensslCryptographicModule那么其他加密模块将需要opensslCryptographicModule来获取接口。


The only solution that I can see is a third module that will make the link between both of the modules.我能看到的唯一解决方案是第三个模块,它将在两个模块之间建立链接。

It will implements the interface (that will be in the MainModule) using the adapter pattern to provides the functionalities of the cryptographic module:它将使用适配器模式实现接口(将在 MainModule 中)以提供加密模块的功能:

public class OpenSSLCryptographicModuleAdapter implements CryptographicModule  {

    private OpensslCryptographicModule module;    

    public OpenSSLCryptographicModuleAdapter(OpensslCryptographicModule module) {
        this.module = module 
    }

    @Override
    public String encrypt(String content, String key) {
        //...
    }

    @Override
    public String decrypt(String encrypted, String key) {
        //...
    }
}

And the MainModule will rely on this third module to work.MainModule将依赖这第三个模块来工作。

But I find it a bit overkill especially when we have control of all the modules.但我觉得这有点矫枉过正,尤其是当我们可以控制所有模块时。 This design pattern is great when using a third party library or if we want to works with some old code, but not when the whole project is been written.这种设计模式在使用第三方库或我们想使用一些旧代码时非常有用,但在编写整个项目时则不然。

Your analysis is correct, including the part where you state this might be overkill.您的分析是正确的,包括您声明这可能是矫枉过正的部分。 This is a fundamental piece of "design patterns" that people often miss.这是人们经常错过的“设计模式”的基本部分。 Sometimes, the benefit of applying a pattern (decoupling, etc.) does not outweigh the cost (which is often "complexity").有时,应用模式(解耦等)的好处不会超过成本(这通常是“复杂性”)。 Part of engineering is figuring out whether the tradeoff is worth it.工程的一部分是弄清楚这种权衡是否值得。
Three modules might be a good idea, or it might be overkill for your case;三个模块可能是个好主意,或者对您的案例来说可能有点矫枉过正; it really depends on the specifics of your project.这实际上取决于您的项目的具体情况。 It might be a good idea to start with a simpler setup, then factor interfaces out later, as you discover where the appropriate abstractions are.最好从更简单的设置开始,然后在发现适当的抽象位置时将接口分解。 It's impossible to give a general rule for something like this.不可能为这样的事情给出一般规则。

You could make OpenSSLCryptographicModule and abstract class, and make encrypt and decrypt abstract methods.您可以制作 OpenSSLCryptographicModule 和抽象类,并制作加密和解密抽象方法。 Then you'll only have one base abstract class, and another concrete class for each implementation.那么对于每个实现,您将只有一个基本抽象类和另一个具体类。

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

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