C# 如果两个类需要交换信息,则进行类设计

C# 如果两个类需要交换信息,则进行类设计,c#,design-patterns,c#-4.0,C#,Design Patterns,C# 4.0,如果我有两门课 class A { } 及 class A希望使用class B的方法,反之亦然 除了中介模式之外,最好的设计应该是什么?至少,您应该将类与接口解耦。这样,两个类之间的预期契约将通过接口显式捕获 interface IA { // Methods for use in class B... } class A : IA { private readonly IB b; } interface IB { // Methods for use in cl

如果我有两门课

class A
{
}

class A
希望使用
class B
的方法,反之亦然


除了中介模式之外,最好的设计应该是什么?

至少,您应该将类与接口解耦。这样,两个类之间的预期契约将通过接口显式捕获

interface IA {
    // Methods for use in class B...
}

class A : IA {
    private readonly IB b;
}

interface IB {
    // Methods for use in class A...
}

class B : IB {
    private readonly IA a;
}

除此之外,这在很大程度上取决于你计划的互动性质

为什么不采用中介模式?(我不确定这是正确的模式,但为什么反对?)
interface IA {
    // Methods for use in class B...
}

class A : IA {
    private readonly IB b;
}

interface IB {
    // Methods for use in class A...
}

class B : IB {
    private readonly IA a;
}