设计模式

工厂模式:

  1. 创建抽象类 abstract "classname"

  2. 创建工厂 "classname"factory

  3. 在工厂中提供一个公开方法,根据用户选择参数不同,实例化不同的抽象类子类并返回

   public "classname"(){
        private "classname";
        if(){
            "classname"=new "classname";
        }else if(){
            "classname"=new "classname";
        }else{
        }
   }
  1. BeanFactory

    public class BeanFactory {
    
     static Properties properties = new Properties();//配置类
    
     static {
         try {
             InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("bean.properties");//以流的方式加载bean配置文件(bean配置文件在src目录下)
             properties.load(in);
                 //获取流当中的键值对
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
    
     public static Object getBean(String beanName) {
         try {
             Class<?> c = Class.forName(properties.getProperty(beanName));
                 //获取properties中beanName所对应的值(即bean所属的类名)
             return c.getConstructor().newInstance();
             //将类构造并返回
                 //getConstuctor()返回该类public的构造方法
                 //newInstance()使用构造方法将该类实例化
         } catch (Exception e) {
             e.printStackTrace();
         }
         return beanName;
     }
    }
    
    

单例模式:

  1. 提供一个静态属性返回当前对象为一实例

  2. 私有化空参构造器 --无法通过外界创建

  3. 提供一个公开静态方法返回当前对象

    如:

    
    public class Computer {
        //静态属性为自身对象
     private static Computer computer;
        //私有化构造方法
     private Computer() {
     }
        //提供一个接口供外界访问返回本类唯一实例
     public static Computer getInstance() {
         if (computer == null) {
         computer = new Computer();
         }
         return computer;
     }
    }
    

Constructor:

  • getConstructors() : 获取 public的 Constructor[](public)
  • getDeclaredConstructors() : 获取 所有的 Constructor[]
  • getConstructor(parameterTypes) : 根据 参数类型(可变参数), 获取具体 Constructor(public)
  • getDeclaredConstructor(parameterTypes):根据 参数类型(可变参数), 获取具体 Constructor

以上方法:

  • getConstructors()方法

    ​ --------是获取 public的构造函数 数组

  • getDeclaredConstructors()方法

    ​ --------是获取所有的构造函数 数组

  • getConstructor(parameterTypes) 方法

    ​ --------是根据 参数类型(可以有多个Class<?>的可变参数), 获取具体public的 Constructor

  • getDeclaredConstructor(parameterTypes) 方法,

    ​ --------是根据 参数类型(可以有多个Class<?>的可变参数),获取具体的 Constructor

模板模式:

1.定义一个抽象(abstract)的顶级业务流程类

2.其中包含若干抽象方法

3.包含一个final的业务流程,按照一定逻辑调用类中的抽象方法

优点:拓展能力强

生产者消费模式:

//资源
public class Product{
    String name;
    Double price;
    boolean flag = false;
}
//消费者线程类
public class Customer extends Thread {
    private Product product;

    public Customer(String name, Product product) {
        super(name);
        this.product = product;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (product) {
                if (product.flag == false) {
                    try {
                        product.wait();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("我买了" + product.name);
                product.flag = false;
                product.notify();
            }
        }
    }
}
//生产者线程
public class Supermarket extends Thread {
    private Product product;

    public Supermarket(String name, Product product) {
        super(name);
        this.product = product;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (product) {
                if (product.flag == true) {
                    try {
                        product.wait();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("我生产了一个" + product.name + "快来买");
                product.name = "商品";
                product.flag = true;
                product.notify();
            }
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容