Document directory
- The fourth advantage of the static factory method is that they can return any child type objects of the original return type.
- Enummapdemo
- The fourth advantage of the static factory method is that they make the code more concise when building parameterized instances.
- Myhashmap
The fourth advantage of the static factory method is that they can return any child type objects of the original return type.
The Java. util. enumset class introduced in release 1.5 does not have a common constructor and only has static factory methods. They return one of the two implementation classes, depending on the underlyingEnumeration type size: If its element is smaller than or equal to 64, like most enumeration types, the static factory method returns a regularenumset instance, which is supported by a single long; if there are more than 64 enumerated elements, the factory returns the jumboenumset instance, which is supported by the long array. The specific factory method code is as follows:
View code
/** * Creates an empty enum set with the specified element type. * * @param elementType the class object of the element type for this enum * set * @throws NullPointerException if <tt>elementType</tt> is null */ public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) { Enum[] universe = getUniverse(elementType); if (universe == null) throw new ClassCastException(elementType + " not an enum"); if (universe.length <= 64) return new RegularEnumSet<E>(elementType, universe); else return new JumboEnumSet<E>(elementType, universe); }
Note: The Enumeration type size refers to the number of elements in the enumeration type. For example, the following enumeration type databasetype has a size of 4.
// Currently supported Database Type Enumeration type definition. The enumerated type size is 4. Enum databasetype {MySQL, Oracle, DB2, sqlserver}
Now that we have mentioned enumset, we can also talk about enummap. Here is an enummap instance:
Enummapdemoview code
Package Edu. SJTU. erplab. collection; import Java. util. enummap; // currently supported Database Type Enumeration type definitions Enum databasetype {MySQL, Oracle, DB2, sqlserver} public class enummapdemo {// method defined in the class to obtain the database URL and the enummap statement. Private enummap <databasetype, string> URLs = new enummap <databasetype, string> (databasetype. class); // constructor public enummapdemo () {URLs. put (databasetype. DB2, "JDBC: DB2: // localhost: 5000/sample"); URLs. put (databasetype. mySQL, "JDBC: mysql: // localhost/mydb"); URLs. put (databasetype. oracle, "JDBC: oracle: thin: @ localhost: 1521: sample"); URLs. put (databasetype. sqlserver, "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = mydb");}/*** depending on the database type, return the corresponding URL * @ Param type databasetype Enumeration type instance * @ return */Public String geturl (databasetype type) {return this. URLs. get (type);} public static void main (string [] ARGs) {enummapdemo EMD = new enummapdemo (); system. out. println (EMD. geturl (databasetype. oracle ));}}
The fourth advantage of the static factory method is that they make the code more concise when building parameterized instances.
When calling the constructor of the parameterized class, it must be specified even if the type parameter is obvious. This usually requires two successive type parameters. For example, in the following instances, the type parameter <string, list <string> is declared twice. Here, we can see that the parameter types at the beginning and end are the same.
Map<String,List<String>> m1=new HashMap<String,List<String>>();
But with the static factory method, the compiler can find the type parameter for you. This is called type inference ). For example, assume that myhashmap provides the following static factory method:
// Static factory method public static <K, V> myhashmap <K, V> newinstance () {return New myhashmap <K, V> ();}
Then you can replace the preceding tedious statement with the following concise code:
Map<String,List<String>> m2=MyHashMap.newInstance();
Unfortunately, the implementation of standard sets, such as hashmap, does not provide factory methods, but these methods can be put in our own tool class, such as our custom implementation of myhashmap.
Complete code example:
Myhashmapview code
Package Edu. SJTU. erplab. collection; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; public class myhashmap <K, V> extends hashmap <K, V> {// static factory method public static <K, V> myhashmap <K, V> newinstance () {return New myhashmap <K, V> ();} public static void main (string [] ARGs) {list <string> L = new arraylist <string> (); L. add ("zhangsan"); L. add ("Lisi"); L. add ("wangwu"); system. out. println (l); Map <string, list <string> m1 = new hashmap <string, list <string> (); m1.put ("M1", L); system. out. println (M1); Map <string, list <string> m2 = myhashmap. newinstance (); m2.put ("m2", L); system. out. println (m2 );}}
Output result:
[Zhangsan, Lisi, wangwu]
{M1 = [zhangsan, Lisi, wangwu]}
{M2 = [zhangsan, Lisi, wangwu]}