//
// main.cpp
// PlusApp
//
// Created by Mr Gan on 12/18/14.
// Copyright (c) 2014 Mr Gan. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
class Composite
{
public:
Composite(string name):m_strName(name){}
virtual ~Composite(){}
virtual void operation() = 0;
virtual void add(Composite *p){};
virtual void del(Composite *p){};
protected:
string m_strName;
};
class LeafComposite : public Composite
{
public:
LeafComposite(string name):Composite(name){}
virtual ~LeafComposite(){}
void operation();
};
void LeafComposite::operation()
{
cout << m_strName << endl;
}
class SubComposite : public Composite
{
public:
SubComposite(string name):Composite(name){}
virtual ~SubComposite(){}
void operation();
virtual void add(Composite *p);
virtual void del(Composite *p);
private:
vector<Composite*> m_vc;
};
void SubComposite::operation()
{
cout << m_strName << endl;
vector<Composite*>::iterator tem = m_vc.begin();
for (; tem != m_vc.end(); ++tem)
{
(*tem)->operation();
}
}
void SubComposite::add(Composite *p)
{
m_vc.push_back(p);
}
void SubComposite::del(Composite *p)
{
vector<Composite*>::iterator tem = m_vc.begin();
for (; tem != m_vc.end(); ++tem)
{
if (*tem == p)
{
m_vc.erase(tem);
}
}
}
int main(int argc, const char * argv[]) {
// insert code here...
LeafComposite *apple= new LeafComposite("apple");
LeafComposite *banana= new LeafComposite("banana");
SubComposite *frute = new SubComposite("frute");
frute->add(apple);
frute->add(banana);
LeafComposite *primer= new LeafComposite("primer");
SubComposite *book = new SubComposite("book");
book->add(primer);
LeafComposite *sumgSmartPhone= new LeafComposite("sumgSmartPhone");
LeafComposite *iphone= new LeafComposite("iphone");
LeafComposite *htc= new LeafComposite("htc");
SubComposite *smartPhone = new SubComposite("smartPhone");
smartPhone->add(sumgSmartPhone);
smartPhone->add(iphone);
smartPhone->add(htc);
SubComposite *object = new SubComposite("object");
object->add(frute);
object->add(book);
object->add(smartPhone);
object->operation();;
delete apple;
delete banana;
delete frute;
delete primer;
delete book;
delete sumgSmartPhone;
delete iphone;
delete htc;
delete smartPhone;
delete object;
std::cout << "Hello, World!\n";
return 0;
}