Listing 3: The prototype factory class using a cloning policy

template<typename Proto>
class CloneFromPointer
{
public:
  typedef Proto* cloning_result_type;
  static cloning_result_type Clone(Proto* p)
  { return p->Clone(); }
};

template
<
  typename Proto, 
  typename Id,
  typename CloningPolicy = CloneFromPointer<Proto>
>

class ProtoTypeFactory
{
public:
  void RegisterProtoType(Proto* pProtoType, Id id)
  { protoTypeCollection[id] = pProtoType; }
  
  CloningPolicy::cloning_result_type GetClone(Id id)
  { return CloningPolicy::Clone(protoTypeCollection[id]); }

private:
  std::map<Id, Proto*> protoTypeCollection;
};
— End of Listing —