工作中遇到的设计模式整理

单例模式:

复制代码
public class Singleton {
    public static Singleton instance;
    private Singleton(){

    }
    public static Singleton getInstance() {
        if(instance==null){
            synchronized (Singleton.class){
                if(instance==null){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    instance=new Singleton();
                }
            }
        }
        return instance;
    }
}

工厂模式:

复制代码
interface Shape{
	void draw();
}
class Rectangle implements Shape{
	@Override
	public void draw(){
	    System.out.println("Inside Rectangle::draw() method.");
    }
}
class Circleimplements Shape{
	@Override
	public void draw(){
	    System.out.println("Inside Rectangle::draw() method.");
    }
}
class ShapeFactory(){
	Public Shape getShape(String shapeType){
	    if(shapeType==null){
	        return null;
        }
        if(shapeType.equals("circle")){
	        return new Circle();
        }else if(shapeType.equals("rectangle")){
	        return new Rectangle();
        }
        return null;
    }
}

代理模式

jdk动态代理,实现接口的类

cglib动态代理

策略模式

ability

装饰器模式

相关推荐
鲨鱼辣钊1 小时前
【FastAPI筑基-Day19】APScheduler定时任务全实战|自动执行、动态启停、后台常驻
java·spring·fastapi
学长毕业设计5 小时前
基于SpringBoot的公益基金管理系统(源码+文档+讲解视频)
java·spring boot·后端
东小西6 小时前
【SAA实战】第 3 篇 · 工具调用全攻略:把业务能力交给 Agent 自己调度
java·后端·spring
东小西6 小时前
【SAA实战】第 4 篇 · Agent 短期记忆:saver 让 Agent 跨轮记得住(threadId 隔离)
java·后端·spring
许彰午6 小时前
22-DataCenter报文序列化
java·低代码·架构·状态模式
2601_962065256 小时前
[MySQL] SQL优化之性能分析
java·sql·mysql
小范同学_6 小时前
JDK1.7 与 JDK1.8 HashMap 底层原理对比 + 数组并发扩容死循环详解
java·开发语言
予昊7 小时前
从零实现“在线五子棋对战“:WebSocket 实时通信 + 段位匹配
java·开发语言·网络·websocket
2601_962203517 小时前
【SpringAI入门】初识SpringAI
java
weixin_461408588 小时前
Mybatis-flex小记
java·开发语言·mybatis