本文主要是为了实战Java反射,简单工厂只是一个背景,故对简单工厂模式不做解释了。
1、首先先做个一个使用了简单工厂模式的demo。 Fruit是一个接口,里面含有一个get()方法
1 2 3 4 5 package SimpleFactory;public interface Fruit { public void get () ; }
2、Apple与Banana为两个实现了Fruit的接口,其中Apple类中方法较多用于实现Java反射中的各种情况。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package SimpleFactory;public class Apple implements Fruit { private String name = "name" ; public String getName () { return name; } public void setName (String name) { this .name = name; } @Override public void get () { System.out.println("This is apple" ); } private void aha () { System.out.println("This is apple, but you can't see this word." + name); } private void ahaString (String str) { System.out.println("This is apple, but you can't see this word." + str); } private String ahaReturn (String str) { System.out.println("This is apple, but you can't see this word." + str); return "yes" ; } }
3、FruitFactory为简单工厂模式创建类(为了后期再Main函数中体现Java反射机制,正确的应该像注释中那样,直接返回类)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package SimpleFactory;public class FruitFactory { public static Class getFruit (String typeName) { try { return Class.forName(typeName); }catch (ClassNotFoundException e){ e.printStackTrace(); } return null ; } }
4、下面是执行一下常见的反射操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 package SimpleFactory;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class MainSimpleFactory { public static void main (String[] args) { try { Class appleClass = FruitFactory.getFruit("SimpleFactory.Apple" ); Fruit apple = (Fruit) appleClass.newInstance(); apple.get(); Method getAha = appleClass.getDeclaredMethod("aha" ); getAha.setAccessible(true ); getAha.invoke(apple); Field name = appleClass.getDeclaredField("name" ); name.setAccessible(true ); name.set(apple, "newName" ); getAha.invoke(apple); Method getAhaString = appleClass.getDeclaredMethod("ahaString" ,String.class); getAhaString.setAccessible(true ); getAhaString.invoke(apple, "false" ); Method getAhaReturn = appleClass.getDeclaredMethod("ahaReturn" ,String.class); getAhaReturn.setAccessible(true ); String res = (String) getAhaReturn.invoke(apple, "true" ); System.out.println(res); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } try { Class appleClass = FruitFactory.getFruit("SimpleFactory.Banana" ); Fruit banana = (Fruit) appleClass.newInstance(); banana.get(); Method getAha = appleClass.getDeclaredMethod("ahaa" ); getAha.setAccessible(true ); getAha.invoke(banana); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
5、执行结果如下:
1 2 3 4 5 6 7 8 This is apple This is apple, but you can't see this word.name This is apple, but you can't see this word.newName This is apple, but you can't see this word.false This is apple, but you can't see this word.true yes This is Banana This is banana, but you can't see this word.