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();
}
}
}
分享到:
相关推荐
简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单...
简单工厂(Simple Factory)模式又称为静态工厂方法(Static Factory Method)模式,属于类的创建型模式,通常它根据自变量的不同返回不同的类的实例。 简单工厂模式的实质是由一个工厂类根据传入的参量,动态决定...
Include auto-value-cursor in your project and add a static factory method to your auto-value object. import com.gabrielittner.auto.value.cursor.ColumnName; @AutoValue public abstract class User { ...
从设计模式的类型上来说,简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂...
Java设计模式是面向对象编程...在阅读《Chapter1___Java常用设计模式(SingleTon、FactoryMethod、AbstractFactory)》的相关资料时,你可以更深入地学习这些模式的细节,包括适用场景、优缺点以及如何在实际项目中实现。
消费者模型网络一个类实现RPC框架设计模式创造型模式(Creative)工厂简单模式(Simple Factory)工厂方法模式(工厂方法)抽象工厂模式(Abstract Factory)静态工厂方法模式(Static Factory Method)创建者模式...
今天我们要探讨的是设计模式中的一个经典成员——工厂方法模式(Factory Method Pattern)。工厂方法模式是创建型设计模式的一种,它提供了一种创建对象的最佳方式。 ### 一、什么是工厂方法模式? 工厂方法模式...
简单工厂模式(Simple Factory Pattern)又称静态工厂方法(Static Factory Method)模式,它属于类创建型模式。在简单工厂模式中,可以根据自变量的不同返回不同类的实例。简单工厂模式专门定义一个类来负责创建...
1. **简单工厂模式(Simple Factory)/ 静态工厂方法模式(Static Factory Method)** - 在简单工厂模式中,一个工厂类根据输入参数决定创建哪个类的实例。这种模式的核心是工厂类,它包含必要的判断逻辑来决定何时...
### 简单工厂模式(Static Factory Method Pattern) 简单工厂模式是最基本的工厂模式,它通过定义一个创建对象的静态方法,将创建对象的过程封装在该方法中,客户端只需调用这个静态方法即可获得所需对象的实例。...
简单工厂模式又称为静态工厂方法(Static Factory Method)模式,属于类的创建型模式,通常根据一个条件(参数)来返回不同的类的实例。 简单工厂模式的优点: * decoupling:简单工厂模式可以将对象的创建与使用...
6. 非线程安全的静态工厂方法(Non-thread-safe Static Factory Method): 虽然不适用于多线程环境,但简单易读。`SingleInstance6.java`可能使用了这种模式。如果确信单例在程序运行期间不会被多线程同时访问,这...
1. 简单工厂模式(Static Factory Method):也被称作静态工厂方法模式,其主要目的是定义一个接口来创建对象,但它并不是GoF(Gang of Four)所提出的23种设计模式之一。它将对象的创建封装在一个工厂类中,根据...
"t02FactoryMethod" 指的是工厂方法(Factory Method)设计模式,它是面向对象设计中的一种经典模式。这个模式主要关注于如何创建对象,通过引入一个工厂接口或者抽象类,将对象的创建过程封装起来,使得具体的对象...
静态工厂方法注入(Static Factory Method Injection) 除了构造器注入,Spring还支持通过静态工厂方法来创建Bean实例。这种方式不需要Bean类提供构造器,而是通过调用一个静态工厂方法来创建Bean。这种方法的优势...
简单工厂模式(Simple Factory Pattern)又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式。在简单工厂模式中,可以根据参数的不同返回不同类的实例。简单工厂模式专门定义一个类来负责创建...
3. 静态工厂方法注入(Static Factory Method Injection):除了上述两种方法外,Spring还支持通过静态工厂方法来创建对象并注入依赖。`FactoryHelloAction`类的实例化过程通过`createInstance`静态方法实现,这也...
- **静态工厂方法引用(Static Factory Method References)**: 在lambda表达式中可以直接使用类的静态工厂方法,简化了代码。 - **HttpClient API**: 提供了一个新的HTTP客户端API (`java.net.http`包),替代了过时...
简单工厂模式是属于创建型模式,又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单...
从设计模式的类型上来说,简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂...