This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Composite Pattern and Decorator Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Reply Bookmark "Composite Pattern and Decorator Pattern" Watch "Composite Pattern and Decorator Pattern" New topic
Author

Composite Pattern and Decorator Pattern

krishna prasad gunasekaran
Ranch Hand

Joined: Jul 25, 2006
Posts: 158
Hi,
they both look a bit similar in the way they carry out the job. Like traversing through the component they compose which happens to extend the same interface.
The difference being CP is more of a tree while DP is like a chain with new bahaviors added on the way. somehow they look similar in certain ways. Do they?
My doubt with CP is that how it works in case of Objects of different kind?


have a great day,
krishna prasad
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

No, there is difference

Use Composite pattern if
1. You need to keep either object or group of objects inside another object.
2. and If behaviour of groups and single object might be common.


Use Decorator if
1. You have variations of an operations at runtime
like :
FileWriter file = new FileWriter("test.txt");
BufferedWriter writer = new BufferedWriter(file); //we can compose one stream from another

 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Composite Pattern and Decorator Pattern
 
Similar Threads
application flow
Frames
Packaging, naming and directory structure.
EJB Deployment on WebSphere 4.0 AEs
how to find out JBoss AS 7 version?