Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

c# - Constructing two objects that need each other

A puzzling architectural question: You have two symmetrical classes A and B. Each A/B object may privately produce a value of type IA/IB using the A.CreateIA()/B.CreateIB() methods. These values are needed by the opposite classes - A needs IB and B needs IA.

The goal is to write the PairMaker.MakePair() function that constructs an interlinks a pair of A and B objects. You also have to write appropriate constructors for the A and B classes. A and B classes are in different assemblies and don't see each other's internals. The link should be secure - the external code should not be able to access or modify the object fields. You can write additional classes and add any methods to A and B as needed - just don't break the security of the link.

interface IA { }
interface IB { }

class A {
    IB ib;

    //Some constructor
    //Other members

    IA CreateIA() { }
}

class B {
    IA ia;

    //Some constructor
    //Other members

    IB CreateIB() { }
}

class PairMaker {
    public static Tuple<A, B> MakePair() {
        //What's here?
    }
}

This question is similar to How to construct two objects, with each other as a parameter/member, but that question wasn't answered properly.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is a possible solution. I don't like how it looks (I hate those out parameters in the constructors).

class A {
    IB _ib;

    public A(out Func<IA> getter, out Action<IB> setter) {
        getter = CreateIA;
        setter = SetIB;
    }
    void SetIB(IB ib) {
        _ib = ib;
    }
    IA CreateIA() { throw new NotImplementedException(); }
}

class B {
    IA _ia;

    public B(out Func<IB> getter, out Action<IA> setter) {
        getter = CreateIB;
        setter = SetIA;
    }
    void SetIA(IA ia) {
        _ia = ia;
    }
    IB CreateIB() { throw new NotImplementedException(); }
}

.

class PairMaker {
    public static Tuple<A, B> MakePair() {
        Func<IA> iaGetter;
        Func<IB> ibGetter;
        Action<IA> iaSetter;
        Action<IB> ibSetter;
        A a = new A(out iaGetter, out ibSetter);
        B b = new B(out ibGetter, out iaSetter);
        iaSetter(iaGetter());
        ibSetter(ibGetter());
        return Tuple.Create(a, b);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...