Setting up the Example Object Graph


Let's look at an example set of relationships , as shown by Figure 8-2. In English, it's possible to describe particular relationships between Bob Smith and the other people as expressed from Bob Smith's perspective. For example, we would say that Nancy Swenson is Bob's maternal grandmother, or that Zeppo and Mocha are Bob's grandchildren.

Figure 8-2. Bob Smith's family tree.


In Java, we can represent all of these people (and their relationships) with a set of Java objects of class Person, as shown in Listing 8-3. Notice that when an object specifies a father or mother, the relationship is made bidirectional (the mother or father has the referring object marked as a child).

Listing 8-3. Person Class
 package com.cascadetg.ch08; import java.util.Hashtable; public class Person {     String firstName;     String lastName;     Person mother;     Person father;     Hashtable children = new Hashtable(0);     String gender;     public static final String MALE = "male";     public static final String FEMALE = "female";     public Person(String gender) {this.gender = gender;}     public Person(String gender, String firstName, String lastName)     {         this.firstName = firstName;         this.lastName = lastName;         this.gender = gender;     }     public String toString()     { return this.lastName + ", " + this.firstName; }     public java.util.Collection getChildren()     { return children.values(); }     public void addChild(Person child)     { this.children.put(child.toString(), child); }     public Person getFather() { return father; }     public void setFather(Person father)     {         this.father = father;         father.addChild(this);     }     public String getFirstName() { return firstName; }     public void setFirstName(String firstName)     { this.firstName = firstName; }     public String getLastName() { return lastName; }     public void setLastName(String lastName)     { this.lastName = lastName; }     public Person getMother() { return mother; }     public void setMother(Person mother)     {         this.mother = mother;         mother.addChild(this);     }     public String getGender(){ return gender; }     public void setGender(String gender) { this.gender = gender; } } 

Given the Person class shown in Listing 8-3, the code in Listing 8-4 creates a suite of Person objects, initializing an object hierarchy to match the family graph shown in Figure 8-2.

Listing 8-4. Initializing Bob's Family
 package com.cascadetg.ch08; public class FamilyFactory {     static Person startPerson = null;     public static Person getPerson() { return startPerson; }     public static void initFamily()     {         if (startPerson != null)             return;         // Bob is the "heart" of the family         Person bobSmith = new Person(Person.MALE, "Bob", "Smith");         startPerson = bobSmith;         // Bob's father         Person robertSmith =             new Person(Person.MALE, "Robert", "Smith");         bobSmith.setFather(robertSmith);         // Bob's mother         Person maggySmith =             new Person(Person.FEMALE, "Maggy", "Smith");         bobSmith.setMother(maggySmith);         // Bob's sister, married         Person maryJohnson =             new Person(Person.FEMALE, "Mary", "Johnson");         maryJohnson.setMother(maggySmith);         maryJohnson.setFather(robertSmith);         // Bob's mother's mother         Person nancySwenson =             new Person(Person.FEMALE, "Nancy", "Swenson");         maggySmith.setMother(nancySwenson);         // Bob's father's father         Person olavOliver =             new Person(Person.MALE, "Olav", "Oliver");         robertSmith.setFather(olavOliver);         // Bob's first wife         Person juneSmith =             new Person(Person.FEMALE, "June", "Smith");         // Bob's three children         Person kittySmith =             new Person(Person.FEMALE, "Kitty", "Smith");         kittySmith.setFather(bobSmith);         kittySmith.setMother(juneSmith);         Person willowSmith =             new Person(Person.FEMALE, "Willow", "Smith");         willowSmith.setFather(bobSmith);         willowSmith.setMother(juneSmith);         Person jonSmith = new Person(Person.MALE, "Jon", "Smith");         jonSmith.setFather(bobSmith);         jonSmith.setMother(juneSmith);         // Bob's grandchildren         Person zeppoSmith =             new Person(Person.MALE, "Zeppo", "Smith");         zeppoSmith.setFather(jonSmith);        Person mochaSmith =             new Person(Person.FEMALE, "Mocha", "Smith");         mochaSmith.setFather(jonSmith);     } } 



    Apache Jakarta Commons(c) Reusable Java Components
    Real World Web Services
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 137
    Authors: Will Iverson

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net