Policy Based Class Design

PolicyBasedClassDesign is the name of the first chapter in ModernCeePlusPlusDesign

This is a technique of implementing design choices in CeePlusPlus. The design choices are said to be orthogonal if they can be implemented independently. If each policy is given a name, that name can define a minimal interface, and there can be several implementations which meet the needs of the policy. One of these can be chosen at compile time to provide alternative implementations.

One example I have used goes as follows. I want to make use of pointers to objects within a C++ class. I decide that the interface for this will be

  Pointer_Class::Pointer_Type<T>

An implementation of this using BoostSharedPtr could be

  template <class B>
  class Boost_Pointer_Class {
public:
typedef boost::shared_ptr<B> Pointer_Type;
  };

In the following, class Foo uses Boost_Pointer_Class as a default option.

  template <class T, template <class> class Pointer_Class = Boost_Pointer_Class>
  class Foo
  {
typedef typename Pointer_Class<T>::Pointer_Type ptr_t;
  };

Class Foo can be declared without reference to this policy as long as the default will do:

  Foo<int> foo_int;

One project which makes use of this is MetaAgent which is to be found at http://codeproject.com/useritems/metaagent.asp#__top

By way of contrast, TheBoostGraphLibrary describes SetsOfRequirements which are ways of testing what a template type must do if it is to be put into a given class.

JohnFletcher


CategoryCpp CategoryCppTemplates CategoryMetaprogramming


EditText of this page (last edited March 4, 2006) or FindPage with title or text search