Info

7 public double getLeafAreaIndex() {

8 return leafSystem.getLeafAreaIndex();

Figure 7-3. Implementation in Java of the delegation pattern.

Using the same principle of delegation, class Plant will be able to respond to messages that are defined in classes RootSystem and StemSystem. Class Plant is the main distributing hub for component Plant, as it controls access to other classes. If RootSystem and StemSystem classes had a direct link with classes outside Plant component, that would have compromised the principle of encapsulation that needs to be observed during the component design process.

3. CREATIONAL PATTERNS 3.1 The factory method pattern

The object-oriented approach is about creating objects with a specific behavior to interact with other objects. It is a good modeling practice to make the process of creating objects localized, so if changes have to be made to the way objects are created, these changes will occur in only one place in the code. The factory method pattern is about organizing the object creation process in such a way that new type of objects can be added to the system without reconsidering what is already in place. This is reached by forcing the process of object creation to occur through a common factory, rather then allowing it to be dispersed all over the application. If a new type of object needs to be added to the system, then appropriate changes need to be made to the factory that creates objects.

Let us consider a crop simulation scenario in which many instances of plants will be created such as maize, rice, and wheat. All of these types of plants have some attributes in common such as variety, planting date, etc. The common data and behavior can be defined in an abstract class referred to as Plant and subclasses of Plant will define additional data and behavior to describe a particular type of plant. The crop simulation system should be able to simulate many types of crops; therefore, objects of different plants will be created. As mentioned before, one solution can be that the process of object creation can be dispersed throughout the code and objects are created when and where they are needed. This solution has a big disadvantage. When changes are needed to be made to the process of object creation, we will need to find all the places where objects are created and do the necessary changes.

The factory method pattern provides a better way of creating objects and works as shown in Figure 7-4.

Figure 7-4. The factory method pattern.

As shown in this figure, the Simulator sends a create request to PlantFactory class to create an object of a particular type of plant. PlantFactory is provided with a method referred to as createPlant(String plant) used to create the required object. This method has a parameter of type String named plant that indicates the type of the object to be created. Thus, if an object of type Maize needs to be created, then the Simulator sends the message createPlant (maize) to PlantFactory. The following code shows the implementation in Java of the class PlantFactory. The kinds of objects that need to be created need to be known in advance and the corresponding code is included in the factory. Figure 7-5 shows that the following factory creates objects of type Maize, Wheat, and Rice. If new kinds of objects need to be created, then the factory needs to be updated but the update will occur in one and well-defined place.

1 package FactoryMethodPattern;

0 0

Post a comment