15 return newPlant;

Figure 7-5. Implementation of class PlantFactory in Java.

As shown in Figure 7-5, line 1 shows the package (subdirectory) where the class PlantFactory is stored. Line 3 shows the class definition. Line 4 shows the default constructor of class PlantFactory. Lines 6 through 16 show that the definition of the method createPlant(Sthng plantName) considers three cases of creation of new objects: Maize, rice and wheat. The newly created object depends on the value of the parameter plantName. Line 15 returns the newly created object. If additional types of plants need to be created, then there is only one place to make the corresponding changes; the method createPlant of class PlantFactory.

0 0

Post a comment