title: Optional Member Objects
body: <p>Okay, so you have a load of methods sprinkled around your system's main class.  So you do the right thing and refactor by creating a new class and perform move method(s) into a new class.  The new class has a single responsibility and all is right with the world again:</p>
  
  <pre><code>class Feature
  {
  public:
      Feature(){};
  
      void doSomething();
      void doSomething1();
      void doSomething2();   
  };
  </code></pre>
  
  <p>So now your original class has a member variable of type object:</p>
  
  <pre><code>  Feature _feature;
  </code></pre>
  
  <p>Which you will call in the main class.  Now if you do this many times, you will have many member-objects in your main class.</p>
  
  <p>Now these features may or not be required based on configuration so in a way it's costly having all these objects that may or not be needed.</p>
  
  <p>Can anyone suggest a way of improving this?</p>
  
  <hr>
  
  <p>EDIT: Based on suggestion to use The Null Object Design Pattern I've come up with this:</p>
  
  <p>An Abstract Class Defining the Interface of the Feature:</p>
  
  <pre><code>class IFeature
  {
  public:
      virtual void doSomething()=0;
      virtual void doSomething1()=0;
      virtual void doSomething2()=0;
  
      virtual ~IFeature(){}
  };
  </code></pre>
  
  <p>I then define two classes which implement the interface, one real implementation and one Null Object:</p>
  
  <pre><code>class RealFeature:public IFeature
  {
  public:
      RealFeature(){};
  
      void doSomething(){std::cout&lt;&lt;"RealFeature doSomething()"&lt;&lt;std::endl;}
      void doSomething1(){std::cout&lt;&lt;"RealFeature doSomething()"&lt;&lt;std::endl;}
      void doSomething2(){std::cout&lt;&lt;"RealFeature doSomething()"&lt;&lt;std::endl;}
  }; 
  </code></pre>
  
  <hr>
  
  <pre><code>class NullFeature:public IFeature
  {
  public:
      NullFeature(){};
  
      void doSomething(){std::cout&lt;&lt;"NULL doSomething()"&lt;&lt;std::endl;};
      void doSomething1(){std::cout&lt;&lt;"NULL doSomething1()"&lt;&lt;std::endl;};
      void doSomething2(){std::cout&lt;&lt;"NULL doSomething2()"&lt;&lt;std::endl;};
  
  
  };
  </code></pre>
  
  <p>I then define a Proxy class which will delegate to either the real object or the null object depending on configuration:</p>
  
  <pre><code>class Feature:public IFeature
  {
  public:
      Feature();
      ~Feature();
  
      void doSomething();
      void doSomething1();
      void doSomething2();
  
  private:
      std::auto_ptr&lt;IFeature&gt; _feature;
  };
  </code></pre>
  
  <p>Implementation:</p>
  
  <pre><code>   Feature::Feature()
      {
          std::cout&lt;&lt;"Feature() CTOR"&lt;&lt;std::endl;
          if(configuration::isEnabled() )
          {
              _feature = auto_ptr&lt;IFeature&gt;( new RealFeature() );
          }
          else
          {
              _feature = auto_ptr&lt;IFeature&gt;( new NullFeature() );
          }
      }
  
  
      void Feature::doSomething()
      {
          _feature-&gt;doSomething();
      }
  
      //And so one for each of the implementation methods
  </code></pre>
  
  <p>I then use the proxy class in my main class (or wherever it's required):</p>
  
  <pre><code>Feature _feature;
  
  _feature.doSomething();
  </code></pre>