java反射机制(工厂模式)

http://www.phpddt.com/dhtml/338.html

java里面没有typeof,js有。

我终于实现了用反射机制编写的工厂模式。java反射在工厂模式可以体现。

包含产品接口类,具体产品实现类,工厂类,这三种不同的类型

http://wenku.baidu.com/view/8899ecfff705cc175527094d.html

http://wenku.baidu.com/view/8ceb39c52cc58bd63186bd87.html

http://blog.csdn.net/justinavril/article/details/2873664

http://bbs.csdn.net/topics/60429890

http://www.cnblogs.com/pricks/archive/2009/08/11/1543855.html

http://www.cnblogs.com/bingoidea/archive/2009/06/21/1507889.html

http://www.cnblogs.com/octobershiner/archive/2012/03/18/2404751.html

具体java代码三种实现如下:

1、接口实现

/**

 * 

 */

package factory.realize1;



/**

 * @author wangjian2

 * 接口

 */

public interface Product {

    public String getProductName(String type,int id);

}

 

package factory.realize1;



public class Route implements Product {



    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub



    }



    @Override

    public String getProductName(String type, int id) {

        return "RouteNameupdate";

    }



}

 

/**

 * 

 */

package factory.realize1;



/**

 * @author wangjian2

 *

 */

public class Package implements Product {



    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub



    }



    @Override

    public String getProductName(String type, int id) {

        return "PackageName";

    }



}

 

/**

 * 

 */

package factory.realize1;



import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;



/**

 * @author wangjian2

 *

 */

public class Factory {



    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub



    }



    public void getName(String type,int id){

        if("跟团".equals(type)){

            Product prd = new Route();

            System.out.println(prd.getProductName("", 0));

        }else if ("自助游".equals(type)){

            Product prd = new Package();

            System.out.println(prd.getProductName("", 0));

        }

    }

    

    /**

     * 通过反射将参数传给函数并返回结果

     * @param type

     * @param id

     */

    public void getNameByreflect(String type,int id){

        Class<?> cls = null;

        try {
//获取指定的任意type的所需类的完全限定名(包名+类名)相关联的class对象 cls
= Class.forName("factory.realize1."+type);
//获取类对应的任意方法对象 Method getProductName
= cls.getMethod("getProductName", new Class[]{String.class,int.class});
//使用该方法 Object result
= getProductName.invoke(cls.newInstance(), new Object[]{new String(type),0}); System.out.println(result); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
/**

 * 

 */

package factory.realize1;





/**

 * @author wangjian2

 * 

 */

public class TestExample {



    /**

     * @param args

     */

    public static void main(String[] args) {

        Factory factory = new Factory();

        factory.getName("跟团", 0);

        //反射实现

        factory.getNameByreflect("Route", 0);

    }



    

}

2、抽象类实现

 

/**

 * 

 */

package factory.realize2;



/**

 * @author wangjian2

 * 抽象类

 */

public abstract class Product {

    public abstract String getProductName(String type,int id);

}
package factory.realize2;



public class Route extends Product {



    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub



    }



    @Override

    public String getProductName(String type, int id) {

        return "RouteNameupdate";

    }



}
/**

 * 

 */

package factory.realize2;



/**

 * @author wangjian2

 *

 */

public class Package extends Product {



    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub



    }



    @Override

    public String getProductName(String type, int id) {

        return "PackageName";

    }



}

 

/**

 * 

 */

package factory.realize2;



import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;



/**

 * @author wangjian2

 *

 */

public class Factory {



    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub



    }



    public void getName(String type,int id){

        if("跟团".equals(type)){

            Product prd = new Route();

            System.out.println(prd.getProductName("", 0));

        }else if ("自助游".equals(type)){

            Product prd = new Package();

            System.out.println(prd.getProductName("", 0));

        }

    }

    

    /**

     * 通过反射将参数传给函数并返回结果

     * @param type

     * @param id

     */

    public void getNameByreflect(String type,int id){

        Class<?> cls = null;

        try {

            cls = Class.forName("factory.realize1."+type);

            Method getProductName = cls.getMethod("getProductName", new Class[]{String.class,int.class});

            Object result = getProductName.invoke(cls.newInstance(), new Object[]{new String(type),0});

            System.out.println(result);

        } catch (ClassNotFoundException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (SecurityException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (NoSuchMethodException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IllegalArgumentException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IllegalAccessException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (InvocationTargetException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (InstantiationException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

 

/**

 * 

 */

package factory.realize2;





/**

 * @author wangjian2

 * 

 */

public class TestExample {



    /**

     * @param args

     */

    public static void main(String[] args) {

        Factory factory = new Factory();

        factory.getName("跟团", 0);

        factory.getNameByreflect("Route", 0);

    }



    

}

 

3、继承类重写(覆盖)

/**

 * 

 */

package factory.realize3;



/**

 * @author wangjian2

 * 继承类重写(覆盖)

 */

public class Product {

    public String getProductName(String type,int id) {

        return null;

    }

}

 

package factory.realize3;



public class Route extends Product {



    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub



    }



    @Override

    public String getProductName(String type, int id) {

        return "RouteNameupdate";

    }



}
/**

 * 

 */

package factory.realize3;



/**

 * @author wangjian2

 *

 */

public class Package extends Product {



    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub



    }



    @Override

    public String getProductName(String type, int id) {

        return "PackageName";

    }



}

 

/**

 * 

 */

package factory.realize3;



import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;



/**

 * @author wangjian2

 *

 */

public class Factory {



    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub



    }



    public void getName(String type,int id){

        if("跟团".equals(type)){

            Product prd = new Route();

            System.out.println(prd.getProductName("", 0));

        }else if ("自助游".equals(type)){

            Product prd = new Package();

            System.out.println(prd.getProductName("", 0));

        }

    }

    

    /**

     * 通过反射将参数传给函数并返回结果

     * @param type

     * @param id

     */

    public void getNameByreflect(String type,int id){

        Class<?> cls = null;

        try {

            cls = Class.forName("factory.realize1."+type);

            Method getProductName = cls.getMethod("getProductName", new Class[]{String.class,int.class});

            Object result = getProductName.invoke(cls.newInstance(), new Object[]{new String(type),0});

            System.out.println(result);

        } catch (ClassNotFoundException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (SecurityException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (NoSuchMethodException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IllegalArgumentException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IllegalAccessException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (InvocationTargetException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (InstantiationException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

 

/**

 * 

 */

package factory.realize3;





/**

 * @author wangjian2

 * 

 */

public class TestExample {



    /**

     * @param args

     */

    public static void main(String[] args) {

        Factory factory = new Factory();

        factory.getName("跟团", 0);

        factory.getNameByreflect("Route", 0);

        

    }



    

}

 

 个人理解反射机制是为了开发者方便,减少开发量。

http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html

java反射在工厂模式可以体现

http://blog.csdn.net/ritterliu/article/details/7764849

你可能感兴趣的:(java反射机制)