Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you invoke a constructor, it always will return a new object (unless an exception is thrown). Static factory methods, or any kind of factory for that matter, don't have to always return a new object. For example, the <code>getInstance()</code> method on the traditional Singleton design pattern is a factory method that always returns the exact same object. There are cases where sometimes you want to do this kind of thing, whether it is to enforce an object can only be instantiated once, or to create some kind of object pool, etc. In general, I think this is a fringe reason for using static factory methods. The main purpose is to create nicely-named pseudo-constructors.</p> <p>Here's a (somewhat silly) example of using static factory methods to make nicely-named pseudo-constructors. Consider this class:</p> <pre><code>class Person { public Person(Role role) { setRole(role); } ... } </code></pre> <p>Without static factory methods, you might do something like this:</p> <pre><code>Person employee = new Person(Role.EMPLOYEE); Person manager = new Person(Role.MANAGER); </code></pre> <p>Instead you could create static factory methods:</p> <pre><code>class Person { public static Person newEmployee() { return new Person(Role.EMPLOYEE); } public static Person newManager() { return new Person(Role.MANAGER); } private Person(Role role) { setRole(role); } ... } </code></pre> <p>and you might instead do something like this:</p> <pre><code>Person employee = Person.newEmployee(); Person manager = Person.newManager(); </code></pre> <p>This may not be a good example, but consider a more complex constructor or one with a less descriptive parameter. Sometimes going the factory method route makes the code more clear. There are of course drawbacks...</p> <p>As far as limiting object creation, consider some strange constraint like there can never be more than one CEO:</p> <pre><code>class Person { private static Person singletonCEO = new Person(Role.CEO); public static Person newCEO() { return singletonCEO; } ... } </code></pre> <p>and how it would be created:</p> <pre><code>Person ceo1 = Person.newCEO(); Person ceo2 = Person.newCEO(); assertThat(ceo1, is(ceo2)); // JUnit 4.x </code></pre> <p>I hope these examples help.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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