一句话总结设计模式-《大化设计模式》读书笔记

设计模式并不是一种算法,而是一种思想,一种软件开发思想;这种思想便于开发的维护、扩展。

  • 基于创建型
    • 单例模式:通过锁机制或者单一加载机制,确保程序只创建一个对象。
    • 工厂模式:将对象的实例化交给第三者-工厂类
    • 抽象工厂:在工厂模式的基础上继续加一层,有多个工厂类。通过一个工厂接口,将所有的工厂统一起来
  • 基于结构型
    • 装饰器:通过继承同一个接口对功能进行增强
    • 适配器:对功能不同的接口进行聚合,能够在一个适配器中全部完成
    • 代理模式:通过代理类来调用具体类的动作。与工厂类不同的是:工厂类用于创建对象,代理类是调用对象的方法,代替执行任务(因而也可能需要创建对象)
  • 基于行为:
    • 模板模式:定义一个抽象类模板,包络不可更改的方法和可更改的方法,不可更改的就是规定的模板,然后继承者只需要实现具体的行为,而固定的模式不可更改
    • 策略模式:一个接口的方法,在继承者中有多种不同的实现方式,使用时,实现方式不同就需要更改对应的对象和方法名,造成大量的代码修改。因而可以把这些封装为一个策略,调用时,值更改策略对象参数就行。


/**
 * @description: 1.1多线程单列-懒汉式-用时再创建
 * @author: zoutai
 * @create: 2019/4/11
 **/
public class SingletonCon {
    // 全局访问、volatile禁止重排序
    private volatile static SingletonCon instance;
    // 构造函数私有
    private SingletonCon(){}
    // 实例化函数类函数
    public static SingletonCon getInstance(){
        // 判断是否为null,只有为null是才加锁,否则不加锁,直接返回实例;提高效率
        if(instance==null){
            synchronized (SingletonCon.class){
                // 在上一步判断为null和加锁期间,其他线程可能已经实例化了对象,所以需要再次判断
                if(instance==null){
                    instance = new SingletonCon();
                }
            }
        }
        return instance;
    }
}
/**
 *
 * 补充说明volatile禁止重排序:对于构造语句 instance = new SingletonCon();
 * 在构造时,CPU会对进行语句优化,进行重排序,先对instance赋值,然后才实例化对象;这样就instance不为null,但是却没有实例化对象。
 内部重排序指令如下:
 inst = allocat(); // 分配内存
 sSingleton = inst;      // 赋值
 constructor(inst); // 真正执行构造函数
 *
 **/
 
 
/**
 * @description: 1.2 单例模式-饿汉式
 * @author: zoutai
 * @create: 2019/4/11
 **/
public class SingletonStatic {
    // 类加载时就初始化,只初始化一次
    private static SingletonStatic instance = new SingletonStatic();
    private SingletonStatic(){}
    public static SingletonStatic getInstance(){
        return instance;
    }
}

// 2 工厂模式:
// 线程池接口
public interface MyThreadPool {
    void createThead();
}
// 第一种线程池
public class SingleThreadPool implements MyThreadPool{
    @Override
    public void createThead() {
        System.out.println("线程个数只有一个的线程池:");
    }
}
// 第二种线程池
public class FixedThreadPool implements MyThreadPool{
    @Override
    public void createThead() {
        System.out.println("线程个数固定的线程池:");
    }
}

// 工厂创建
public class ThreadFactory {
    // 工厂方法创建-参考自Executors类
    public MyThreadPool newThread(int num){
        if(num==1){
            return new SingleThreadPool();
        } else {
            return new FixedThreadPool();
        }
    }
}

public class TestFactory {
    public static void main(String[] args) {
        ThreadFactory threadFactory = new ThreadFactory();
        MyThreadPool singleThread = threadFactory.newThread(1);
        singleThread.createThead();
        MyThreadPool fixedThread = threadFactory.newThread(10);
        fixedThread.createThead();
    }
}

// 3 抽象工厂模式
// 创建用户工厂
public interface User {
    void createUser();
}

public class StudentUser implements User {
    @Override
    public void createUser() {
        System.out.println("学生用户");
    }
}

public class TeacherUser implements User{
    @Override
    public void createUser() {
        System.out.println("老师用户");
    }
}

// 创建线程池工厂同上(省略)

// 创建抽象工厂,集合所有的工厂
public interface AbstractFactory {
    MyThreadPool newThread();
    User newUser();
}

public class SingleFactory implements AbstractFactory {
    @Override
    public MyThreadPool newThread() {
        return new FixedThreadPool();
    }

    @Override
    public User newUser() {
        return new TeacherUser();
    }
}

public class FixedFactory implements AbstractFactory{
    @Override
    public MyThreadPool newThread() {
        return new SingleThreadPool();
    }

    @Override
    public User newUser() {
        return new StudentUser();
    }
}

public class TestAbstractFactory {
    public static void main(String[] args) {
        AbstractFactory factory = new SingleFactory();
        MyThreadPool singleThread = factory.newThread();
        singleThread.createThead();
        User user = factory.newUser();
        user.createUser();
    }
}

// 4.装饰器
// 定义输入流接口
public interface InputStream {
    void read();
}
// 文件类型输入流
public class FileInputStream implements InputStream{
    @Override
    public void read() {
        System.out.println("fileInputStream:");
    }
}
// 装饰器:为所有的输入流 增添新的功能
public abstract class FilterInputStream implements InputStream{
    protected InputStream inputStream;

    public FilterInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    @Override
    public void read() {
        inputStream.read();
    }
}
// 缓冲装饰器:添加具有缓冲功能的装饰器
public class BufferedInputStream extends FilterInputStream {
    public BufferedInputStream(InputStream inputStream) {
        super(inputStream);
    }

    @Override
    public void read() {
        super.read();
        readBuffered();
    }

    private void readBuffered() {
        System.out.println("buffered read");
    }
}

public class TestDecorator {
    public static void main(String[] args) {
        FileInputStream fi = new FileInputStream();
        System.out.println("1、只有文件读功能:");
        fi.read();

        System.out.println("-----------");

        System.out.println("2、加入缓冲功能");
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream());
        bis.read();
    }
}

// 5 适配器
// 副接口:后置通知模板-参考AOP
// 适配者
public interface AfterAdvice {
    void after();
}
// 接口after的拦截器
public class MethodAfterAdvice implements AfterAdvice {
    @Override
    public void after() {
        System.out.println("MethodAfterAdvice:");
    }
}

// 主接口
public interface BeforeAdvice {
    void before();
}
// 具体的拦截器
public class MathodBeforeAdvice implements BeforeAdvice {
    @Override
    public void before() {
        System.out.println("MathodBeforeAdvice:");
    }
}
// 适配器:适配接口通过实例对象调用
public class MethodBeforeAdviceAdapter implements BeforeAdvice{
    AfterAdvice afterAdvice;

    public MethodBeforeAdviceAdapter(int type) {
        if(type>0){
            afterAdvice=new MethodAfterAdvice();
        }
    }

    @Override
    public void before() {
        if(afterAdvice!=null){
            afterAdvice.after();
        }else {
            System.out.println("MathodBeforeAdvice:");
        }
    }
}

// 6 代理模式
public interface Task {
    void dealTask(String taskName);
}
public class PostTask implements Task {
    @Override
    public void dealTask(String taskName) {
        System.out.println("post task :" + taskName);
    }
}
// 代理类,实现被代理者的内部逻辑
public class ProxyTask implements Task {
    private Task task;

    @Override
    public void dealTask(String taskName) {
        if(task==null){
            task = new PostTask();
        }
        System.out.println("代理间接调用");
        task.dealTask(taskName);
    }
}
public class TestProxy {
    public static void main(String[] args) {
        Task task = new ProxyTask();
        task.dealTask("打印开始日志");
    }
}

// 7 模板类
public abstract class JdbcTemplate {
    // 具体的方式自己实现
    abstract void getConnection();
    abstract void doTask();
    abstract void releaseConnection();

    // 固定业务逻辑顺序-不可变
    public void execute(){
        getConnection();
        doTask();
        releaseConnection();
    }
}
public class MyJdbcTemplate extends JdbcTemplate{
    @Override
    void getConnection() {
        System.out.println("getConnection:");
    }

    @Override
    void doTask() {
        System.out.println("doTask");
    }

    @Override
    void releaseConnection() {
        System.out.println("releaseConnection");
    }
}

// 8 策略模式
public interface Strategy {
    int cal(int num1,int num2);
}
// 各种策略实现
public class AddStrategy implements Strategy {
    @Override
    public int cal(int num1, int num2) {
        return num1 + num2;
    }
}
public class SubStrategy implements Strategy {
    @Override
    public int cal(int num1, int num2) {
        return num1 - num2;
    }
}
// 策略类
public class Context {
    private Strategy strategy;

    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    public int execute(int num1, int num2){
        return strategy.cal(num1,num2);
    }
}
public class testStrategy {
    public static void main(String[] args) {
        Context context = new Context(new AddStrategy());
        System.out.println(context.execute(1,2));
        context = new Context(new SubStrategy());
        System.out.println(context.execute(1,2));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值