3

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.

Palec
  • 12,743
  • 8
  • 69
  • 138
Vaillancourt
  • 1,380
  • 1
  • 11
  • 42
  • Isn't this singleton creation and dependency injection? – MingShun Mar 20 '15 at 21:29
  • @MingShun Manager is based on a singleton but it's not one in the current context. As for the dependency injection, I would not think so as it's not delegating any behaviour to external components. – Vaillancourt Mar 20 '15 at 21:50
  • 1
    I was referring to config as the singleton creation. But I hadn't considered dependency injection as pushing a behavior into an object before. Until now, I've thought of it as a way to decouple a dependency object from the object by initializing it outside. Food for thought. – MingShun Mar 20 '15 at 22:02

1 Answers1

3

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

casablanca
  • 69,683
  • 7
  • 133
  • 150