【发布时间】:2011-11-13 02:53:50
【问题描述】:
我有一个关于我想在我的应用程序中实现的中介模式的问题(使用 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和同事在不同的命名空间和程序集中,如何解决循环依赖?
【问题讨论】:
-
依赖于抽象,而不是具体化。 Mediator 和 Colleague 可以在单独的程序集中,但接口应该在它们自己的程序集中。
标签: c# design-patterns mediator