Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
198
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

Example

Note:

For a full working example of this code example, with additional supporting classes and/or a RunPattern class, see “ Composite ” on page 453 of the “ Full Code Examples ” appendix.

The Composite class diagram for the code example is shown in Figure 3.5.

Figure 3.5. Composite class diagram for the code example

The example demonstrates how to use the Composite pattern to calculate the time required to complete a project or some part of a project. The example has four principal parts:

Deliverable –

 

Y

A class that represents an end product of a completed Task.

 

 

L

 

 

F

Project – The class used as the root of the composite, representing the entire project.

 

 

M

ProjectItem –

A

This interface describes functionality common to all items that can be part of a project. The

 

E

 

getTimeRequired method is defined in Tthis interface.

Task – A class that represents a collection of actions to perform. The task has a collection of ProjectItem objects.

The general functionality available to every object that can be part of a project is defined in the ProjectItem interface. In this example, there is only a single method defined: getTimeRequired.

Example 3.9 ProjectItem.java

1.import java.io.Serializable;

2.public interface ProjectItem extends Serializable{

3.public double getTimeRequired();

4.}

Since the project items can be organized into a tree structure, two kinds of classes are ProjectItems. The Deliverable class represents a terminal node, which cannot reference other project items.

Example 3.10 Deliverable.java

 

1.

import java.io.Serializable;

 

2.

public interface ProjectItem extends Serializable{

 

3.

public double getTimeRequired();

 

4.

}

 

The Project and Task classes are nonterminal

classes keep a collection of ProjectItems

that represent children: associated tasks or

 

TEAM FLY PRESENTS

111

Example 3.11 Project.java

1.import java.util.ArrayList;

2.import java.util.Iterator;

3.public class Project implements ProjectItem{

4.private String name;

5.private String description;

6.private ArrayList projectItems = new ArrayList();

8.public Project(){ }

9.public Project(String newName, String newDescription){

10.name = newName;

11.description = newDescription;

12.}

13.

14.public String getName(){ return name; }

15.public String getDescription(){ return description; }

16.public ArrayList getProjectItems(){ return projectItems; }

17.public double getTimeRequired(){

18.double totalTime = 0;

19.Iterator items = projectItems.iterator();

20.while(items.hasNext()){

21. ProjectItem item = (ProjectItem)items.next();

22. totalTime += item.getTimeRequired();

23.}

24.return totalTime;

25.}

26.

27.public void setName(String newName){ name = newName; }

28.public void setDescription(String newDescription){ description = newDescription; }

30.public void addProjectItem(ProjectItem element){

31.if (!projectItems.contains(element)){

32. projectItems.add(element);

33.}

34.}

35.public void removeProjectItem(ProjectItem element){

36.projectItems.remove(element);

37.}

38.}

Example 3.12 Project.java

1.import java.util.ArrayList;

2.import java.util.Iterator;

3.public class Project implements ProjectItem{

4.private String name;

5.private String description;

6.private ArrayList projectItems = new ArrayList();

8.public Project(){ }

9.public Project(String newName, String newDescription){

10.name = newName;

11.description = newDescription;

12.}

13.

14.public String getName(){ return name; }

15.public String getDescription(){ return description; }

16.public ArrayList getProjectItems(){ return projectItems; }

17.public double getTimeRequired(){

18.double totalTime = 0;

19.Iterator items = projectItems.iterator();

20.while(items.hasNext()){

21. ProjectItem item = (ProjectItem)items.next();

22. totalTime += item.getTimeRequired();

23.}

24.return totalTime;

25.}

26.

27.public void setName(String newName){ name = newName; }

28.public void setDescription(String newDescription){ description = newDescription; }

30.public void addProjectItem(ProjectItem element){

31.if (!projectItems.contains(element)){

32. projectItems.add(element);

33.}

34.}

35.public void removeProjectItem(ProjectItem element){

36.projectItems.remove(element);

112

37.}

38.}

Example 3.13 Task.java

1.import java.util.ArrayList;

2.import java.util.Iterator;

3.public class Task implements ProjectItem{

4.private String name;

5.private String details;

6.private ArrayList projectItems = new ArrayList();

7.private Contact owner;

8.private double timeRequired;

9.

10.public Task(){ }

11.public Task(String newName, String newDetails,

12.Contact newOwner, double newTimeRequired){

13.name = newName;

14.details = newDetails;

15.owner = newOwner;

16.timeRequired = newTimeRequired;

17.}

18.

19.public String getName(){ return name; }

20.public String getDetails(){ return details; }

21.public ArrayList getProjectItems(){ return projectItems; }

22.public Contact getOwner(){ return owner; }

23.public double getTimeRequired(){

24.double totalTime = timeRequired;

25.Iterator items = projectItems.iterator();

26.while(items.hasNext()){

27. ProjectItem item = (ProjectItem)items.next();

28. totalTime += item.getTimeRequired();

29.}

30.return totalTime;

31.}

32.

33.public void setName(String newName){ name = newName; }

34.public void setDetails(String newDetails){ details = newDetails; }

35.public void setOwner(Contact newOwner){ owner = newOwner; }

36.public void setTimeRequired(double newTimeRequired){ timeRequired = newTimeRequired; }

38.public void addProjectItem(ProjectItem element){

39.if (!projectItems.contains(element)){

40. projectItems.add(element);

41.}

42.}

43.public void removeProjectItem(ProjectItem element){

44.projectItems.remove(element);

45.}

46.}

The getTimeRequired method shows how the Composite pattern runs. To get the time estimate for any part of the project, you simply call the method getTimeRequired for a Project or Task object. This method behaves differently depending on the method implementer:

Deliverable: Return 0.

Project or Task: Return the sum of the time required for the object plus the results of calling the getTimeRequired method for all ProjectItems associated with this node.

113