Tuesday, 15 January 2013

c# - Abstracting from Stateful object navigation (1-1) - the challenge -


The issue of this exercise is to create navigation among objects

For example, 1-1 of the Association The person should be aware and address:

  • If any person has been given an address, then that person should be assigned to the address (and vice versa).
  • If the address is assigned to 1 person and then person 2, then there will be no address of person 1 and the person will be 2.

This is the code snippet that implements it.

  public class A {internal BA; Public b value {receive {return; } Set {if (value == null) {if (a! = Null) a.a = null; } And value. A = this; A = value; }}} Public Segment B {Internal AA; Public A Value {Get {Return; } Set {if (value == null) {if (a! = Null) a.a = null; } And value. A = this; A = value; }}}  

This allows the following tests to be passed:

  // For general setup: var a = new A (); Var B = new B (); // Test 1: A. Val = b; Apprehension Same (A, B. Well); // Test 2: b. Value = a; Assurance Reekval (b, a.well); // test3: b. Value = a; B Value = null; Assert.IsNull (a.Value); // test4: var a2 = new A (); B Value = A2; Apprehension Same (B, A 2. Well); Apprehension Oh no (A, B. Well); // Test 5: a. Value = b; Apprehension Same (A, B. Well); Var a1 = New A (); Var B1 = New B (); A1.Value = b1; Decide. Same (A1, B1 value); // test 6: var a1 = new A (); Var B1 = New B (); Assert.IsNull (a.Value); Assert.IsNull (b.Value); Assert.IsNull (a1.Value); Assert.IsNull (b1.Value);  

The question now is how will you handle the code in the setters to avoid potential mistakes while writing to such classes?

:

  • Class A and B's public interfaces can not be changed.
  • The factory should not be used.
  • Statistics should not be used (to continue sharing information).
  • ThreadInfo or similar should not be used
  • I really can not understand your challenge, what happens when you go to class A Let's start some examples and then an example of Class B?

      a1 = new A (); A2 = new A (); A3 = new A (); AA4 = new A (); AA5 = new A (); BB = new B ();  

    Which test is near? Which fails?

    You can see, once A occurs immediately, it has state in some way by B Must include an example of an current example . Therefore this example of B must be present before you start this A class.

    Also B . There should be a reference for an already present example of A .

    As far as I understand, Class A should be a constructor with reference to the current version: <: >

      Public category A {Private BB; Public A (BB) {this.b = b; }} // Then you can have: BB1 = new B (); A1 = New A (B1); // Here the link is BB2 = new B (); A2 = New A (B2); // and another link  

    either with this or the other b .

    You write that you do not want to change the public signature of A and b , and you do not want to add factories in the code. I really can not see the solution continuously under such obstacles, or the challenge itself is not clear?

    EDIT: By making a wild guess here, I think that you try to get it here, using reflection can be done: you want to reflect the current code point (call stack For a current instance of A , and a new example of A . It may be by using the image, but it is very hard and you have a concrete rule for the relationship between the new examples of A s and And should be strong set b . If the direction of this solution is , then I think you should dive in the reflection and see how it goes, it is a large area.


No comments:

Post a Comment