spring-设计模式

创建型模式

结构型模式

行为型模式

策略模式

首先,定义一个策略接口:

java 复制代码
public interface Strategy {
    public void draw(int radius, int x, int y);
}

然后定义具体的几个策略:

java 复制代码
public class RedPen implements Strategy {
    @Override
    public void draw() {
        System.out.println("red pen");
    }
}

public class YellowPen implements Strategy {
    @Override
    public void draw() {
        System.out.println("yellow pen");
    }
}

public class BluePen implements Strategy {
    @Override
    public void draw() {
        System.out.println("blue pen");
    }
}

使用策略的类:

java 复制代码
public class Context {
    private Strategy strategy;
    
    public Context(Strategy strategy) {
        this.strategy = strategy;
    }
    
    public int excuteDraw(int radius, int x, int y) {
        return strategy.drwa(radius, x, y);
    }
}

客户端演示:

java 复制代码
public static void main(String[] args) {
    
    // 使用蓝笔画画
    Context context = new Context(new BluePen());
    context.excuteDraw(84, 62, 063);
}

图示:

观察者模式

首先,定义主题,每个主题需要持有观察者列表的引用,用于在数据变更的时候通知各个观察者:

java 复制代码
public Class Subject {
    private List<Observer> observerList = new ArrayList<>();
    private int state;
    
    public int getState() {
        return state;
    }
    public void setState(int state) {
        this.state = state;

        // 数据变更 通知观察者们
        notifyAll();
    }
    
    // 注册观察者
    public void addObserver(Observer observer) {
        observerList.add(observer);
    }
    
    // 通知观察者们
    public void notifyAll() {
        for (Observer observer : observerList) {
            observer.update();
        }
    }
}

定义观察者接口:

java 复制代码
public abstract class Observer {
    protected Subject subject;
    public abstract void update();
}

定义具体的2个观察者类:

java 复制代码
// 二进制观察者
public class BinaryObserver extends Observer {

    public BinaryObserver(Subject subject) {
        this.subject = subject;
        this.subject.addObserver(this);
    }
    
    // 本方法由主题类在数据变更时进行调用
    @Override
    public void update() {
        System.out.println("数据变化!对应的二进制变为:result");
    }
    
}


// 十六进制观察者
public class HexaObserver extends Observer {

    public HexaObserver(Subject subject) {
        this.subject = subject;
        this.subject.addObserver(this);
    }

    @Override
    public void update() {
        System.out.println("数据变化!对应的十六进制变为:result");
    }

}

客户端演示:

java 复制代码
public static void main(String[] args) {
    
    // 先定义一个主题
    Subject subject = new Subject();
    
    // 定义观察者
    new BinaryObserver(subject);
    new HexaObserver(subject);
    
    // 模拟数据变更 这个时候 观察者们的uodate方法会被调用
    subject.setState(8462063);
}
相关推荐
unique_perfect11 小时前
vue2与springboot实现deepseek打印机聊天
spring boot·websocket·ai·vue2·deepseek
哈哈老师啊12 小时前
Springboot学生综合测评系统hxtne(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·数据库·spring boot
气π14 小时前
【JavaWeb】——(若依 + AI)-基础学习笔记
java·spring boot·笔记·学习·java-ee·mybatis·ruoyi
老华带你飞15 小时前
列车售票|基于springboot 列车售票系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·spring
汝生淮南吾在北15 小时前
SpringBoot+Vue在线考试系统
vue.js·spring boot·后端·毕业设计·毕设
script.boy16 小时前
基于spring boot校园二手交易平台的设计与实现
java·spring boot·后端
猿与禅17 小时前
Spring Boot 4.0 完整核心特性及实践指南
java·spring boot·后端·spring·重大升级·springboot4.0
曲莫终17 小时前
SpringBoot使用AutoConfiguration自动配置Bean
spring boot
E***U94518 小时前
从新手到入门:如何判断自己是否真的学会了 Spring Boot
数据库·spring boot·后端
invicinble18 小时前
javase-异常体系
开发语言·spring boot