简体   繁体   中英

Static Factory method Advantages

I was reading effective java and one advantage of static factory methods written is that they can return an object of any sub-type of return type. I understood the way we can implement this as mentioned in following link with example. https://www.slideshare.net/mysky14/java-static-factory-methods

But in the book an example of Collections API is given that has static factory methods in java.util.Collections utility class and it is written that "Collections API is much smaller than it would have been had it exported 32 separate public classes".

It is also mentioned that in this manner, API can return objects without their classes to be public and this results in very compact API.

I want to know how the API size is reduced by implementing this method and not having separate public classes.

In general having static factory method would take out your object instantiation logic out of your class. Suppose based on certain logic, you need to return different subclass objects. This would result in if-else logic in your class method whichever is responsible for appropriate object instantiation. Moving this out to static factory method would result in cleaner class design which would be easier to test and closer to "Closed to modification" principle

I want to know how the API size is reduced by implementing this method and not having separate public classes.

Let's use the same concrete example used in the book: java.util.EnumSet has static factories that return one of two implementations: RegularEnumSet or JumboEnumSet . These implementations have their own complexities, but are effectively hidden to the clients of Collections. In theory, the factories could use other implementations in the future, and the clients of them would not be affected.

If you visualized this in a class diagram, the factory methods (eg, of() , as opposed to a constructor) return an abstract type EnumSet , which hides the details of the implementations. Abstract (or Interface) types effectively abstract (simplify) the API.

在此处输入图片说明

What's more, the implementations are actually package private , meaning they're declared without a public keyword. This means that only classes in the same package can see them, so it prevents having Client depend on them. This is a great example of information hiding, which allows API developers to simplify their API and also to change the hidden parts later without breaking the code.

Another example that comes to mind where factory methods can simplify an API are the concrete iterators in Collections. In this case, it's a factory method that is not static, eg, ArrayList.iterator() , that returns a concrete iterator for ArrayLists. The name of this class is even less "known" than the EnumSet implementations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM