Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Tagged as

Building Decorator Chains

, 22 Oct 2010
The article explains a method, how flexible and extendible decorator chains can be built in a generic way. Its power is best seen if it is used with boost::factory and boost::bind.
decorator_chain.zip
decorator_chain
#include <iostream>
#include <string>

#include <boost/functional/factory.hpp>
#include <boost/bind.hpp>
#include <boost/bind/placeholders.hpp>

#include "decorator_chain.hpp"



using namespace std;

class abstract_class
{
public:
    virtual ~abstract_class( void ) { }
    virtual void do_something( void ) = 0;
};

class concrete_class : public abstract_class
{
public:
    void do_something( void ) { cout << "concrete_class_a\n"; }
};

class decorator : public abstract_class
{
    abstract_class *m_decorated_class;
public:
    decorator( abstract_class *decorated_class ) : m_decorated_class( decorated_class ) { }
    void do_something( void )
    {
        m_decorated_class->do_something();
        cout << "decorator\n";
    }
};

class int_decorator : public abstract_class
{
    abstract_class *m_decorated_class;
    int m_val;
public:
    int_decorator( abstract_class *decorated_class , int val ) : m_decorated_class( decorated_class ) , m_val( val ) { }
    void do_something( void )
    {
        m_decorated_class->do_something();
        cout << "int_decorator : " << m_val << "\n";
    }
};

class string_decorator : public abstract_class
{
    abstract_class *m_decorated_class;
    string &m_val;
public:
    string_decorator( abstract_class *decorated_class , string &val ) : m_decorated_class( decorated_class ) , m_val( val ) { }
    void do_something( void )
    {
        m_decorated_class->do_something();
        cout << "string_decorator " << m_val << "\n";
    }
};

int main( int argc , char **argv )
{
    concrete_class c;
    decorator d( &c );
    d.do_something();

    decorator_chain< abstract_class > dc;
    dc.create_class( boost::factory< concrete_class* >() );
    dc.decorate( boost::bind( boost::factory< decorator* >() , _1 ) );
    dc.decorate( boost::bind( boost::factory< int_decorator* >() , _1 , 10 ) );
    string str = "Hello world!";
    dc.decorate( boost::bind( boost::factory< string_decorator* >() , _1 , boost::ref( str ) ) );
    dc.get_class_reference().do_something();

    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

headmyshoulder

Germany Germany
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 22 Oct 2010
Article Copyright 2010 by headmyshoulder
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid