news 2026/5/13 10:44:19

常见设计模式

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
常见设计模式

AI时代,做一个古法编程逆行者:)
最近vibecoding太多,手写一会儿代码,还别有一番风味~


单例模式:
懒汉式:

public class Singleton{ private static volatile Singleton instance; private Singleton(){}; public static Singleton getInstance(){ if(instance==null){ synchronized(Singleton.class){ if(instance==null){ instance = new Singleton(); } } } return instance; } }

饿汉式:

public class Singleton{ private static final Singleton instance = new Singleton(); private Singleton(){}; public static Singleton getInstance(){ return instance; } }

静态内部类:

public class Singleton{ private Singleton(){}; private static class Create{ static final Singleton instance = new Singleton(); } public static Singleton getInstance(){ return Create.instance; } }

工厂模式:
简单工厂模式:

interface Button{ void create(); } public class WindowsButton implements Button{ public void create(){ System.out.println("Windows按钮创建成功!"); } } public class MacButton implements Button{ public void create(){ System.out.println("Mac按钮创建成功!"); } } public class ButtonFactory{ public static Button createButton(String type){ if("Windows".equalsIgnoreCase(type)){ return new windowsButton; } else if("Mac".equalsIgnoreCase(type)){ return new macButton; } else{ throw new IllegalArgumentException("暂不支持该平台类型"+type); } } }

工厂方法模式:复用上方的Button接口和两种实现类,将工厂解耦,这样添加商品时,只需要增加类,不需要修改旧类。

interface ButtonFactory{ Button create(); } public class WindowsButtonFactory implements ButtonFactory{ public Button create(){ return new WindowsButton(); } } public class MacButtonFactory implements ButtonFactory{ public Button create(){ return new MacButton(); } }

抽象工厂模式:就是在工厂方法里面返回产品族。

interface Checkbox{ void create(); } public class WindowsCheckbox implements Checkbox{ public void create(){ System.out.println("Windows复选框创建成功!"); } } public class MacCheckbox implements Checkbox{ public void create(){ System.out.println("Mac复选框创建成功!"); } } interface AbstractFactory{ Button createButton(); Checkbox createCheckbox(); } public class WindowsFactory implements AbstractFactory{ public WindowsButton createButton(){ return new WindowsButton(); } public WindowsCheckbox createButton(){ return new WindowsCheckbox(); } } public class MacFactory implements AbstractFactory{ public MacButton createButton(){ return new MacButton(); } public MacCheckbox createCheckbox(){ return new MacCheckbox(); } }

策略模式:

interface PayStrategy{ void pay(int amount); } class WeChat implements PayStrategy{ public void pay(int amount){ System.out.println("微信支付成功!"); } } class Credit implements PayStrategy{ public void pay(int amount){ System.out.println("信用卡支付成功!"); } } class Pay{ private PayStrategy strategy; public void setPayStrategy(PayStrategy strategy){ this.strategy = strategy; } public void checkOut(int amount){ strategy.pay(amount); } }

责任链模式:

abstract class Approver{ protected Approver next; public abstract void handleRequest(int leaveDays); public void setNext(Approver next){ this.next = next; } } class Boss extends Approver{ public void handleRequest(int leaveDays){ System.out.println("通过"); } } class Manager extends Approver{ public void handleRequest(int leaveDays){ if(leaveDays<7){ System.out.println("通过"); } else if(next!=null){ next.handleRequest(leaveDays); } } } class GroupLeader extends Approver{ public void handleRequest(int leaveDays){ if(leaveDays<2){ System.out.println("通过"); } else if(next!=null){ next.handleRequest(leaveDays); } } } class Main{ public static void main(String[] args){ Boss a = new Boss(); Manager b = new Manager(); GroupLeader c = new GroupLeader(); c.setNext(b); b.setNext(a); c.handleRequest(7); } }

更新补充
装饰器模式:

abstract class MyInputStream{ abstract public void run(); } class FileInputStream extends MyInputStream{ @Override public void run(){ System.out.println("文件输入流"); } } abstract class FilterInputStream extends MyInputStream{ private MyInputStream in; public FilterInputStream(MyInputStream in){ this.in = in; } @Override public void run(){ in.run(); } } class BufferInputStream extends FilterInputStream{ public BufferInputStream(MyInputStream in){ super(in); } @Override public void run(){ super.run(); System.out.println("输入缓冲池"); } } class EpInputStream extends FilterInputStream{ public EpInputStream(MyInputStream in){ super(in); } @Override public void run(){ super.run(); System.out.println("加密处理"); } } class Main{ public static void main(String[] args){ FileInputStream in = new FileInputStream(); BufferInputStream bi = new BufferInputStream(in); EpInputStream ei = new EpInputStream(bi); ei.run(); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/13 10:39:14

lucky-commit 性能优化秘籍:GPU 加速 vs CPU 多线程

lucky-commit 性能优化秘籍&#xff1a;GPU 加速 vs CPU 多线程 【免费下载链接】lucky-commit Customize your git commit hashes! 项目地址: https://gitcode.com/gh_mirrors/lu/lucky-commit lucky-commit 是一款强大的 Git 提交哈希自定义工具&#xff0c;它能帮助开…

作者头像 李华
网站建设 2026/5/13 10:37:10

基于python-telegram-bot的审批按钮系统设计与实现

1. 项目概述&#xff1a;一个为Telegram机器人设计的审批按钮系统如果你在团队协作、内容审核或者自动化流程中&#xff0c;经常需要通过Telegram机器人来处理“同意”或“拒绝”这类审批请求&#xff0c;那么你很可能遇到过这样的困扰&#xff1a;用户发来一条需要审核的消息&…

作者头像 李华
网站建设 2026/5/13 10:34:24

2026届学术党必备的五大AI科研神器实测分析

Ai论文网站排名&#xff08;开题报告、文献综述、降aigc率、降重综合对比&#xff09; TOP1. 千笔AI TOP2. aipasspaper TOP3. 清北论文 TOP4. 豆包 TOP5. kimi TOP6. deepseek 降 AI 指令&#xff0c;是一种合规优化工具&#xff0c;用于调试 AI 生成逻辑&#xff0c;以…

作者头像 李华
网站建设 2026/5/13 10:31:29

从零部署Discord AI聊天机器人:基于ChatGPT API与Firestore的实践指南

1. 项目概述&#xff1a;打造一个属于你自己的Discord AI聊天机器人 如果你在运营一个Discord社区&#xff0c;无论是游戏公会、技术讨论组还是兴趣社团&#xff0c;肯定遇到过这样的场景&#xff1a;成员们总有一些稀奇古怪的问题&#xff0c;或者需要一个随时在线的“智能助…

作者头像 李华
网站建设 2026/5/13 10:31:12

快速解决Windows文件占用问题:PowerToys File Locksmith终极指南

快速解决Windows文件占用问题&#xff1a;PowerToys File Locksmith终极指南 【免费下载链接】PowerToys Microsoft PowerToys is a collection of utilities that supercharge productivity and customization on Windows 项目地址: https://gitcode.com/GitHub_Trending/po…

作者头像 李华
网站建设 2026/5/13 10:28:36

ChatYuan模型压缩技术:INT4量化实现400M轻量化推理的终极指南

ChatYuan模型压缩技术&#xff1a;INT4量化实现400M轻量化推理的终极指南 【免费下载链接】ChatYuan ChatYuan: Large Language Model for Dialogue in Chinese and English 项目地址: https://gitcode.com/gh_mirrors/ch/ChatYuan 在当今大语言模型快速发展的时代&…

作者头像 李华