我有一个关于调解器模式的问题,我想在我的应用程序中实现(使用C#)。在我的代码中实现模式时,我遇到了循环依赖。这些类的结构如下:使用c调解器模式的循环依赖关系#
Mediator
和Colleague
组件/类在不同的程序集中,因为中介模式需要两个组件(类)相互使用。引用对方时出现问题。
考虑下面的代码:
namespace Mediator
{
public abstract class IMediator
{
public IColleague colleague{get;set;}
void Register();
void Send();
}
public class MediatorA:IMediator
{
void Register(){//code here}
void Send(){//code here}
}
}
namespace Colleague
{
public abstract class IColleague
{
IMediator mediator;
void Send();
void Recieve();
}
public class ColleagueA:IColleague
{
void Send(){//code here}
void Recieve(){//code here}
}
}
为Mediater和同事都在不同的命名空间和组件,如何解决循环依赖?
取决于抽象,而不是结核。调解员和同事可以在单独的集会中,但是interfacas应该在他们自己的集会中。 – MattDavey