java高阶玩法:建造者+继承+注解

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * BuilderProperty
 *
 * @author 31235
 * @version 2023/05/26 15:26
 **/
// 注解定义
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface BuilderProperty {
    String value() default "";
}
/**
 * Person
 *
 * @author 31235
 * @version 2023/05/26 15:26
 **/
// 基础类
public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @BuilderProperty("name")
    public void setName(String name) {
        this.name = name;
    }
    @BuilderProperty("age")
    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

/**
 * Employee
 *
 * @author 31235
 * @version 2023/05/26 15:27
 **/
// 继承类
public class Employee extends Person {
    private String designation;

    public Employee() {
    }

    public Employee(String name, int age, String designation) {
        super(name, age);
        this.designation = designation;
    }

    @BuilderProperty("designation")
    public void setDesignation(String designation) {
        this.designation = designation;
    }

    // Getter方法...
    public String getDesignation() {
        return designation;
    }
}

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * PersonBuilder
 *
 * @author 31235
 * @version 2023/05/26 15:27
 **/
// 建造者类
public class PersonBuilder<T> {
    private Class<T> type;
    private T instance;

    public PersonBuilder(Class<T> type) {
        this.type = type;
        try {
            this.instance = type.getDeclaredConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException |
                 InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public PersonBuilder<T> withName(String name) {
        Method[] methods = type.getMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(BuilderProperty.class) &&
                    method.getName().startsWith("set") &&
                    method.getParameterCount() == 1 &&
                    method.getParameterTypes()[0] == String.class) {
                BuilderProperty annotation = method.getAnnotation(BuilderProperty.class);
                if ("name".equals(annotation.value())) {
                    try {
                        method.invoke(instance, name);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
        return this;
    }

    public PersonBuilder<T> withAge(int age) {
        Method[] methods = type.getMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(BuilderProperty.class) &&
                    method.getName().startsWith("set") &&
                    method.getParameterCount() == 1 &&
                    method.getParameterTypes()[0] == int.class) {
                BuilderProperty annotation = method.getAnnotation(BuilderProperty.class);
                if ("age".equals(annotation.value())) {
                    try {
                        method.invoke(instance, age);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
        return this;
    }

    public PersonBuilder<T> withDesignation(String designation) {
        Method[] methods = type.getMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(BuilderProperty.class) &&
                    method.getName().startsWith("set") &&
                    method.getParameterCount() == 1 &&
                    method.getParameterTypes()[0] == String.class) {
                BuilderProperty annotation = method.getAnnotation(BuilderProperty.class);
                if ("designation".equals(annotation.value())) {
                    try {
                        method.invoke(instance, designation);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
        return this;
    }

    public T build() {
        return instance;
    }
}

public static void main(String[] args) {
        Employee employee = new PersonBuilder<>(Employee.class)
                .withName("John")
                .withAge(25)
                .withDesignation("Manager")
                .build();

        System.out.println(JSONObject.toJSONString(employee));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值