Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Two objects interating with a third without knowing about each other

Suppose I have two classes, Foo and Bar, shown bellow:

public class Foo{

private Bar _bar;

public int Value { get; private set; }

public void UpdateValue(Bar bar) 
{
    // Update Value, say:
    Value++;
    // What do I put here to change bar?
}

// Bunch of methods.

}

public class Bar{

private List<Foo> _foos;

public bool Value {get; private set; }

public void UpdateValue(Foo foo)
{
    // Update Value, say:
    Value = !Value;
    
    // What do I put here to change foo?

}

// A bunch of other methods.

}

I want to have two instances of Foo, say fooA and fooB, and one instance of bar. bar contains copy references to both Foo instances, and each Foo instance contains a copy reference to bar. The idea being that bar "knows" fooA and fooB, and vice-versa, but fooA doesn’t know about fooB.

I wan’t to have both fooA and fooB interacting with bar, without interacting with one another. Say, if fooA changes something about bar, fooB must be told about these changes, without necessarily knowing if bar changed itself, or if something else changed bar. I want both fooA and fooB and bar to have the most up-to-date information about one-another at all times. The thing is, I’m not sure the best way to implement these interactions.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a good way, according to best-practices, to do this? Or better yet, how do I make both foo instances interact with bar to change each other inside values simuntaneously without getting to know each other (any change is valid, I’m only trully worried about how the interaction can be implemented).

>Solution :

I think this is what you want:

public class Foo
{
    private Bar _bar;

    public int Value { get; private set; }

    public void UpdateValue(Bar bar)
    {
        // Update Value, say:
        Value++;
        // What do I put here to change bar?
        bar.UpdateValue(this);
    }

    // Bunch of methods.

}

public class Bar
{

    private List<Foo> _foos;

    public bool Value { get; private set; }

    public void UpdateValue(Foo foo)
    {
        // Update Value, say:
        Value = !Value;

        // What do I put here to change foo?
        foo.UpdateValue(this);
    }

    // A bunch of other methods.

}

But then you end up with some endless recursion.

So you need to specify a terminal condition where one update will not call the other update.

For example

    public void UpdateValue(Foo foo)
    {
        // Update Value, say:
        Value = !Value;
        if(Value) 
        {
            // What do I put here to change foo?
            foo.UpdateValue(this);
        }
    }

This will ensure an end to one method calling the other.

There just isn’t enough information in the question to understand your intent with just this code snippet.

Also, note that both Foo and Bar are classes which means references such as _bar will always point to the most up-to-date information. They don’t store a copy of Bar but point to the memory where the Bar object is defined. So there is no need to push changes around unless the state of one class depends on the state of the other.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading