What's a "static factory" method?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
I thought i will add some light to this post on what i know. We used this technique extensively in our
recent android project
. Instead ofcreating objects using new operator
you can also usestatic method
to instantiate a class. Code listing:Static methods support conditional object creation: Each time you invoke a constructor an object will get created but you might not want that. suppose you want to check some condition only then you want to create a new object.You would not be creating a new instance of Vinoth each time, unless your condition is satisfied.
Another example taken from Effective Java.
This method translates a boolean primitive value into a Boolean object reference. The
Boolean.valueOf(boolean)
method illustrates us, it never creates an object. The ability ofstatic factory methods
to return the same object from repeatedinvocations
allows classes to maintain strict control over what instances exist at any time.Static factory methods
is that, unlikeconstructors
, they can return anobject
of anysubtype
of their return type. One application of this flexibility is that an API can return objects without making their classes public. Hiding implementation classes in this fashion leads to a very compact API.Calendar.getInstance() is a great example for the above, It creates depending on the locale a
BuddhistCalendar
,JapaneseImperialCalendar
or by default oneGeorgian
.Another example which i could think is
Singleton pattern
, where you make your constructors private create an owngetInstance
method where you make sure, that there is always just one instance available.A factory method a method that abstracts away the instantiation of an object. Generally factories are useful when you know that you need a new instance of a class that implements some interface but you don't know the implementing class.
This is useful when working with hierarchies of related classes, a good example of this would be a GUI toolkit. You could simply hard-code calls to the constructors for concrete implementations of each widget but if you ever wanted to swap one toolkit for another you'd have a lot of places to change. By using a factory you reduce the amount of code you would need to change.
One of the advantages that stems from Static factory is that that API can return objects without making their classes public. This lead to very compact API. In java this is achieved by Collections class which hides around 32 classes which makes it collection API very compact.
It all boils down to maintainability. The best way to put this is whenever you use the
new
keyword to create an object, you're coupling the code that you're writing to an implementation.The factory pattern lets you separate how you create an object from what you do with the object. When you create all of your objects using constructors, you are essentially hard-wiring the code that uses the object to that implementation. The code that uses your object is "dependent on" that object. This may not seem like a big deal on the surface, but when the object changes (think of changing the signature of the constructor, or subclassing the object) you have to go back and rewire things everywhere.
Today factories have largely been brushed aside in favor of using Dependency Injection because they require a lot of boiler-plate code that turns out to be a little hard to maintain itself. Dependency Injection is basically equivalent to factories but allows you to specify how your objects get wired together declaratively (through configuration or annotations).
A static factory method is good when you want to ensure that only one single instance is going to return the concrete class to be used.
For example, in a database connection class, you may want to have only one class create the database connection, so that if you decide to switch from Mysql to Oracle you can just change the logic in one class, and the rest of the application will use the new connection.
If you want to implement database pooling, then that would also be done without affecting the rest of the application.
It protects the rest of the application from changes that you may make to the factory, which is the purpose.
The reason for it to be static is if you want to keep track of some limited resource (number of socket connections or file handles) then this class can keep track of how many have been passed out and returned, so you don't exhaust the limited resource.
If the constructor of a class is private then you cannot create an object for class from outside of it.
We cannot create an object for above class from outside of it. So you cannot access x, y from outside of the class. Then what is the use of this class?
Here is the Answer : FACTORY method.
Add the below method in above class
So now you can create an object for this class from outside of it. Like the way...
Hence, a static method which returns the object of the class by executing its private constructor is called as FACTORY method
.