数据库教程 设计模式(单例模式、代理模式、工厂模式)

Java中常用的设计模式:
概况:
1、单例模式
饱汉式
饿汉式
内部类实现
2、工厂模式
3、代理模式
静态代理
动态代理
num1:单例模式
1.1 饱汉式
public class Singleton { private static final Student STUDENT = new Student(); // 私有的构造函数,防止实例化 private Singleton() { } public Student setStudent() { return STUDENT; } }
1.2饿汉式
public class Singleton { private static Student STUDENT = null; // 私有的构造函数,防止实例化 private Singleton() { } public Student setStudent() { if (STUDENT == null) { STUDENT = new Student(); } return STUDENT; } }
1.3 内部类实现
public class Singleton { // 创建内部类 private static class newStudent { private static final Student STUDENT = new Student(); } // 私有的构造函数,防止实例化 private Singleton() { } // 调用内部类 public static Student getStudent() { return newStudent.STUDENT; } }
关于饱汉式单例设计模式、饿汉式单例设计模式 以及 内部类实现单例模式模式 的对比:
1、饱汉式单例设计模式:线程安全,不使用也加载
2、饿汉式单例设计模式:线程不安全,延迟加载,可以通过加 synchronized 关键字来实现线程安全
3、内部类实现单例模式:不适用不加载(减少使用内存),线程式安全的
更多的方法,增加关键字,双检锁
num2:工厂模式
设计模式之工厂模式:就是把创建实例交个一个指定的类(工厂类)进行实现,至于工厂类怎么实现,我不管
num3:代理模式
1.1静态代理
1.2动态代理
运用的方法:
第一个参数:类加载器
第二个参数:动态代理要实现的接口
第三个参数:拦截器的作用(拦截所代理类的行为)
public static Object newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h) { Objects.requireNonNull(h); @SuppressWarnings("removal") final Class<?> caller = System.getSecurityManager() == null ? null : Reflection.getCallerClass(); /* * Look up or generate the designated proxy class and its constructor. */ Constructor<?> cons = getProxyConstructor(caller, loader, interfaces); return newProxyInstance(caller, cons, h); }
实例化:
Global 类(全局变量类):
package com.lgf.shejimoshi; import java.sql.Connection; /** * 全局的变量或方法 */ public class Global { public static final String url = "jdbc:mysql://localhost:3306/employee?charaterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimeZone=Asia/Shanghai"; public static final String username = "root"; public static final String password = "123456"; /** * 获取数据库连接 * * @return */ public static Connection getConnection() { return JdbcUtils.getConnection(url, username, password); } }
连接池类
package com.lgf.shejimoshi; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Connection; import java.util.ArrayList; import java.util.List; /** * 数据库链接池 * * @author liuguangfa * */ public class MySql { private final int databaseMin = 5; private final int datebaseMax = 15; List<PooledDatabase> conns = new ArrayList<>(); /** * 创建数据库链接 */ public Connection getConnection() { return Global.getConnection(); } /** * 初始化链接池 */ public void iniPool() { for (int i = 0; i < databaseMin; i++) { Connection conn = getConnection(); // 创建实例 PooledDatabase pd = new PooledDatabase(); pd.setUsed(true); pd.setConnectoion(conn); // conns.add(pd); } } /** * 链接池中的连接代理 */ public void buildConnectionProxy(PooledDatabase pd) { Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { Connection.class }, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().equals("close")) { return null; } else { return method.invoke(pd, args); } } }); } /** * 从数据库池中拿链接 */ public Connection gtConnection() { for (int i = 0; i < conns.size(); i++) { if (!conns.get(i).isUsed()) { conns.get(i).setUsed(true); return conns.get(i).getConnectoion(); } } // 数据库连接不够了,小于最大的数据库连接,创建数据库 if (conns.size() < datebaseMax) { Connection conn = getConnection(); PooledDatabase ps = new PooledDatabase(); ps.setConnectoion(conn); ps.setUsed(true); conns.add(ps); return ps.getConnectoion(); } throw new RuntimeException("数据库已经达到最大的连接数"); } /** * 判断链接池中的链接是否在使用 */ public static class PooledDatabase { private Connection connectoion; private boolean used = false; public Connection getConnectoion() { return connectoion; } public void setConnectoion(Connection connectoion) { this.connectoion = connectoion; } public boolean isUsed() { return used; } public void setUsed(boolean used) { this.used = used; } } }
作者为:刘广法,网站地址:https://liuguangfa.com/