Search code examples
design-patternsidentification

Is this construction a design pattern?


Sometimes I initialize my classes by passing to it a POD (Plain Old Data). This reduces the number of parameters I have to pass to the function (whether it is the constructor or an init function), it allows me to not care about the order in which they are declared, and to change the number and nature of parameters without having to change method signatures.

I find it handy.

Here is a small example:

class Manager
{
public:

  struct Configuration
    : mDataVectorSize( 20 )
    , mShouldOutputDebug( false )
  {
    int mDataVectorSize;
    bool mShouldOutputDebug;
  };

  Manager(const Configuration& aConfiguration);

  void serviceA();
  void serviceB();

private:
  Configuration mConfiguration;
  std::vector<int> mData;

};

With the usage:

Manager::Configuration config;
config.mDataVectorSize = 30;

Manager manager( config );

manager.serviceA();

What is the name of this pattern, if it’s even a pattern? I thought that it was called Flyweight, but reading the description on Wikipedia, it doesn’t exactly match what’s in here.


Solution

  • This is called the parameter object pattern, where you encapsulate a bunch of method parameters into another object/structure. As you already mentioned in the question, one of the advantages is being able to modify the actual parameters without having to change the signature of the method. You can read more here: http://www.c2.com/cgi/wiki?ParameterObject