Skip to content

Instantly share code, notes, and snippets.

@geekskick geekskick/composite.cpp
Last active Feb 11, 2018

Embed
What would you like to do?
Composite Pattern - Runnable
#include <iostream>
#include <deque>
void x(void){
std::cout << "Doing x" << std::endl;
}
void y(void){
std::cout << "Doing y" << std::endl;
}
class test_t{
public:
virtual void test(void) = 0;
virtual ~test_t(void){}
};
class loop_test_t: public test_t{
public:
virtual ~loop_test_t(void){ while(!m_loop.empty()){ test_t* t = m_loop.front(); delete t; m_loop.pop_front(); }}
void test(void){
while(!m_loop.empty()){
test_t* t = m_loop.front();
t->test();
delete t;
m_loop.pop_front();
}
}
protected:
virtual void populate_loop(void) = 0;
std::deque<test_t*> m_loop;
};
class pin_test : public test_t{
public:
pin_test(const int pin): m_pin(pin){}
void test(void){
x();
y();
std::cout << "Testing Pin " << m_pin << std::endl;
x();
y();
}
private:
const int m_pin;
};
class example_loop: public loop_test_t{
public:
example_loop(void){populate_loop();}
protected:
void populate_loop(void){
m_loop.push_back(new pin_test(1));
m_loop.push_back(new pin_test(2));
m_loop.push_back(new pin_test(3));
}
};
class example_test: public loop_test_t{
public:
example_test(void){populate_loop();}
protected:
void populate_loop(void){
m_loop.push_back(new pin_test(99));
m_loop.push_back(new example_loop());
m_loop.push_back(new pin_test(99));
}
};
int main(int argc, char const *argv[])
{
example_test().test();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.