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

单例模式:

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

装饰器模式

相关推荐
無限進步D4 小时前
Java 运行原理
java·开发语言·入门
難釋懷4 小时前
安装Canal
java
是苏浙4 小时前
JDK17新增特性
java·开发语言
阿里加多7 小时前
第 4 章:Go 线程模型——GMP 深度解析
java·开发语言·后端·golang
likerhood8 小时前
java中`==`和`.equals()`区别
java·开发语言·python
小小李程序员8 小时前
Langchain4j工具调用获取不到ThreadLocal
java·后端·ai
zs宝来了8 小时前
AQS详解
java·开发语言·jvm
lulu121654407811 小时前
Claude Code Harness架构技术深度解析:生产级AI Agent工程化实践
java·人工智能·python·ai编程
阿里加多11 小时前
第 1 章:Go 并发编程概述
java·开发语言·数据库·spring·golang