Decorator Pattern 1 : Decorator Pattern « Design Pattern « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Design Pattern » Decorator PatternScreenshots 
Decorator Pattern 1

//[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;
    }
}

           
       
Related examples in the same category
1. Decorator pattern in JavaDecorator pattern in Java
2. Decorator Design Pattern in Java
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.