Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen to use the abstract factory pattern?
    primarykey
    data
    text
    <p>I'm trying to succinctly describe <strong>when</strong> to use a factory, for both myself and my team. I ran across the following related questions, which helped somewhat:</p> <ul> <li><a href="https://stackoverflow.com/questions/4067271/when-to-use-factory-patterns">When to use factory patterns?</a> <ul> <li>(useful pdf link is broken)</li> </ul></li> <li><a href="https://stackoverflow.com/questions/2181269/how-do-you-create-your-factories">How do you create your Factories?</a> <ul> <li>(more 'how' rather than 'when')</li> </ul></li> <li><a href="https://stackoverflow.com/questions/489623/what-is-your-threshold-to-use-factory-instead-of-a-constructor-to-create-an-objec">What is your threshold to use factory instead of a constructor to create an object?</a> <ul> <li>(some general answers)</li> </ul></li> <li><a href="https://stackoverflow.com/questions/69849/factory-pattern-when-to-use-factory-methods">Factory Pattern. When to use factory methods?</a> <ul> <li>(more about factory methods than factory classes)</li> </ul></li> <li><a href="https://stackoverflow.com/questions/1519358/when-to-use-factory-method-pattern">When to use Factory method pattern?</a> <ul> <li>(again more about factory methods)</li> </ul></li> </ul> <p>Based on these links, and a bunch of other sources (listed at the bottom), I've come up with the following:</p> <p><strong>When to use the abstract factory pattern:</strong></p> <ul> <li>when you use an interface var or the 'new' operator <ul> <li>e.g. User user = new ConcreteUserImpl(); </li> </ul></li> <li>and the code you are writing should be testable / extensible at some point </li> </ul> <p><strong>Explanation:</strong></p> <ul> <li>interfaces by their very nature imply multiple implementations (good for unit testing) </li> <li>interface vars imply OCP- and LSP-compliant code (support sub-classing) </li> <li>use of the 'new' operator breaks OCP/DI, because highly-coupled classes are hard to test or change </li> </ul> <p><strong>"Do I create a factory for every object type? That seems excessive."</strong></p> <ul> <li>no, you can have one (or a few) factories that produce a lot of (usually related) object types </li> <li>e.g. appFactory.createUser(); appFactory.createCatalog(); etc. </li> </ul> <p><strong>When NOT to use a factory:</strong></p> <ul> <li>the new object is very simple and unlikely to be sub-classed <ul> <li>e.g. List list = new ArrayList();</li> </ul></li> <li>the new object is not interesting to test <ul> <li>has no dependencies</li> <li>performs no relevant or long-running work</li> <li>e.g. Logger log = new SimpleLogger();</li> </ul></li> </ul> <p><strong>References:</strong></p> <ul> <li><a href="http://googletesting.blogspot.com/2008/08/where-have-all-singletons-gone.html" rel="nofollow noreferrer">http://googletesting.blogspot.com/2008/08/where-have-all-singletons-gone.html</a></li> <li><a href="http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/" rel="nofollow noreferrer">http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/</a></li> <li><a href="http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/" rel="nofollow noreferrer">http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/</a></li> <li><a href="http://en.wikipedia.org/wiki/Dependency_Injection" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Dependency_Injection</a></li> <li><a href="http://en.wikipedia.org/wiki/Open_Closed_Principle" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Open_Closed_Principle</a></li> <li><a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Liskov_substitution_principle</a></li> </ul> <hr/> <p>My question is: is my summary accurate, and does it make sense? Is there anything I've overlooked?</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload