I am trying to understand the factory design pattern and how it can be implemented in Javascript. So far what I understood is factory design pattern helps to create an instance of more specific class that inherits from a general abstract class.

So I was reading this article. I stumbled upon second point of drawbacks of constructors and class which states:

Details of instantiation get leaked into the calling API (via the 'new' [keyword] requirement):
All callers are tightly coupled to the constructor implementation. If you ever need the additional flexibility of the factory, the refactor is a breaking change. Class to factory refactors are common enough that they appear in the seminal Refactoring book, “Refactoring: Improving the Design of Existing Code” by Martin Fowler, Kent Beck, John Brant, William Opdyke, and Don Roberts.

What does details of instantiation get leaked into the calling API mean?

  • Frankly, that's not a very good article. I would find a better one. For one thing, "classical" Javascript doesn't work the same way as ES6. Classical Javascript doesn't even have classes; he seems to be talking about classical Javascript, but most of his examples are in ES6, where the problem of missing new keywords doesn't even exist. Several of the points he makes in his article are debunked in the replies. – Robert Harvey Sep 23 '17 at 16:16
  • The author of the article also misunderstands of the Open/Closed Principle as explicitly pointed out in the comments (and then greatly magnified by the author's response to the comment). – Derek Elkins left SE Sep 24 '17 at 5:15
  • @RobertHarvey okay. It had a lots of upvotes. so thought it would be a nice article. – Pravin Sep 25 '17 at 6:36

Details of instantiation get leaked if you don't use a factory.

Suppose you have an abstract Shape class, which has a Circle and a Polygon subclass, the latter having a Triangle and Square subclass.

If you want to instantiate somewhere in your code some Shape objects, your code will have to know not only about Shape, but also about every concrete subclass it might have to instantiate, as well as the parameters to use to invoke the corresponding constructor.

Your specific instantiation code will hence depend on a lot of details (that leaked). Example:

 int shapetype=random();  
 if (shapetype==0) 
      myshape = new Circle(random(), random()); 
 else if (shapetype==2)
      myshape = new Square(random());  

If on the other hand you'd use a factory, the using class would only have to know about the abstract Shape class and how to invoke the factory. It doesn't have to know anything about the subclasses and constructor parameters, thus making your code less dependent on other classes.

  myshape = shapefactory (random());
| improve this answer | |
  • so we have to explicitly define with an if case in the first scenario with a new keyword. but in the second one we just call a method of the factory? – Pravin Sep 25 '17 at 6:36
  • @Pravin yes. In the second approach your code doesn't have to know anything about the subclassing. It just has to know the abstract class and the factory method. – Christophe Sep 25 '17 at 6:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.