论坛首页 Java企业应用论坛

NUll Objects的一种新实现

浏览 3190 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-06-27  
转自:寸心知

前些天看到Joe Walnes's blog提到了一种NUll Objects的新实现,自我感觉在项目中引入,效果应该不错,但是他没有提供源码。凑巧,这段时间正在使用JMock,Null Objects其实也是Mock Objects的一种特殊形式。因此,我从JMock中提取了一些代码,实现了新的NUll Objects。

在Joe的例子中,它是实现了针对Interface的NUll Objects,并没有实现Pojo class的NUll Objects,这主要有两个好处:

1、面向接口编程越来越普遍,它的优势应该是大家都比较熟悉了,这里就不在婆妈了。

2、只实现interface的NUll Objects只需要使用JDK1.4中自带的reflection API即可,如果要实现Pojo,还需要其它的API(如Cglib),没有太大的必要。

既然我是继续试验TDD编程,只测试了interface,其它的如int,String等,可以自己试试。首先看看testcase的源码:


NUlltest.java

------------------

public class NULLtest extends TestCase {
    
    public void testObject(); {
        Boy boy = new Boy();;
        Girl girl = new PrettyGirl();;
        assertEquals("", Kissable.NULL.kiss("sophie");.kissSomeone(););;
        assertEquals("", Boy.NULL.kiss("sophie");.kissSomeone(););;
        assertEquals(girl.kissSomeone();, boy.kiss("sophie");.kissSomeone(););;
    }
    
    interface Kissable {
        Kissable NULL = (Kissable);NUll.object(Kissable.class);;//NUll Objects
        Girl kiss(Object kisser);;
    }
    
    class Boy implements Kissable {
        public Girl kiss(Object kisser); {
            System.out.println("I was kissed by " + kisser);;
            return new PrettyGirl();;
        }
    } 
    
    interface Girl {
        Girl NULL = (Girl);NUll.object(Girl.class);; //NUll Objects
        String kissSomeone();;
    }
   
    class PrettyGirl implements Girl {
        public String kissSomeone(); {
            return "sophie";
        }
    }
}

然后看一看NUll Objects的源码:

NUll.java

-----------------------

import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

public class NUll implements InvocationHandler{

    private Map resultValuesByType = new HashMap();;
    
    public NUll(); {
        createDefaultResults();;
    }

    public static Object object(Class clazz); {
        return Proxy.newProxyInstance(clazz.getClassLoader();,
                new Class[] { clazz }, new NUll(););;
    }
    

    //摘自JMock,在此感谢JMock的作者
    public Object invoke(Object proxy, Method method, Object[] args);
            throws Throwable {
        Class returnType = method.getReturnType();;

        if (resultValuesByType.containsKey(returnType);); {
            return resultValuesByType.get(returnType);;
        } else if (returnType.isArray();); {
            return Array.newInstance(returnType.getComponentType();, 0);;
        } else if (returnType.isInterface();); {
            return NUll.object(returnType);;
        } else {
            throw new Exception(String.valueOf(method););;
        }
    }
   
    public void addResult( Class resultType, Object resultValue ); {
        resultValuesByType.put(resultType, resultValue);;
    }
    
    protected void createDefaultResults(); {
        addResult(boolean.class, Boolean.FALSE);;
        addResult(void.class, null);;
        addResult(byte.class, new Byte((byte);0););;
        addResult(short.class, new Short((short);0););;
        addResult(int.class, new Integer(0););;
        addResult(long.class, new Long(0L););;
        addResult(char.class, new Character('\0'););;
        addResult(float.class, new Float(0.0F););;
        addResult(double.class, new Double(0.0););;
        addResult(Boolean.class, Boolean.FALSE);;
        addResult(Byte.class, new Byte((byte);0););;
        addResult(Short.class, new Short((short);0););;
        addResult(Integer.class, new Integer(0););;
        addResult(Long.class, new Long(0L););;
        addResult(Character.class, new Character('\0'););;
        addResult(Float.class, new Float(0.0F););;
        addResult(Double.class, new Double(0.0););;
        addResult(String.class, "");;
    }
}

以后,可能我会用Cglib实现Pojo的NUll Objects,主要在于是否真的有用。
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics