How can I synchronize three classes?

class Foo
{
    Bar b;
    List<Foo> Neighbours;
}

class Bar
{
    Spam s;
    List<Bar> Neighbours;
}

class Spam
{
    List<string> Neighbours; 
}

Each of these classes has methods AddNeighbour, RemoveNeighbour.

The user can add / remove neighbors from any class in a random order.

I want these three objects to be in sync .

How can i do this?

+3
source share
2 answers

Option one. You have a Foo call bar when it is updated and a Bar Spam call. This will result in tightly coupled objects, and you will probably also need to return Spam back to Bar, which will call Foo back to synchronize in the other direction, yuk.

. , , . ( )

- , <Bar> , List <Foo> List <string> . , ( ) .

4. - , .

EDIT. , , , , . , , .

+4

.

Observer, - , Observer, .

+2

Source: https://habr.com/ru/post/1744888/


All Articles