0% found this document useful (0 votes)
3 views

4 Design Patterns Java Structural m4 Slides 4

The Composite Pattern allows for the creation of tree structures where individual objects and composites can be treated uniformly, enabling the same operations to be applied to both. It is exemplified by components like java.awt.Component and JSF widgets, but can lead to oversimplification and implementation challenges. The pattern is contrasted with the Decorator Pattern, which modifies behavior without altering the underlying object.

Uploaded by

podam91581
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

4 Design Patterns Java Structural m4 Slides 4

The Composite Pattern allows for the creation of tree structures where individual objects and composites can be treated uniformly, enabling the same operations to be applied to both. It is exemplified by components like java.awt.Component and JSF widgets, but can lead to oversimplification and implementation challenges. The pattern is contrasted with the Decorator Pattern, which modifies behavior without altering the underlying object.

Uploaded by

podam91581
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Composite Pattern

Bryan Hansen
twitter: bh5k | http://www.linkedin.com/in/hansenbryan
Concepts
▪ Components represent part or whole structure
▪ Compose objects into tree structures
▪ Individual object treated as a Composite
▪ Same operations applied on individual and
composites
▪ Examples:
▪ java.awt.Component
▪ JSF widgets
▪ RESTful service GETs
Design

Tree structured
Component
Leaf or Composite, same operations
Composite knows about child objects
Component, Leaf, Composite
UML
Everyday Example - Map

Map<String,  String>  personAttributes  =  new  HashMap<>();  


   
personAttributes.put("site_role",  “person");  
personAttributes.put("access_role",  "limited");  
     
Map<String,  String>  groupAttributes  =  new  HashMap<>();  
     
groupAttributes.put("group_role",  "claims");  
     
Map<String,  String>  secAttributes  =  new  HashMap<>();  
     
secAttributes.putAll(personAttributes);  
secAttributes.putAll(groupAttributes);
Exercise Composite

Menu, MenuItem, MenuComponent


Create Composite
Features Not Supported
Pitfalls
▪ Can overly simplify system
▪ Difficult to restrict
▪ Implementation can possibly be costly
Contrast

Composite Decorator
▪ Tree structure ▪ Contains another entity
▪ Leaf and Composite have same ▪ Modifies behavior (adds)
interface ▪ Doesn’t change underlying object
▪ Unity between objects
Composite Summary

• Generalizes a hierarchical structure


• Can simplify things too much
• Easier for clients
• Composite != Composition

You might also like