Annotation与工厂设计模式

    xiaoxiao2022-07-15  140

    一、先自定义一个Annotation 

    import java.lang.annotation.Retention; import static java.lang.annotation.RetentionPolicy.RUNTIME; //先定义一个自定义注解 @Retention ( RUNTIME ) @interface MyAnnotation1{ public Class<?> target3() ; }

    二、定义一个接口及接口的实现 

    interface IFruite{ public void eat() ; } class Apple implements IFruite{ @Override public void eat() { System.out.println ("吃一个苹果!" ); } }

    三、定义一个工厂 

    @MyAnnotation1(target3 = Apple.class) class Factory{ public static <T> T getIntance() throws IllegalAccessException, InstantiationException { MyAnnotation1 mt =Factory.class.getAnnotation (MyAnnotation1.class) ; return (T)mt.target3().newInstance(); } }

     四、测试类

    public class Annotation与工厂设计模式 { public static void main(String[] args) throws InstantiationException, IllegalAccessException { IFruite fruite=Factory.getIntance (); fruite.eat (); } }

     

    最新回复(0)