user1006897
user1006897

Reputation: 13

Class design if two class needs to exchange information

If I have two classes as

class A
{
}

and

class B
{
}

class A wants to use methods of class B and vice-versa.

What should be the best design other than the mediator pattern?

Upvotes: 0

Views: 126

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

At the minimum, you should decouple your classes with interfaces. This way your intended contract between the two classes would be captured explicitly through the interfaces.

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;
}

Other than that, it depends a lot on the nature of interaction that you are planning.

Upvotes: 2

Related Questions