我有一個關於調解器模式的問題,我想在我的應用程序中實現(使用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