Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was able to read some of this book <a href="http://books.google.com/books?id=ka2VUBqHiWkC&amp;pg=PA5&amp;lpg=PA5&amp;dq=Josh+Bloch+static+factory+methods&amp;source=bl&amp;ots=yXHhQfp1P1&amp;sig=vcgnUi6zE-vlLUMbW3F5iRh9wTg&amp;hl=en&amp;ei=lGasSqTlFteJtgfd_NWwCA&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=2#v=onepage&amp;q=&amp;f=false" rel="nofollow noreferrer">here</a>. After reading what he wrote it seems that what he is saying is that static factory methods give you more flexibility as a developer and it also allows you to be more explicit with what is being returned. When you contrast this to a constructor the constructor may not provide clarity in terms of what is being returned. Additionally you can do things like caching etc in the static factory method which I thought was fascinating. This approach seems like a good approach if you need this level of control and flexibility.</p> <p>The point on not creating unnecessary duplicate objects come in if you want to use caching. With this static factory approach you could return the same object on each call to the static factory method.</p> <p>an example:</p> <pre><code>public class Person { private Person(string firstName, string lastName) { this.FirstName = firstName; this.LastName = lastName; } public string FirstName {get; private set;} public string LastName {get; private set;} private static Dictionary&lt;string, Person&gt; objectPool = new Dictionary&lt;string, Person&gt;(); private object lockObject = new object(); public static Person CreatePerson(string firstName, string lastName) { var result = objectPool[firstName + lastName]; Person person = null; if (result != null) { return result } lock(lockObject) { person = new Person(firstName, lastName); objectPool.Add(firstName + lastName, person) } return person; } } </code></pre>
    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.
 

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