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

单例模式:

复制代码
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 小时前
考研论坛平台|考研论坛小程序系统|基于java和微信小程序的考研论坛平台小程序设计与实现(源码+数据库+文档)
java·vue.js·spring boot·考研·小程序·毕设·考研论坛平台小程序
CHEN5_021 小时前
leetcode-hot100 11.盛水最多容器
java·算法·leetcode
songx_991 小时前
leetcode18(无重复字符的最长子串)
java·算法·leetcode
在路上`2 小时前
前端学习之后端java小白(三)-sql外键约束一对多
java·前端·学习
dazhong20122 小时前
Spring Boot 项目新增 Module 完整指南
java·spring boot·后端
xrkhy3 小时前
SpringBoot之日志处理(logback和AOP记录操作日志)
java·spring boot·logback
搬山境KL攻城狮3 小时前
MacBook logback日志输出到绝对路径
java·intellij-idea·logback
yb0os13 小时前
RPC实战和核心原理学习(一)----基础
java·开发语言·网络·数据结构·学习·计算机·rpc
liuyao_xianhui3 小时前
内存管理(C/C++)
java·开发语言·c++
superlls3 小时前
(设计模式)区分建造者、 规格模式(MyBatis Example+Criteria )
java·tomcat