1

Let's take the following Javascript, but the language is not really relevant:

module.exports = (user) => {
    return {
      createPrimaryConfig: () => {
        return new Config('a', 'b', user.getName());
      },

     createAlternativeConfig: () => {
      return new Config('a', 'b', user.getName());
     }  
    }
 }

The idea being here that different configurations for a given object can be created (the job of a factory traditionally?) and in most cases, systems will reuse some of these but we want the business logic of deciding how the configuration is built (from the user object) hidden away. If the buisness logic was more complicated, it would be more worthwhile but this simple is contrived and exaggerated a bit.

Is this still a factory method? If not, what would you call it? Does a factory method have to strictly have one method that returns an output? What is it you let the caller select the type it wants?

  • 2
    I think you might be overthinking this a bit. What makes a factory method a factory is the fact that it performs a factory operation. The name matters less than the concept. – Robert Harvey Apr 30 '18 at 5:05
  • I might be dense @RobertHarvey but could you expand? Wikipedia seems to think that it's to hide the "exact class being created" but sometimes I use it for "hiding the details of how the complicated object is created" I guess it boils down to -- is hiding the business logic also consitutional of a "factory method"? Or maybe I'm getting buried in semantics. – Vaughan Hilts Apr 30 '18 at 5:30
  • 3
    You're getting buried in semantics. – Philip Kendall Apr 30 '18 at 5:32
  • Sounds good. I'll let it go @PhilipKendall :) – Vaughan Hilts Apr 30 '18 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.