//[C] 2002 Sun Microsystems, Inc.---
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
public class RunDecoratorPattern {
public static void main(String [] arguments){
System.out.println("Example for the Decorator pattern");
System.out.println();
System.out.println("This demonstration will show how Decorator classes can be used");
System.out.println(" to extend the basic functionality of ProjectItems. The Task and");
System.out.println(" Deliverable classes provide the basic ProjectItems, and their");
System.out.println(" functionality will be extended by adding subclasses of the");
System.out.println(" abstract class ProjectDecorator.");
System.out.println();
System.out.println("Note that the toString method has been overridden for all ProjectItems,");
System.out.println(" to more effectively show how Decorators are associated with their");
System.out.println(" ProjectItems.");
System.out.println();
System.out.println("Creating ProjectItems.");
Contact contact1 = new ContactImpl("Simone", "Roberto", "Head Researcher and Chief Archivist", "Institute for Advanced (Java) Studies");
Task task1 = new Task("Perform months of diligent research", contact1, 20.0);
Task task2 = new Task("Obtain grant from World Java Foundation", contact1, 40.0);
Deliverable deliverable1 = new Deliverable("Java History", "Comprehensive history of the design of all Java APIs", contact1);
System.out.println("ProjectItem objects created. Results:");
System.out.println(task1);
System.out.println(task2);
System.out.println(deliverable1);
System.out.println();
System.out.println("Creating decorators");
ProjectDecorator decorator1 = new SupportedProjectItem(new File("JavaHistory.txt"));
ProjectDecorator decorator2 = new DependentProjectItem(task2);
System.out.println("Decorators created. Adding decorators to the first task");
decorator1.setProjectItem(task1);
decorator2.setProjectItem(decorator1);
System.out.println();
System.out.println("Decorators added. Results");
System.out.println(decorator2);
System.out.println("");
}
}
interface Contact extends Serializable{
public static final String SPACE = " ";
public String getFirstName();
public String getLastName();
public String getTitle();
public String getOrganization();
public void setFirstName(String newFirstName);
public void setLastName(String newLastName);
public void setTitle(String newTitle);
public void setOrganization(String newOrganization);
}
class ContactImpl implements Contact{
private String firstName;
private String lastName;
private String title;
private String organization;
public ContactImpl(){}
public ContactImpl(String newFirstName, String newLastName,
String newTitle, String newOrganization){
firstName = newFirstName;
lastName = newLastName;
title = newTitle;
organization = newOrganization;
}
public String getFirstName(){ return firstName; }
public String getLastName(){ return lastName; }
public String getTitle(){ return title; }
public String getOrganization(){ return organization; }
public void setFirstName(String newFirstName){ firstName = newFirstName; }
public void setLastName(String newLastName){ lastName = newLastName; }
public void setTitle(String newTitle){ title = newTitle; }
public void setOrganization(String newOrganization){ organization = newOrganization; }
public String toString(){
return firstName + SPACE + lastName;
}
}
class Deliverable implements ProjectItem{
private String name;
private String description;
private Contact owner;
public Deliverable(){ }
public Deliverable(String newName, String newDescription,
Contact newOwner){
name = newName;
description = newDescription;
owner = newOwner;
}
public String getName(){ return name; }
public String getDescription(){ return description; }
public Contact getOwner(){ return owner; }
public double getTimeRequired(){ return 0; }
public void setName(String newName){ name = newName; }
public void setDescription(String newDescription){ description = newDescription; }
public void setOwner(Contact newOwner){ owner = newOwner; }
public String toString(){
return "Deliverable: " + name;
}
}
class DependentProjectItem extends ProjectDecorator{
private ProjectItem dependentItem;
public DependentProjectItem(){ }
public DependentProjectItem(ProjectItem newDependentItem){
dependentItem = newDependentItem;
}
public ProjectItem getDependentItem(){ return dependentItem; }
public void setDependentItem(ProjectItem newDependentItem){ dependentItem = newDependentItem; }
public String toString(){
return getProjectItem().toString() + EOL_STRING
+ "\tProjectItem dependent on: " + dependentItem;
}
}
abstract class ProjectDecorator implements ProjectItem{
private ProjectItem projectItem;
protected ProjectItem getProjectItem(){ return projectItem; }
public void setProjectItem(ProjectItem newProjectItem){ projectItem = newProjectItem; }
public double getTimeRequired(){
return projectItem.getTimeRequired();
}
}
interface ProjectItem extends Serializable{
public static final String EOL_STRING = System.getProperty("line.separator");
public double getTimeRequired();
}
class Task implements ProjectItem{
private String name;
private ArrayList projectItems = new ArrayList();
private Contact owner;
private double timeRequired;
public Task(){ }
public Task(String newName, Contact newOwner,
double newTimeRequired){
name = newName;
owner = newOwner;
timeRequired = newTimeRequired;
}
public String getName(){ return name; }
public ArrayList getProjectItems(){ return projectItems; }
public Contact getOwner(){ return owner; }
public double getTimeRequired(){
double totalTime = timeRequired;
Iterator items = projectItems.iterator();
while(items.hasNext()){
ProjectItem item = (ProjectItem)items.next();
totalTime += item.getTimeRequired();
}
return totalTime;
}
public void setName(String newName){ name = newName; }
public void setOwner(Contact newOwner){ owner = newOwner; }
public void setTimeRequired(double newTimeRequired){ timeRequired = newTimeRequired; }
public void addProjectItem(ProjectItem element){
if (!projectItems.contains(element)){
projectItems.add(element);
}
}
public void removeProjectItem(ProjectItem element){
projectItems.remove(element);
}
public String toString(){
return "Task: " + name;
}
}
class SupportedProjectItem extends ProjectDecorator{
private ArrayList supportingDocuments = new ArrayList();
public SupportedProjectItem(){ }
public SupportedProjectItem(File newSupportingDocument){
addSupportingDocument(newSupportingDocument);
}
public ArrayList getSupportingDocuments(){
return supportingDocuments;
}
public void addSupportingDocument(File document){
if (!supportingDocuments.contains(document)){
supportingDocuments.add(document);
}
}
public void removeSupportingDocument(File document){
supportingDocuments.remove(document);
}
public String toString(){
return getProjectItem().toString() + EOL_STRING
+ "\tSupporting Documents: " + supportingDocuments;
}
}
|