Factory Methods - Intro
7 September 2008This post lists some facts about factory methods.
Factory methods are static methods that return an instance of the native class. Some examples from JDK include :
LogManager.getLogManager
Pattern.compile
Collections.unmodifiableCollection, Collections.synchronizeCollection Calendar.getInstance
Factory methods do not have names like constructors and do not need to create a new object upon each invocation. Objects can be cached and reused, if necessary. Factory methods can return a subtype of their return type. They can return an object whose implementation class is unknown to the caller. It is a very valuable and widely used feature in many frameworks which use interfaces as the return type of static factory methods.
Common names for factory methods include getInstance and valueOf.
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Factory Methods - Intro