• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Composite Pattern and Decorator Pattern

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

reply
    Bookmark Topic Watch Topic
  • New Topic