Small example from factory method to annotation

1. Background introduction

The last article " Custom Annotations and Annotation Parsers " introduced the use of custom annotations through small examples; this article continues to implement factory methods based on small examples, and annotations to implement factory methods

2. Ideas & plans

  • 1. Mr. Mi depends on Mr. He; strong coupling dependency diagram
  • 2. Mr. Mi relies on Mr. He; the dependency diagram of weak coupling is realized through Class
  • 3. Mr. Mi relies on Mr. He; the dependency diagram of the weakly coupled automation through the factory method
  • 4. Mr. Mi relies on Mr. He; replace the factory method with annotations to realize the weakly coupled dependency diagram of automation

3. Process

The first few processes here do not conform to the code, readers are welcome to supplement; only the fourth edition is attached, a small example of automation by replacing factory methods with annotations

Process Diagram 1

insert image description here

Process Diagram 2

insert image description here

Process Figure 3

insert image description here

Process diagram four (diagram at runtime)

insert image description here

the code

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);
    }

}

Four. Summary

  • 1. Through step-by-step changes, we are more aware of the importance of the law of change, making it appear smooth
  • 2. The way of picture to code and code with picture strengthens the macro
  • 3. The layout in the picture is also a macro display
  • 4. Use deliberate practice to describe your ability to improve your imagination through regularity

Five, sublimation

Explore the laws of this change, and combine the needs to achieve the effect of each change. Through such training, you can achieve the magic of knowing the past and knowing the future.

Guess you like

Origin blog.csdn.net/u013030601/article/details/128605588