static factory method


package ycl.learn.effective.java;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* java.long.Boolean
*
* The <code>Boolean</code> object corresponding to the primitive
* value <code>true</code>.
public static final Boolean TRUE = new Boolean(true);

* The <code>Boolean</code> object corresponding to the primitive
* value <code>false</code>.
public static final Boolean FALSE = new Boolean(false);

* Returns a <tt>Boolean</tt> instance representing the specified
* <tt>boolean</tt> value. If the specified <tt>boolean</tt> value
* is <tt>true</tt>, this method returns <tt>Boolean.TRUE</tt>;
* if it is <tt>false</tt>, this method returns <tt>Boolean.FALSE</tt>.
* If a new <tt>Boolean</tt> instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Boolean(boolean)}, as this method is likely to yield
* significantly better space and time performance.
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
*
* Enum
*
* public enum UserEnum {
ACTION("ACTION"),FIRST_NAME("FIRST NAME"),LAST_NAME("LAST NAME"),E_MAIL("E-MAIL"),PHONE("PHONE");

private String name=null;
private UserEnum(String name){
this.name = name;
}
public String getName(){
return name;
}
}
* jad class File:
*
* public final class UserEnum extends Enum
{

private UserEnum(String s, int i, String name)
{
super(s, i);
this.name = null;
this.name = name;
}

public String getName()
{
return name;
}

public static UserEnum[] values()
{
UserEnum auserenum[];
int i;
UserEnum auserenum1[];
System.arraycopy(auserenum = ENUM$VALUES, 0, auserenum1 = new UserEnum[i = auserenum.length], 0, i);
return auserenum1;
}

public static UserEnum valueOf(String s)
{
return (UserEnum)Enum.valueOf(ycl/learn/effective/java/UserEnum, s);
}

public static final UserEnum ACTION;
public static final UserEnum FIRST_NAME;
public static final UserEnum LAST_NAME;
public static final UserEnum E_MAIL;
public static final UserEnum PHONE;
private String name;
private static final UserEnum ENUM$VALUES[];

static
{
ACTION = new UserEnum("ACTION", 0, "ACTION");
FIRST_NAME = new UserEnum("FIRST_NAME", 1, "FIRST NAME");
LAST_NAME = new UserEnum("LAST_NAME", 2, "LAST NAME");
E_MAIL = new UserEnum("E_MAIL", 3, "E-MAIL");
PHONE = new UserEnum("PHONE", 4, "PHONE");
ENUM$VALUES = (new UserEnum[] {
ACTION, FIRST_NAME, LAST_NAME, E_MAIL, PHONE
});
}
}
* so you know enum class first declared the singlton oject, then init it as static.
* we usually call values(), this method will be rewrite the father's method.
* and It will be copay to asuserenum1, from 0 to length.[as Type is UserEnum]
* we also usually call valueOf(String s), this method also mandatory transform type to UserEnum.
* so let me see see the enum's father.
*
*
* public abstract class Enum<E extends Enum<E>>
implements Comparable<E>, Serializable {
* The name of this enum constant, as declared in the enum declaration.
* Most programmers should use the {@link #toString} method rather than
* accessing this field.
private final String name;

* Returns the name of this enum constant, exactly as declared in its
* enum declaration.
*
* <b>Most programmers should use the {@link #toString} method in
* preference to this one, as the toString method may return
* a more user-friendly name.</b> This method is designed primarily for
* use in specialized situations where correctness depends on getting the
* exact name, which will not vary from release to release.
*
* @return the name of this enum constant
public final String name() {
return name;
}
* The ordinal of this enumeration constant (its position
* in the enum declaration, where the initial constant is assigned
* an ordinal of zero).
*
* Most programmers will have no use for this field. It is designed
* for use by sophisticated enum-based data structures, such as
* {@link java.util.EnumSet} and {@link java.util.EnumMap}.
private final int ordinal;

* Returns the ordinal of this enumeration constant (its position
* in its enum declaration, where the initial constant is assigned
* an ordinal of zero).
*
* Most programmers will have no use for this method. It is
* designed for use by sophisticated enum-based data structures, such
* as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
*
* @return the ordinal of this enumeration constant
public final int ordinal() {
return ordinal;
}
this method will be call be sun object.
* Sole constructor. Programmers cannot invoke this constructor.
* It is for use by code emitted by the compiler in response to
* enum type declarations.
*
* @param name - The name of this enum constant, which is the identifier
* used to declare it.
* @param ordinal - The ordinal of this enumeration constant (its position
* in the enum declaration, where the initial constant is assigned
* an ordinal of zero).
protected Enum(String name, int ordinal) {
this.name = name;
this.ordinal = ordinal;
}
* Returns the name of this enum constant, as contained in the
* declaration. This method may be overridden, though it typically
* isn't necessary or desirable. An enum type should override this
* method when a more "programmer-friendly" string form exists.
*
* @return the name of this enum constant
public String toString() {
return name;
}
use == instead of equal.
* Returns true if the specified object is equal to this
* enum constant.
*
* @param other the object to be compared for equality with this object.
* @return true if the specified object is equal to this
* enum constant.
public final boolean equals(Object other) {
return this==other;
}
this is compared with the ordinal.
public final int compareTo(E o) {
Enum other = (Enum)o;
Enum self = this;
if (self.getClass() != other.getClass() && // optimization
self.getDeclaringClass() != other.getDeclaringClass())
throw new ClassCastException();
return self.ordinal - other.ordinal;
}

this is the valueof method, will be get enumConstantDirectory() from class, to get the name of enum then return object.
* Returns the enum constant of the specified enum type with the
* specified name. The name must match exactly an identifier used
* to declare an enum constant in this type. (Extraneous whitespace
* characters are not permitted.)
*
* @param enumType the <tt>Class</tt> object of the enum type from which
* to return a constant
* @param name the name of the constant to return
* @return the enum constant of the specified enum type with the
* specified name
* @throws IllegalArgumentException if the specified enum type has
* no constant with the specified name, or the specified
* class object does not represent an enum type
* @throws NullPointerException if <tt>enumType</tt> or <tt>name</tt>
* is null
* @since 1.5
public static <T extends Enum<T>> T valueOf(Class<T> enumType,
String name) {
T result = enumType.enumConstantDirectory().get(name);
if (result != null)
return result;
if (name == null)
throw new NullPointerException("Name is null");
throw new IllegalArgumentException(
"No enum const " + enumType +"." + name);
}

*
*
*
* @author e557400
*
*/
public class StaticFactoryMethod {

Map<String,String> map ;

/**
* instance by construction.
* @param map
*/
public StaticFactoryMethod(Map<String,String> map){
this.map=map;
}

/**
* instance by static factory method
* @return
*/
public static StaticFactoryMethod newInstanceByMap(){
return new StaticFactoryMethod(new HashMap<String,String>());
}


/**
* Boolean and Enum.
* The same point is create Object when you init this Object.
* and then you can use the created Object, Don't need to create this Object.
* This can be shared for hold Application scope, Until the Server is stop.
* The JVM is out.
*
* This is usually use as tools.
*
* @param args
*/
public static void main(String[] args) {
/**
* 1. you need to init params
* 2. you can call method by static factory name, very easy to use.
*/
StaticFactoryMethod con_sfm = new StaticFactoryMethod(new HashMap<String,String>());
StaticFactoryMethod method_sfm = StaticFactoryMethod.newInstanceByMap();

/**
* 3.when you call the method, you don't need to create object first.
*/
List emptyList = Collections.emptyList();//example Java Collections Framework
Map map =Collections.emptyMap();//example Java Collections Framework
Comparator comparator = Collections.reverseOrder();//example reverseOrder[c2.compareTo(c1)]
Comparator reverse_comp = comparator = Collections.reverseOrder(comparator); ///example reverseOrder[cmp.compare(t2, t1)]

ServiceSPI one_serviceSPI = new ServiceSPI(){
public ServiceAPI newService() {
return new ServiceAPI(){
public void say() {
System.out.println("serviceSPI: one");
}

};
}
};

ServiceSPI two_serviceSPI = new ServiceSPI(){
public ServiceAPI newService() {
return new ServiceAPI(){
public void say() {
System.out.println("serviceSPI: two");
}

};
}
};
/**
* 4. we can call the method with less params.
*/
ServiceFramework.registerDefaultSPI(one_serviceSPI);
ServiceFramework.registerSIP("one", one_serviceSPI);
ServiceFramework.registerSIP("two", two_serviceSPI);
//register SPI

ServiceFramework.newInstance().say();
ServiceFramework.newInstance("one").say();
ServiceFramework.newInstance("two").say();
//use difference SPI to get API and call API's method.
//It is like JDK give Servlet API, and Tomcat implement the API Called
//Tomcat Servlet Provider Interface, i just call TOMServiceSPI.
//The ServiceFramework Regiest TOMServiceSPI as Default, so we call
//Servlet API, in real that is process as Tomcat Service Implement.

}

/**
*API
*/
interface ServiceAPI{
public void say();
}

/**
*SPI
*/
interface ServiceSPI{
ServiceAPI newService();
}

/**
* SPF
*/
static class ServiceFramework{
private ServiceFramework(){}
private static final ConcurrentMap<String,ServiceSPI> spis = new ConcurrentHashMap<String,ServiceSPI>();
private static final String DEFAULT_SPI_NAME = "<def>";

public static void registerDefaultSPI(ServiceSPI spi){
registerSIP(DEFAULT_SPI_NAME,spi);
}

public static void registerSIP(String name, ServiceSPI spi) {
spis.put(name, spi);
}

public static ServiceAPI newInstance(){
return newInstance(DEFAULT_SPI_NAME);
}

private static ServiceAPI newInstance(String name) {
ServiceSPI spi = spis.get(name);
if(spi == null){
throw new IllegalArgumentException("No provider registered with name: " + name);
}
return spi.newService();
}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值