从工厂方法到注解的小例子

一、背景介绍

上篇"自定义注解和注解解析器",通过小例子介绍了自定义注解的运用;本篇继续基于小例子来实现工厂方法,以及注解实现工厂方法

二、思路&方案

  • 1.米老师依赖何老师;强耦合的依赖关系图
  • 2.米老师依赖何老师;通过Class实现弱耦合的依赖关系图
  • 3.米老师依赖何老师;通过工厂方法实现自动化的弱耦合的依赖关系图
  • 4.米老师依赖何老师;通过注解替换工厂方法实现自动化的弱耦合的依赖关系图

三、过程

这里的前几个过程就不符代码了,请读者自行补充;只附上第四版,通过注解替换工厂方法实现自动化的小例子

过程图一

在这里插入图片描述

过程图二

在这里插入图片描述

过程图三

在这里插入图片描述

过程图四(运行时的图)

在这里插入图片描述

代码

package oldMark7_Anotation_v1;


/**
 * 1.每个类调用的时候,通过判断注解来进行反射对象
 *
 * 2.注解中的参数,可以通过配置文件获取(注解中的参数,可以当做类的一个属性,然后获取配置文件中的值)
 */
public class Client {
    
    


    public static void main(String[] args){
    
    
        Notice notice = new Notice();
        notice.send();
    }
}

package oldMark7_Anotation_v1;

public abstract class Executant {
    
    

    public String name;

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public abstract void OpenDoor(Executant executant);

}

package oldMark7_Anotation_v1;

public class Helaoshi extends Executant {
    
    

    public Helaoshi(String name) {
    
    
        this.name = name;
    }

    public Helaoshi(){
    
    }

    public void OpenDoor(Executant executant){
    
    
        this.privateOpenDoor(executant);
    }

    private void  privateOpenDoor(Executant executant){
    
    
        System.out.println("我是"+this.name+",我去开门了!");
    }

}

package oldMark7_Anotation_v1;

public class Milaoshi extends Executant {
    
    

    public Milaoshi(String name) {
    
    
        this.name = name;
    }

    public Milaoshi(){
    
    }


    private void privateOpenDoor(Executant executant){
    
    

        System.out.println(this.name+"调用"+executant.getName()+"开门的方法");
        executant.OpenDoor(executant);
    }

    public void OpenDoor(Executant executant){
    
    
        this.privateOpenDoor(executant);
    }

}

package oldMark7_Anotation_v1;


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

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyResource {
    
    
    String packageName();

}

package oldMark7_Anotation_v1;

import java.lang.reflect.Field;

public class MyResourceExplanationFactoryImpl {
    
    


    public static void createBean(Object object){
    
    
        createBeanPrivate(object);
    }

    private static void createBeanPrivate(Object object){
    
    
        try {
    
    
            //1.获取传进来的对象的类模板
            Class<?> clazzObj = object.getClass();

            Field[] fields = clazzObj.getFields();
            for (Field field:fields) {
    
    

                if(field.isAnnotationPresent(MyResource.class)){
    
    
                    String packageName = field.getAnnotation(MyResource.class).packageName();
                    Class<?> clazz = Class.forName(packageName);

                    Executant executant = (Executant)clazz.newInstance();

                    field.setAccessible(true);
                    field.set(object, executant);

                }
            }

        }catch (Exception e){
    
    
            e.printStackTrace();
        }

    }
}

package oldMark7_Anotation_v1;

//ui意识、无限思维-面向对象
public class Notice {
    
    

    //将packageName这个参数进行io传递(提示是否要传递,自动延时XX秒,提示是否需要修改配置文件;)良好的interface friend设计
    @MyResource(packageName = "oldMark7_Anotation_v1.Milaoshi")
    public Executant milaoshi ;

    @MyResource(packageName = "oldMark7_Anotation_v1.Helaoshi")
    public Executant helaoshi;

    public void send(){
    
    
        this.privateSend();
    }

    private void privateSend(){
    
    

        MyResourceExplanationFactoryImpl.createBean(this);
        milaoshi.setName("米老师");
        System.out.println(milaoshi.hashCode());
        System.out.println(milaoshi.toString());
        helaoshi.setName("何老师");
        milaoshi.OpenDoor(helaoshi);
    }

}

四、总结

  • 1.通过一步步的变化更加认识到变化规律的重要性,使其显得行云流水
  • 2.图对代码,代码配合图的方式强化了宏观
  • 3.图中的布局,也是一种宏观的彰显
  • 4.通过刻意练习来刻画自身通过规律提升想象力的能力

五、升华

本次变化摸索其中的规律,以及结合需求达到每一次变的效果,通过这样的训练可以达到知晓过去通晓未来的神奇。

猜你喜欢

转载自blog.csdn.net/u013030601/article/details/128605588