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

单例模式:

复制代码
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

装饰器模式

相关推荐
工具罗某人19 小时前
docker快速部署kafka
java·nginx·docker
秋饼20 小时前
【手撕 @EnableAsync:揭秘 SpringBoot @Enable 注解的魔法开关】
java·spring boot·后端
Good_Starry20 小时前
Java——正则表达式
java·开发语言·正则表达式
萤丰信息20 小时前
开启园区“生命体”时代——智慧园区系统,定义未来的办公与生活
java·大数据·运维·数据库·人工智能·生活·智慧园区
欧洵.20 小时前
Java.基于UDP协议的核心内容
java·开发语言·udp
xunyan623420 小时前
第九章 JAVA常用类
java·开发语言
沛沛老爹20 小时前
Web开发者进阶AI:Agent技能设计模式之迭代分析与上下文聚合实战
前端·人工智能·设计模式
China_Yanhy20 小时前
AWS S3 深度配置指南:每一栏每个选项有什么作用
java·数据库·aws
秃了也弱了。20 小时前
FASTJSON库:阿里出品java界json解析库,使用与踩坑记录
java·开发语言·json
安全渗透Hacker20 小时前
参数未校验导致的DOS(服务拒绝)问题典型场景
java·安全·web安全·网络安全·安全性测试