Nevertheless, there are rare situations where pattern "chain of responsibility" is a reasonable choice. This may be cases in which the channel signal is always single, when the signal transmission should go with a minimum of overhead, when the channel is already running in a secure multi-threaded environment or connects composite parts of one whole component.
Front-end
For example, consider the schematic design of a system built on layers of processing. Target class should allow the dynamic creation of a one-way communication between an arbitrary number of objects and provide an interface for signal propagation.
chain_t;
public boost::enable_shared_from_this< layer >,
public boost::noncopyable
{
base_t;
std::string
name;
layer( std::string
{}
public:
layer_ptr_t;
{
std::cout << name << "::chain(" << arg << ")" << std::endl;
if ( !( base_t::empty< chain_t >() ) )
base_t::call< chain_t,
}
layer_ptr_t attach( layer_ptr_t l )
{
base_t::bind( boost::bind( &layer::chain, l, _1 ) );
return ( shared_from_this() );
}
static layer_ptr_t create( std::string
{ return layer_ptr_t( new layer( name ) ); }
};
{
layer::layer_ptr_t lA2 = layer::create( "A2" );
layer::layer_ptr_t lA1 = layer::create( "A1" )->attach( lA2->attach( layer::create( "A3" ) ) );
std::cout << std::endl << "A1 -> A2 -> A3 chain" << std::endl; lA1->chain( 0 );
layer::layer_ptr_t lB1 = layer::create( "B1" );
lB1->attach( lA2 );
std::cout << std::endl << "B1 -> A2 -> A3 chain" << std::endl; lB1->chain( 0 );
}
Output:
A1 -> A2 -> A3 chain
A1::chain(0)
A2::chain(1)
A3::chain(2)
B1 -> A2 -> A3 chain
B1::chain(0)
A2::chain(1)
A3::chain(2)
A1::chain(0)
A2::chain(1)
A3::chain(2)
B1 -> A2 -> A3 chain
B1::chain(0)
A2::chain(1)
A3::chain(2)
Sources
z3d::chain_of_resp.hpp
Use cases:
z3d::utests::chain_of_resp_ut01.hpp
z3d::utests::chain_of_resp_ut02.hpp
z3d::utests::chain_of_resp_ut03.hpp
Комментариев нет:
Отправить комментарий