java注解应用在工厂模式上

2017-08-21  本文已影响60人  kxs109

1.注解简单示例

使用注解的bean类
public class Player {
    @PlayerAnnotation(name = "Kobe")
    private String name;// 名字
    @PlayerAnnotation(age = 28)
    private int age;// 年龄
    @PlayerAnnotation(height = 198)
    private float height;// 身高
    @PlayerAnnotation(weight = 96)
    private float weight;// 体重
    @PlayerAnnotation(number = "24")
    private String number;// 球衣号码
    @PlayerAnnotation(team = "洛杉矶湖人")
    private String team;// 球队
    @PlayerAnnotation(skill = "进攻万花筒")
    private String skill;// 技能
    @PlayerAnnotation(inPosition = Position.POINT_GUARD)
    private String inPosition;// 司职位置
    @PlayerAnnotation(draft_year = "1996")
    private String draft_year;// 选秀年
    @PlayerAnnotation(overall = 13)
    private int overall;// 顺位
}
注解类

RetentionPolicy.RUNTIME----在运行时生成
ElementType.FIELD----该注解只能定义在类成员上

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface PlayerAnnotation {

    public String name() default "";// 名字

    public int age() default 0;// 年龄

    public float height() default 0f;// 身高

    public float weight() default 0;// 体重

    public String number() default "";// 秋衣号码

    public String team() default "";// 球队

    public String skill() default "";// 技能

    public enum Position {// 司职位置
        POWER_FORWARD, SMALL_FORWARD, CENTER, SHOOTING_GUARD, POINT_GUARD
    }

    public Position inPosition() default Position.POINT_GUARD;

    public String draft_year() default "";// 选秀年

    public int overall() default 0;// 顺位
}
测试类
public class PlayerTest {
    @Test
    public void test() {
        testAnnotation();
    }

    private void testAnnotation() {
        try {
            Class<?> cls = Class.forName("com.kxs109.annotation.Player");
            Field[] declaredFields = cls.getDeclaredFields();
            String fieldName;
            PlayerAnnotation annotation;
            for (Field field : declaredFields) {
                fieldName = field.getName();
                annotation = field.getAnnotation(PlayerAnnotation.class);
                if (fieldName.equals("name")) {
                    System.out.println("name:" + annotation.name());
                } else if (fieldName.equals("age")) {
                    System.out.println("age:" + annotation.age());
                } else if (fieldName.equals("height")) {
                    System.out.println("height:" + annotation.height());
                } else if (fieldName.equals("weight")) {
                    System.out.println("weight:" + annotation.weight());
                } else if (fieldName.equals("number")) {
                    System.out.println("number:" + annotation.number());
                } else if (fieldName.equals("team")) {
                    System.out.println("team:" + annotation.team());
                } else if (fieldName.equals("skill")) {
                    System.out.println("skill:" + annotation.skill());
                } else if (fieldName.equals("inPosition")) {
                    System.out.println("inPosition:" + annotation.inPosition());
                } else if (fieldName.equals("draft_year")) {
                    System.out.println("draft_year:" + annotation.draft_year());
                } else if (fieldName.equals("overall")) {
                    System.out.println("overall:" + annotation.overall());
                }
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
测试结果
name:Kobe
age:28
height:198.0
weight:96.0
number:24
team:洛杉矶湖人
skill:进攻万花筒
inPosition:POINT_GUARD
draft_year:1996
overall:13

2.注解用在工厂模式上

@MobileAnnotation("com.kxs109.annotation2.Vivo")
public class AnnotationTest {
    @Test
    public void testAnnotation() {
        Class<?> cls = AnnotationTest.class;
        MobileAnnotation annotation = cls.getAnnotation(MobileAnnotation.class);
        MobileFactory.getInstance(annotation.value()).adv();;
    }
}

@Retention(RetentionPolicy.RUNTIME)
@interface MobileAnnotation {
    public String value() default "";
}


/**
 * 工厂类
 * @author
 *
 */
class MobileFactory {
    public static Mobile getInstance(String className) {
        try {
            return (Mobile) Class.forName(className).newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

interface Mobile {
    void adv();
}

class Xiaomi implements Mobile {

    @Override
    public void adv() {
        System.out.println("小米手机,拍人更美");
    }
}

class Vivo implements Mobile {

    @Override
    public void adv() {
        System.out.println("vivo1200万柔光双摄,照亮你的美");
    }
}
上一篇 下一篇

猜你喜欢

热点阅读