C++ Composite
Composites are built up from one superclass
to enable uniform handeling.
This is the abstract component class.
This is the super class for all component
and composite classes.
-----------------------------------------------------
class AbstractComposite;
class AbstractComponent{
public:
AbstractComponent();
AbstractComposite* parent;
virtual int countBikers() = 0;
private:
AbstractComposite* getParent();
};
AbstractComponent::AbstractComponent() :
parent(0)
{}
AbstractComposite* AbstractComponent::getParent(){
return parent;
}
-----------------------------------------------------
This is the abstract composite class.
The client can treat this class just as it would a
component class.
When asked to perform component functions it delegates
to its children.
It caches or delegates to its parent contextual information
requests.
-----------------------------------------------------
class AbstractComposite : public AbstractComponent{
public:
AbstractComposite();
AbstractComponent* getBiker(int index);
void addChild(AbstractComponent* child);
void removeChild(AbstractComponent* child);
void changeNotification();
int countBikers();
private:
std::vector children;
int bikerCount;
};
AbstractComposite::AbstractComposite() :
bikerCount(-1)
{}
AbstractComponent* AbstractComposite::getBiker(int index){
return children.at(index);
}
void AbstractComposite::addChild(AbstractComponent* child){
children.push_back(child);
}
void AbstractComposite::removeChild(AbstractComponent* child){
if(child->parent == this){
child->parent = 0;
// children.removeElement(child);
changeNotification();
}
}
void AbstractComposite::changeNotification(){
bikerCount = -1;
if(parent != 0)
parent->changeNotification();
}
int AbstractComposite::countBikers(){
if(bikerCount!=-1)
return bikerCount;
int count = 0;
for(int i =0; icountBikers();
}
bikerCount = count;
return bikerCount;
}
-----------------------------------------------------
Concrete component class.
-----------------------------------------------------
class Biker : public AbstractComponent{
public:
int countBikers();
};
int Biker::countBikers(){
return 1;
}
-----------------------------------------------------
Concrete composite class.
-----------------------------------------------------
class BikerGang : public AbstractComposite{};
-----------------------------------------------------
Concrete composite class.
-----------------------------------------------------
class BikerRally : public AbstractComposite{};
-----------------------------------------------------
Test stub to run it all.
-----------------------------------------------------
int main(){
std::cout<<"main: START\n";
AbstractComposite* bikerRally = new BikerRally();
for(int i =0; i<20; i++){
AbstractComponent* child = new Biker();
bikerRally->addChild(child);
}
for(int i =0; i<10; i++){
BikerGang* bikerGang = new BikerGang();
for(int j =0; j<5; j++){
AbstractComponent* child = new Biker();
bikerGang->addChild(child);
}
bikerRally->addChild(bikerGang);
}
std::cout<<bikerRally->countBikers()<<std::endl;
std::cout<<"main: END\n";
return 0 ;
}
-----------------------------------------------------
Here is a tar with the above classes in it.
C++_composite.tar