[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.4 Object factories

A common use case is to create lots of similarly configured objects. One can repeatedly call CreateObject but there is also a factory design pattern in use in the ns-3 system. It is heavily used in the "helper" API.

Class ObjectFactory can be used to instantiate objects and to configure the attributes on those objects

  void SetTypeId (TypeId tid);
  void Set (std::string name, const AttributeValue &value);
  Ptr<T> Create (void) const;

The first method allows one to use the ns-3 TypeId system to specify the type of objects created. The second allows one to set attributes on the objects to be created, and the third allows one to create the objects themselves.

For example:

  ObjectFactory factory;
  // Make this factory create objects of type FriisPropagationLossModel
  factory.SetTypeId ("ns3::FriisPropagationLossModel")
  // Make this factory object change a default value of an attribute, for
  // subsequently created objects
  factory.Set ("SystemLoss", DoubleValue (2.0));
  // Create one such object
  Ptr<Object> object = m_factory.Create (); 
  factory.Set ("SystemLoss", DoubleValue (3.0));
  // Create another object
  Ptr<Object> object = m_factory.Create (); 

[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated on August 20, 2010 using texi2html 1.82.