README for the abstract factory example by Professor Futrelle For COM1204, July 14, 2002. The key point of this example can be seen in the file AbstractFactoryTest.java. The lines below are numbered for discussion. Line 17 shows that theTrip is of class (type) DogShoppingTrip. But in lines 23 and 34, two distinct subclasses of DogShoppingTrip are returned, LittleDogShoppingTrip at line23 and BigDogShoppingTrip at line 34. Look at the TripFactory class to see how this is done; the source, TripFactory.java, is short and simple. Once the appropriate subclass is returned, the three objects are obtained from theTrip, of classes Dog, DogHouse and DogFood. But depending on what type of shopping trip is being used, the instances are different, having different values of an internal descriptor in each. This is because, for example, buyDog() exists in two variants. One of them can be found in BigDogShoppingTrip and has the definition: Dog buyDog(){ return new Dog("BIG!"); } There are five additional function definitions to handle the three functions variants in each of the two subclasses. You can see them for yourself. So that's how this example works. It contains seven classes plus a testing class: Dog.java DogHouse.java DogShoppingTrip.java DogFood.java BigDogShoppingTrip.java AbstractFactoryTest.java LittleDogShoppingTrip.java TripFactory.java In class, we'll discuss some of the alternatives to this approach which don't work as well, to help recommend the use of the abstract factory, when it's appropriate. I hope this example helps you. -- Professor Futrelle 1 /** 2 * This is to demonstrate abstract factory classes. 3 * 4 * @author Bob Futrelle 5 * @version 1.0 7/12/2002 6 * 7 */ 8 9 public class AbstractFactoryTest { 10 11 12 public static void main(String[] args) { 13 14 Dog dog; 15 DogHouse dogHouse; 16 DogFood dogFood; 17 DogShoppingTrip theTrip; 18 19 // Create the factory 20 TripFactory trips = new TripFactory(); 21 22 // Get a little dog shopping trip 23 theTrip = trips.getTrip(DogShoppingTrip.LITTLE); 24 25 say("Here's the info for the little dog shopping excursion:\n"); 26 dog = theTrip.buyDog(); 27 dog.speak(); 28 dogHouse = theTrip.buyDogHouse(); 29 dogHouse.describe(); 30 dogFood = theTrip.buyDogFood(); 31 dogFood.describe(); 32 33 // Get a BIG DOG shopping trip 34 theTrip = trips.getTrip(DogShoppingTrip.BIG); 35 36 say("\nHere's the info for the BIG DOG shopping excursion:\n"); 37 // Note: code below is the same as the above 38 dog = theTrip.buyDog(); 39 dog.speak(); 40 dogHouse = theTrip.buyDogHouse(); 41 dogHouse.describe(); 42 dogFood = theTrip.buyDogFood(); 43 dogFood.describe(); 44 45 46 } // main() 47 48 static void say(String s) { 49 System.out.println(s); 50 } 51 }