SlideShare a Scribd company logo
Composite Pattern



            by

Somenath Mukhopadhyay
   som@som-itsolutions.com

    Structural pattern

 Compose objects into a tree structure to
represent part-whole relationship

 An abstract class represents both primitives and
containers

 The client handles both the primitives and their
containers in the same fashion
Tree structure of the Composite Pattern
Applicability




    To represent part-whole hierarchy

 To make the client ignorant about whether it is
interacting with an individual object or a composite
object
Class Diagram
Example – base class Shape
class Shape
{
public:
              Shape(){}
              virtual void Add(unsigned int id)
              {
                            throw LeafClassTypeException();
              };
              virtual void Remove(unsigned int id){};

              //leaf classes will not override it..however, it will be overridden by the composite class.
              virtual Shape* GetChild(unsigned int id)
              {
                            throw LeafClassTypeException();
              };

              //Using this reference the "Chain of Responsibility" can be implemented
              virtual Shape* GetParentOfComponent()
              {
                            return ParentOfComponent;
              };

              virtual void SetParentOfComponent(Shape* s)
              {
                            ParentOfComponent = s;
              }

              virtual void Display(){};


              virtual Shape* FindItem(unsigned int id); //implementation afterwards

              virtual ~Shape(){};

protected:
              Shape* ParentOfComponent;
              unsigned int resource_id;
};
Iterator and the FindItem function
typedef map <unsigned int, Shape*, less<unsigned int> > theMap;

theMap Resource_Map;
theMap::iterator theIterator;

Shape* Shape::FindItem(unsigned int id)
{
     theIterator = Resource_Map.begin();
            while (theIterator != Resource_Map.end())
            {
            theIterator = Resource_Map.find(id);
            Shape* s = (*theIterator).second;
            theIterator++;
            return s;
            }

             return NULL;
}
A leaf class - Point
class Point : public Shape
{
public:
              Point():x_Coord(0),y_Coord(0){}
              Point(int x, int y):x_Coord(x), y_Coord(y){}
              Point(const Point& p)
              {
                             x_Coord = p.x_Coord;
                             y_Coord = p.y_Coord;
              }
              Point& operator = (const Point& p)
              {
                             x_Coord = p.x_Coord;
                             y_Coord = p.y_Coord;

                            return *this;
             }

             virtual void Display()
             {
                           cout<<"X Coordinate is:"<<x_Coord<<endl;
                           cout<<"Y Coordinate is:"<<y_Coord<<endl;
             }

             int X_COORD()
             {
                       return x_Coord;
             }

             int Y_COORD()
             {
                       return y_Coord;
             }

             virtual ~Point(){}
private:
             int x_Coord;
             int y_Coord;
};
A leaf class – line – a final class as well
//class Line is working as a leaf class.. Lets implement it as a final class
class Line : public Shape
{
private:
             //private constructor
             Line(unsigned int id):begin(0,0),end(0,0)
             {
                          resource_id = id;
                          Resource_Map.insert(theMap::value_type(resource_id,(Shape*)this));
             }
             //private constructor
             Line(unsigned int id, Point a, Point b):begin(a),end(b)
             {
                          resource_id = id;
                          Resource_Map.insert(theMap::value_type(resource_id,(Shape*)this));
             }
             //private copy constructor
             Line(const Line& in){ }
             //private assignment operator
             Line& operator=(const Line& in){ }
public:
             virtual void Display()
             {
                          cout<<"Begining point is:";
                                     begin.Display();
                          cout<<"End Point is:";
                                     end.Display();
             }
             static Line* CreateLine(unsigned int id, Point a, Point b)
             {
                          return new Line(id,a,b);
             }

           virtual ~Line(){}
private:
           Point begin;
           Point end;
};
A leaf class – rectangle – a final class as well
class Rectangle : public Shape
{
private:
            //private constructor
            Rectangle(unsigned int id, Point& p, int width, int height)
            {
                          top_left = p;
                          top_right = Point(p.X_COORD() + width, p.Y_COORD());
                          bottom_left = Point(p.X_COORD() , p.Y_COORD() + height);
                          bottom_right = Point(p.X_COORD() + width, p.Y_COORD() + height);
                          resource_id = id;
                          Resource_Map.insert(theMap::value_type(resource_id,(Shape*)this));
            }
            //private copy constructor
            Rectangle(const Rectangle& in){ }
                          //private assignment operator
            Rectangle& operator=(const Rectangle& in) { }
public:
            static Rectangle* CreateRectange(unsigned int id, Point& p, int width, int height)
            {
                          return new Rectangle(id, p, width, height);

            }
            virtual ~Rectangle(){}
            virtual void Display()
            {
                          cout<<"The four vertices are:"<<endl;
                          cout<<"Top Left :" ;
                                      top_left.Display();
                          cout <<"Top Right :";
                                      top_right.Display();
                          cout<<"Bottom Left :";
                                      bottom_left.Display();
                          cout<<"Bottom Right :";
                                      bottom_right.Display();

            }

            //Attributes
private:
            Point top_left;
            Point top_right;
            Point bottom_left;
            Point bottom_right;
};
A composite class - picture
class Picture : public Shape
{
public:
           Picture(unsigned int id)
           {
                       resource_id = id;
                       Resource_Map.insert(theMap::value_type(resource_id,(Shape*)this));
           }
           virtual void Display()
           {
                       vector<Shape*>::iterator p = Components.begin();
                       while (p != Components.end())
                       {
                                   (*p)->Display();
                                   p++;
                       }
           }

          //Adds the component with the resource id equal to the passed parameter
          virtual void Add (unsigned int id)
          {
                      Shape* s = FindItem(id);

                    Components.push_back(s);

                    s->SetParentOfComponent(this);

          }
Class Picture ... contd...
//removes the component from the list with the resource_id equal
to the parameter passed
      virtual void Remove(unsigned int id)
      {
            Shape* s = FindItem(id);
            vector<Shape*>::iterator p = Components.begin();
            int pos = 0;
            while (p != Components.end())
            {
                  if(Components.at(pos) == s)
                        break;
                  pos++;
                  p++;
            }
            Components.erase(p);
            s->SetParentOfComponent(NULL);
      }
Class Picture ... contd...
//will return the chile having the id equal to the passed value.
           virtual Shape* GetChild (unsigned int id)
           {
                     return FindItem(id);
           }


           virtual ~Picture()
           {
                     vector<Shape*>::iterator p = Components.begin();

                   int pos = 0;
                   while (p != Components.end())
                   {
                             delete(Components.at(pos));
                             p++;
                             pos++;
                   }


                   Components.clear();
           }
private:
           vector<Shape*> Components;
};
void main()
                                         The client - main()
{
              Point p1(10,20);
              Point p2(30,40);
              Point p3(50,60);
              Point p4(70,80);
              Point p5(100,110);
              Point p6(150,200);
              Line* l1 = Line::CreateLine(ID_LINE1,p1,p2);
              try
              {
              l1->Add(0);
              }
              catch(LeafClassTypeException& e)
              {
                            e.printerrormsg();
              }
              Line* l2 = Line::CreateLine(ID_LINE2,p3,p4);
              Line* l3 = Line::CreateLine(ID_LINE3,p5,p6);

              Rectangle* r1 = Rectangle::CreateRectange(ID_RECTANGLE1, p1, 50,25);


              Shape* p = new Picture(ID_PICTURE);
              p->Add(ID_LINE1);
              p->Add(ID_LINE2);
              p->Add(ID_LINE3);
              p->Add(ID_RECTANGLE1);

              (p->GetChild(ID_RECTANGLE1))->Display();

              p->Remove(ID_RECTANGLE1);

              p->Display();

              cout<<p<<endl;

              cout<<l1->GetParentOfComponent()<<endl;

              delete p;

}
The helper exception class


class LeafClassTypeException
{
public:
     void printerrormsg()
     {
          cout<<"This is a leaf class"<<endl;
     }
}
;
Implementation Details


 Every component is identifiable through its
resource id

 Whenever we create an object (leaf or composite
object), it creates a key pair of the id and the
pointer to that object and pushes this key into a
MAP, from which we can easily search for that
component in later times through its resource id
Implementation details




 The leaf classes, namely Line and Rectangle
have been implemented as final classes by
making their constructors, copy constructors and
assignment operators private and providing static
member functions to create them.
Issues


 GetParentofComponent can be used to traverse
the tree hierarchy

 We have to make sure that any child can have a
composite object as its parent

    No child can have another child if its a leaf

 Whenever a leaf class tries to add a child it
throws the LeafClassException
Issues



 Add and Remove functions have been defined in
the Root class.

    For the leaf classes these just throw exceptions

 However it helps the client to treat leaf and
composite objects uniformly

More Related Content

PPT
C++: Constructor, Copy Constructor and Assignment operator
PPTX
PPTX
Functional DDD
KEY
Objective-Cひとめぐり
PDF
Hidden Gems in Swift
DOC
Oops lab manual2
PDF
Uncommon Design Patterns
KEY
Objective-C Crash Course for Web Developers
C++: Constructor, Copy Constructor and Assignment operator
Functional DDD
Objective-Cひとめぐり
Hidden Gems in Swift
Oops lab manual2
Uncommon Design Patterns
Objective-C Crash Course for Web Developers

What's hot (20)

PPT
Pointers
PPTX
OO in JavaScript
PPT
Lec 45.46- virtual.functions
PDF
OpenGL ES 3 Reference Card
PDF
Evolutionary Problems In Aspect Oriented Software Development
PPTX
Pre zen ta sion
PDF
The STL
PDF
OpenXR 1.0 Reference Guide
PPTX
Max Koretskyi "Why are Angular and React so fast?"
PDF
OpenGL ES 3.1 Reference Card
PDF
glTF 2.0 Reference Guide
PDF
OpenGL 4.4 Reference Card
PPT
Supstat nyc subway
DOC
Practical Class 12th (c++programs+sql queries and output)
KEY
Parte II Objective C
PDF
Type level programming in Scala
PPT
Advance features of C++
PPT
Collection v3
PPT
CodeMash - Building Rich Apps with Groovy SwingBuilder
PDF
ECMA 入门
Pointers
OO in JavaScript
Lec 45.46- virtual.functions
OpenGL ES 3 Reference Card
Evolutionary Problems In Aspect Oriented Software Development
Pre zen ta sion
The STL
OpenXR 1.0 Reference Guide
Max Koretskyi "Why are Angular and React so fast?"
OpenGL ES 3.1 Reference Card
glTF 2.0 Reference Guide
OpenGL 4.4 Reference Card
Supstat nyc subway
Practical Class 12th (c++programs+sql queries and output)
Parte II Objective C
Type level programming in Scala
Advance features of C++
Collection v3
CodeMash - Building Rich Apps with Groovy SwingBuilder
ECMA 入门
Ad

Viewers also liked (7)

PDF
Decorator design pattern (A Gift Wrapper)
PPT
Composite pattern
PDF
The Decorator Pattern
PPTX
Design Patterns - 01 Introduction and Decorator Pattern
PPT
Composite Design Pattern
PPTX
Structural Design pattern - Adapter
ZIP
Adapter Design Pattern
Decorator design pattern (A Gift Wrapper)
Composite pattern
The Decorator Pattern
Design Patterns - 01 Introduction and Decorator Pattern
Composite Design Pattern
Structural Design pattern - Adapter
Adapter Design Pattern
Ad

Similar to Composite Pattern (20)

PDF
C++ L11-Polymorphism
PPT
oop objects_classes
PPTX
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
DOCX
Opp compile
PPTX
Presentations_PPT_ Unit-6_OOP.pptx
PPT
OBJECTS IN Object Oriented Programming .ppt
PDF
Can you please debug this Thank you in advance! This program is sup.pdf
PDF
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
PDF
Hi, Please find mu codeimport java.util.Random;public class Sup.pdf
PDF
ZIP
PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
PDF
C++ programs
PDF
This C++ program keeps giving me the wrong median. #include ios.pdf
PPT
Cquestions
PDF
polymorphism in c++ with Full Explanation.
PDF
TypeScript Introduction
DOCX
namespace ConsoleApplication15 { class Program { static void Main(stri.docx
PDF
Bindings: the zen of montage
PDF
C++ normal assignments by maharshi_jd.pdf
C++ L11-Polymorphism
oop objects_classes
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Opp compile
Presentations_PPT_ Unit-6_OOP.pptx
OBJECTS IN Object Oriented Programming .ppt
Can you please debug this Thank you in advance! This program is sup.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Hi, Please find mu codeimport java.util.Random;public class Sup.pdf
Object Oriented Programming (OOP) using C++ - Lecture 3
C++ programs
This C++ program keeps giving me the wrong median. #include ios.pdf
Cquestions
polymorphism in c++ with Full Explanation.
TypeScript Introduction
namespace ConsoleApplication15 { class Program { static void Main(stri.docx
Bindings: the zen of montage
C++ normal assignments by maharshi_jd.pdf

More from Somenath Mukhopadhyay (20)

PDF
Significance of private inheritance in C++...
PDF
Arranging the words of a text lexicographically trie
PDF
Generic asynchronous HTTP utility for android
PDF
Copy on write
PDF
Java concurrency model - The Future Task
PDF
Memory layout in C++ vis a-vis polymorphism and padding bits
PDF
Developing an Android REST client to determine POI using asynctask and integr...
PDF
Observer pattern
PDF
Uml training
PDF
How to create your own background for google docs
PDF
The Designing of a Software System from scratch with the help of OOAD & UML -...
PDF
Structural Relationship between Content Resolver and Content Provider of Andr...
PDF
Flow of events during Media Player creation in Android
PDF
Implementation of a state machine for a longrunning background task in androi...
PDF
Tackling circular dependency in Java
PDF
Implementation of composite design pattern in android view and widgets
PDF
Exception Handling in the C++ Constructor
PDF
Active object of Symbian in the lights of client server architecture
PDF
Android services internals
PDF
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
Significance of private inheritance in C++...
Arranging the words of a text lexicographically trie
Generic asynchronous HTTP utility for android
Copy on write
Java concurrency model - The Future Task
Memory layout in C++ vis a-vis polymorphism and padding bits
Developing an Android REST client to determine POI using asynctask and integr...
Observer pattern
Uml training
How to create your own background for google docs
The Designing of a Software System from scratch with the help of OOAD & UML -...
Structural Relationship between Content Resolver and Content Provider of Andr...
Flow of events during Media Player creation in Android
Implementation of a state machine for a longrunning background task in androi...
Tackling circular dependency in Java
Implementation of composite design pattern in android view and widgets
Exception Handling in the C++ Constructor
Active object of Symbian in the lights of client server architecture
Android services internals
Android Asynctask Internals vis-a-vis half-sync half-async design pattern

Recently uploaded (20)

PPTX
A Presentation on Artificial Intelligence
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PPTX
1. Introduction to Computer Programming.pptx
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
August Patch Tuesday
PPTX
Chapter 5: Probability Theory and Statistics
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Hybrid model detection and classification of lung cancer
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
project resource management chapter-09.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
Tartificialntelligence_presentation.pptx
PDF
Heart disease approach using modified random forest and particle swarm optimi...
A Presentation on Artificial Intelligence
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
1. Introduction to Computer Programming.pptx
cloud_computing_Infrastucture_as_cloud_p
August Patch Tuesday
Chapter 5: Probability Theory and Statistics
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Hybrid model detection and classification of lung cancer
Zenith AI: Advanced Artificial Intelligence
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Assigned Numbers - 2025 - Bluetooth® Document
project resource management chapter-09.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Web App vs Mobile App What Should You Build First.pdf
Tartificialntelligence_presentation.pptx
Heart disease approach using modified random forest and particle swarm optimi...

Composite Pattern

  • 1. Composite Pattern by Somenath Mukhopadhyay som@som-itsolutions.com
  • 2. Structural pattern  Compose objects into a tree structure to represent part-whole relationship
  • 3.  An abstract class represents both primitives and containers  The client handles both the primitives and their containers in the same fashion
  • 4. Tree structure of the Composite Pattern
  • 5. Applicability  To represent part-whole hierarchy  To make the client ignorant about whether it is interacting with an individual object or a composite object
  • 7. Example – base class Shape class Shape { public: Shape(){} virtual void Add(unsigned int id) { throw LeafClassTypeException(); }; virtual void Remove(unsigned int id){}; //leaf classes will not override it..however, it will be overridden by the composite class. virtual Shape* GetChild(unsigned int id) { throw LeafClassTypeException(); }; //Using this reference the "Chain of Responsibility" can be implemented virtual Shape* GetParentOfComponent() { return ParentOfComponent; }; virtual void SetParentOfComponent(Shape* s) { ParentOfComponent = s; } virtual void Display(){}; virtual Shape* FindItem(unsigned int id); //implementation afterwards virtual ~Shape(){}; protected: Shape* ParentOfComponent; unsigned int resource_id; };
  • 8. Iterator and the FindItem function typedef map <unsigned int, Shape*, less<unsigned int> > theMap; theMap Resource_Map; theMap::iterator theIterator; Shape* Shape::FindItem(unsigned int id) { theIterator = Resource_Map.begin(); while (theIterator != Resource_Map.end()) { theIterator = Resource_Map.find(id); Shape* s = (*theIterator).second; theIterator++; return s; } return NULL; }
  • 9. A leaf class - Point class Point : public Shape { public: Point():x_Coord(0),y_Coord(0){} Point(int x, int y):x_Coord(x), y_Coord(y){} Point(const Point& p) { x_Coord = p.x_Coord; y_Coord = p.y_Coord; } Point& operator = (const Point& p) { x_Coord = p.x_Coord; y_Coord = p.y_Coord; return *this; } virtual void Display() { cout<<"X Coordinate is:"<<x_Coord<<endl; cout<<"Y Coordinate is:"<<y_Coord<<endl; } int X_COORD() { return x_Coord; } int Y_COORD() { return y_Coord; } virtual ~Point(){} private: int x_Coord; int y_Coord; };
  • 10. A leaf class – line – a final class as well //class Line is working as a leaf class.. Lets implement it as a final class class Line : public Shape { private: //private constructor Line(unsigned int id):begin(0,0),end(0,0) { resource_id = id; Resource_Map.insert(theMap::value_type(resource_id,(Shape*)this)); } //private constructor Line(unsigned int id, Point a, Point b):begin(a),end(b) { resource_id = id; Resource_Map.insert(theMap::value_type(resource_id,(Shape*)this)); } //private copy constructor Line(const Line& in){ } //private assignment operator Line& operator=(const Line& in){ } public: virtual void Display() { cout<<"Begining point is:"; begin.Display(); cout<<"End Point is:"; end.Display(); } static Line* CreateLine(unsigned int id, Point a, Point b) { return new Line(id,a,b); } virtual ~Line(){} private: Point begin; Point end; };
  • 11. A leaf class – rectangle – a final class as well class Rectangle : public Shape { private: //private constructor Rectangle(unsigned int id, Point& p, int width, int height) { top_left = p; top_right = Point(p.X_COORD() + width, p.Y_COORD()); bottom_left = Point(p.X_COORD() , p.Y_COORD() + height); bottom_right = Point(p.X_COORD() + width, p.Y_COORD() + height); resource_id = id; Resource_Map.insert(theMap::value_type(resource_id,(Shape*)this)); } //private copy constructor Rectangle(const Rectangle& in){ } //private assignment operator Rectangle& operator=(const Rectangle& in) { } public: static Rectangle* CreateRectange(unsigned int id, Point& p, int width, int height) { return new Rectangle(id, p, width, height); } virtual ~Rectangle(){} virtual void Display() { cout<<"The four vertices are:"<<endl; cout<<"Top Left :" ; top_left.Display(); cout <<"Top Right :"; top_right.Display(); cout<<"Bottom Left :"; bottom_left.Display(); cout<<"Bottom Right :"; bottom_right.Display(); } //Attributes private: Point top_left; Point top_right; Point bottom_left; Point bottom_right; };
  • 12. A composite class - picture class Picture : public Shape { public: Picture(unsigned int id) { resource_id = id; Resource_Map.insert(theMap::value_type(resource_id,(Shape*)this)); } virtual void Display() { vector<Shape*>::iterator p = Components.begin(); while (p != Components.end()) { (*p)->Display(); p++; } } //Adds the component with the resource id equal to the passed parameter virtual void Add (unsigned int id) { Shape* s = FindItem(id); Components.push_back(s); s->SetParentOfComponent(this); }
  • 13. Class Picture ... contd... //removes the component from the list with the resource_id equal to the parameter passed virtual void Remove(unsigned int id) { Shape* s = FindItem(id); vector<Shape*>::iterator p = Components.begin(); int pos = 0; while (p != Components.end()) { if(Components.at(pos) == s) break; pos++; p++; } Components.erase(p); s->SetParentOfComponent(NULL); }
  • 14. Class Picture ... contd... //will return the chile having the id equal to the passed value. virtual Shape* GetChild (unsigned int id) { return FindItem(id); } virtual ~Picture() { vector<Shape*>::iterator p = Components.begin(); int pos = 0; while (p != Components.end()) { delete(Components.at(pos)); p++; pos++; } Components.clear(); } private: vector<Shape*> Components; };
  • 15. void main() The client - main() { Point p1(10,20); Point p2(30,40); Point p3(50,60); Point p4(70,80); Point p5(100,110); Point p6(150,200); Line* l1 = Line::CreateLine(ID_LINE1,p1,p2); try { l1->Add(0); } catch(LeafClassTypeException& e) { e.printerrormsg(); } Line* l2 = Line::CreateLine(ID_LINE2,p3,p4); Line* l3 = Line::CreateLine(ID_LINE3,p5,p6); Rectangle* r1 = Rectangle::CreateRectange(ID_RECTANGLE1, p1, 50,25); Shape* p = new Picture(ID_PICTURE); p->Add(ID_LINE1); p->Add(ID_LINE2); p->Add(ID_LINE3); p->Add(ID_RECTANGLE1); (p->GetChild(ID_RECTANGLE1))->Display(); p->Remove(ID_RECTANGLE1); p->Display(); cout<<p<<endl; cout<<l1->GetParentOfComponent()<<endl; delete p; }
  • 16. The helper exception class class LeafClassTypeException { public: void printerrormsg() { cout<<"This is a leaf class"<<endl; } } ;
  • 17. Implementation Details  Every component is identifiable through its resource id  Whenever we create an object (leaf or composite object), it creates a key pair of the id and the pointer to that object and pushes this key into a MAP, from which we can easily search for that component in later times through its resource id
  • 18. Implementation details  The leaf classes, namely Line and Rectangle have been implemented as final classes by making their constructors, copy constructors and assignment operators private and providing static member functions to create them.
  • 19. Issues  GetParentofComponent can be used to traverse the tree hierarchy  We have to make sure that any child can have a composite object as its parent  No child can have another child if its a leaf  Whenever a leaf class tries to add a child it throws the LeafClassException
  • 20. Issues  Add and Remove functions have been defined in the Root class.  For the leaf classes these just throw exceptions  However it helps the client to treat leaf and composite objects uniformly