问题
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.
Update: Please note, that this problem IS solvable (as you can write any constructors you want). Please don't downvote or close this question just because you cannot devise a solution.
回答1:
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);
}
}
来源:https://stackoverflow.com/questions/24216746/constructing-two-objects-that-need-each-other