next up previous
Next: The Composite Pattern Up: The Union and Composite Previous: The Union and Composite

Member Hoisting

The preceding Java code can be improved by eliminating duplicated code. Note that the fields name, address, and phone appear in all three concrete subclasses of the abstract class CityEntry. So do definitions of the corresponding accessors getName, getAddress, and getPhone. These repeated member definitions can be hoisted into the abstract class CityEntry yielding the following Java code:

abstract class CityEntry {

  String name;	
  String address;
  String phone;

  /* accessors */
  String getName() { return this.name; }	
  String getAddress() { return this.address; }
  String getPhone() { return this.phone; }
}

class BusinessEntry extends Entry {

  String city;
  String state;

  /* constructor */
  BusinessEntry(String n, String a, String p, String c, String s) {
    this.name = n;
    this.address = a;
    this.phone = p;
    this.city = c;
    this.state = s;
  }

  /* accessors */
  String getCity() { return this.city; }
  String getState() { return this.state; }	
}

class GovernmentEntry extends Entry {

  String city;
  String state;
  String government;

  /* constructor */
  GovernmentEntry(String n, String a, String p, String c, String s, String g) {
    this.name = n;
    this.address = a;
    this.phone = p;
    this.city = c;
    this.state = s;
    this.government = g;
  }

  /* accessors */
  String getCity() { return this.city; }
  String getState() { return this.state; }	
}

class ResidentialEntry extends Entry

  String address;
  String phone;

  /* constructor */
  ResidentialEntry(String n, String a, String p) {
    this.name = n;	
    this.address = a;
    this.phone = p;
  }
}


Finger Exercise By applying some very simple program transformations, you can eliminate more code duplication in the CityEntry class and subclasses. Note that the subclasses BusinessEntry and GovernmentEntry include common fields and accessors. Introduce a new abstract class NonResidentialEntry extending CityEntry to hold these common class members. After this addition, the class CityEntry still has only three concrete subclasses but only one of them is an immediate subclass. The other immediate subclass is NonResidentialEntry. Test your code using Drjava.


Optional Finger Exercise Note that the constructor for each concrete subclass of CityEntry replicates the code for initializing the fields address and phone defined in the abstract class CityEntry. Similarly, the constructor for each concrete subclass of NonResidentialEntry replicates code for initializing the fields city and state. Consult a Java language reference book such as The Java Programming Language by Arnold and Gosling or Java in a Nutshell by Flanagan to learn about the use of super calls in the definition of constructors. Define appropriate constructors for the abstract classes CityEntry and NonResidentialEntry and convert the constructor definitions in the concrete variants to invoke these new constructors using super. Test your code using DrJava.


next up previous
Next: The Composite Pattern Up: The Union and Composite Previous: The Union and Composite
Corky Cartwright
1999-09-07