vendredi 10 août 2018

Is there a design pattern for a class storing information needed by another class, for example for its construction?

I have classes that depends on various parameters, in particular in their constructor.

class Foo {
  Foo(int foo, double bar);
  // no default constructor
};

When Foo is used from another class, I sometimes need to store intermediate values needed by Foo. This happens when Foo depends on one or several values that are not readily available when the object using Foo is created.

I can store those values in the client class, but when this class uses several objects like Foo I end up having many parameters that relates to Foo-like objects. Therefore I usually end-up creating a class that holds these parameters and can create an object when all necessary parameters are eventually collected.

My question is, is there already a named pattern for this, and what is this name? I don't think Factory is the correct name because the intent of a Factory is different and closely related to inheritance. Or, is this approach wrong, and what would be a superior solution?

Aucun commentaire:

Enregistrer un commentaire