循环依赖现象问题 [英] Spot problems with circular dependency

查看:149
本文介绍了循环依赖现象问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个系统,其中有两个模块,一个用于打印文件和另一个用户。对于某些逻辑操作,他们需要彼此提供的服务。

I am designing a system where two modules, one which gestions files and another users. For certain logic operations, they need the services offered by each other.

每个模块都由一个单例表示,该单例实现了一个提供一些服务的接口,其中包含抽象工厂提供给他们,如下所示:

Each module is represented by a singleton which implements a interface offering some services to each other, with abstract factories to provide them, like so:

public class UserMain implements UserInternalService {

/*
 * Internal interfaces
 */
/**
 * Allows interaction with the projects database.
 */
FilesInternaService fileSystem;

/**
 * Constructor is private, as this is a singleton.
 */
protected UserMain() {
}

private static UserMain singleton = null;

/**
 * Singleton factory. Returns a reference to the singleton. If there is no
 * reference yet, creates it.
 */
protected static synchronized UserMain getReference() {
    if (singleton == null) {
        singleton = new UserMain();
        singleton.fileSystem = FileMain.getInternalService();
    }
    return singleton;
}

/**
 * Factory method for the singleton as a UserInternalService
 */
public static UserInternalService getUserInternalService() {
    return getReference();
}

}

文件模块主类是像这样:

And the file module main class is like so:

public class FileMain implements FilesInternaService{

/**
 * Interface to user subsystem for request validation, etc.
 */
UserInternalService userSystem;

/**
 * Creation of instances aside from singleton disallowed.
 */
protected FileMain(){};

private static FileMain singleton = null;

/**
 * Singleton factory.
 * Returns a reference to the singleton.
 * If there is no reference yet, creates it.
 */
protected synchronized static FileMain getReference(){
    if(singleton == null)
        singleton = new FileMain();
        singleton.userSystem = UserMain.getUserInternalService();
    return singleton;
}

/**
 * Abstract factory for Internal Services singleton.
 * @return
 */
public static FilesInternaService getInternalService(){
    return getReference();
}
}

我不知道我是否正确处理循环依赖。
有什么办法可能会意外中断吗?

I am not enterely sure that I am correctly handling the circular dependency. Is there any way this may break unexpectedly?

编辑:正如下面已经回答的那样,正确的处理方法是注射。但是,处理这个问题的正确方法不是我在这里问的问题,而是这个具体解决方案如何爆发。

EDIT: As it has been answered below, the correct way to handle this is injection. However, the correct way to handle this is not what I am asking here, but rather how could this specific solution blow up.

推荐答案

处理这个的干净的方法是使用依赖注入,以保持接口级别的依赖。

The clean way to handle this is using dependency injection, to keep the dependencies at the interface level.

UserMain 依赖于 FilesInternaService ,它可以 FileMain 依赖于 UserInternalService ;但是对于 UserMain 依赖于 FileMain FileMain 依赖 UserMain 。换句话说,依赖于具体的实现是不行的。

It's ok for UserMain to depend on FilesInternaService and it's ok for FileMain to depend on UserInternalService; but it's not ok for UserMain to depend on FileMain or for FileMain to depend on UserMain. In other words, it's not ok to depend on concrete implementation.

应该将 FilesInternaService 的一个实例注入 UserMain ,一个 UserInternalService 的实例应注入 FileMain

An instance of FilesInternaService should be injected into UserMain and an instance of UserInternalService should be injected into FileMain.



参考


  1. 循环依赖是否被认为是坏设计?

  2. 为什么循环引用被认为是有害的?

  3. https://softwareengineering.stackexchange.com/questions/11856 / what--wrong-with-circular-references

  4. https://softwareengineering.stackexchange.com/questions/306483/how-to-solve-circular-依赖性

  1. Are circular dependencies considered bad design?
  2. Why are circular references considered harmful?
  3. https://softwareengineering.stackexchange.com/questions/11856/whats-wrong-with-circular-references
  4. https://softwareengineering.stackexchange.com/questions/306483/how-to-solve-circular-dependency

这篇关于循环依赖现象问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆