Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="http://en.wikipedia.org/wiki/Factory_method_pattern" rel="nofollow noreferrer">factory method pattern</a> can be useful for times when there is no need to create a new instance of an object in order to perform some action.</p> <p>Here are a couple of general cases I can think of where a static factory method which returns the same object can come in handy:</p> <ol> <li><p><strong>The object is expensive to create</strong> -- There is a lot of processing when the object is instantiated, so instantiating the object more than once is not desirable. (This is also related to the <a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="nofollow noreferrer">Singleton pattern</a>.)</p></li> <li><p><strong>The object keeps no state</strong> -- If there is no state difference between the instances, there is no good purpose to create a new object every time.</p></li> </ol> <p>The <a href="http://en.wikipedia.org/wiki/Factory_method_pattern" rel="nofollow noreferrer">Wikipedia page on the factory method pattern</a> has more information on this topic.</p> <hr> <p>Let's take a look at a concrete example.</p> <p>The <a href="http://java.sun.com/javase/6/docs/api/java/text/DateFormat.html" rel="nofollow noreferrer"><code>DateFormat</code></a> class uses the <a href="http://java.sun.com/javase/6/docs/api/java/text/DateFormat.html#getInstance()" rel="nofollow noreferrer"><code>getInstance</code></a> static method to return a <code>DateFormat</code> instance, which can be used to format a <code>Date</code> into a preset formatting depending on the locale of the machine.</p> <p>Since the <code>DateFormat</code> that is returned is using the same formatting for every date formatting action, there is no real reason for creating a new <code>DateFormat</code> instance every time.</p> <p>In general, the way this is implemented is to create an instance if an instance does not already exist, and then keep a reference to that instance. If the instance is needed again, the reference is returned. (This is generally how the Singleton pattern is implemented as well.)</p> <p>For example:</p> <pre><code>class MySingleInstanceObject { private MySingleInstanceObject instance; private MySingleInstanceObject() { // Initialize the object. // This may be expensive. } public MySingleInstanceObject getInstance() { if (instance == null) { instance = new MySingleInstanceObject(); } return instance; } } </code></pre> <p>(FYI, the above code is an example of a singleton. Also, it is not thread-safe.)</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