1. 详解java中的23种设计模式
Java 中的 23 种设计模式是由 GoF(Gang of Four,四人帮)在《设计模式:可复用面向对象软件的基础》一书中总结的。这些模式分为三大类:创建型模式、结构型模式和行为型模式。以下是详细解释:
一、创建型模式
创建型模式关注对象的创建过程,以实现创建过程与使用对象的代码分离。
单例模式(Singleton)
确保一个类只有一个实例,并提供全局访问点。
应用场景:数据库连接池、线程池、缓存等。实现方式:懒汉式、饿汉式、双重校验锁、静态内部类、枚举。
public class Singleton { private static Singleton instance; private Singleton() {} public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
工厂方法模式(Factory Method)
定义一个创建对象的接口,由子类决定实例化哪一个类。
应用场景:日志记录器、连接器等。public interface Product {} public class ConcreteProduct implements Product {} public interface Factory { Product createProduct(); } public class ConcreteFactory implements Factory { public Product createProduct() { return new ConcreteProduct(); } }
抽象工厂模式(Abstract Factory)
提供创建一系列相关或相互依赖对象的接口,而无需指定它们的具体类。
应用场景:跨平台应用开发。public interface GUIFactory { Button createButton(); Checkbox createCheckbox(); }
建造者模式(Builder)
将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建不同的表示。
应用场景:复杂对象(如 JSON 解析器、报表生成器)。public class ProductBuilder { private Product product = new Product(); public ProductBuilder setPartA(String partA) { product.setPartA(partA); return this; } public Product build() { return product; } }
原型模式(Prototype)
通过复制现有对象来创建新对象,而不是通过实例化类。
应用场景:对象的克隆。public class Prototype implements Cloneable { public Prototype clone() throws CloneNotSupportedException { return (Prototype) super.clone(); } }
二、结构型模式
结构型模式关注对象和类之间的组合,形成更大的结构。
适配器模式(Adapter)
将一个类的接口转换为客户端希望的另一个接口。
应用场景:老系统兼容性改造。public class Adapter extends Adaptee implements Target { public void request() { specificRequest(); } }
桥接模式(Bridge)
将抽象部分与其实现部分分离,以便它们可以独立变化。
应用场景:UI 框架。public abstract class Shape { protected Color color; public Shape(Color color) { this.color = color; } }
组合模式(Composite)
将对象组合成树状结构以表示“部分-整体”的层次结构。
应用场景:文件系统。public interface Component { void operation(); } public class Composite implements Component { private List<Component> children = new ArrayList<>(); public void add(Component component) { children.add(component); } }
装饰器模式(Decorator)
动态地给对象添加新的功能。
应用场景:Java I/O 流。public class Decorator implements Component { private Component component; public Decorator(Component component) { this.component = component; } }
外观模式(Facade)
提供一个统一的接口,简化子系统的使用。
应用场景:系统模块化。public class Facade { private SubSystem1 sub1 = new SubSystem1(); public void operation() { sub1.doSomething(); } }
享元模式(Flyweight)
共享对象以减少内存消耗。
应用场景:对象池。public class Flyweight { private String intrinsicState; }
代理模式(Proxy)
为其他对象提供代理以控制访问。
应用场景:权限控制、延迟加载。public class Proxy implements Subject { private RealSubject realSubject; public void request() { if (realSubject == null) { realSubject = new RealSubject(); } } }
三、行为型模式
行为型模式关注对象间的交互。
责任链模式(Chain of Responsibility)
将请求沿着处理者链传递,直到被处理。
应用场景:日志记录。public abstract class Handler { protected Handler next; public void setNext(Handler next) { this.next = next; } }
命令模式(Command)
将请求封装为对象,便于存储和传递。
应用场景:任务队列。public class Command { private Receiver receiver; public void execute() { receiver.action(); } }
解释器模式(Interpreter)
定义一种语言的文法,并解析句子。
应用场景:编译器。迭代器模式(Iterator)
提供一种顺序访问集合的方法,而不暴露其内部结构。
应用场景:集合框架。中介者模式(Mediator)
通过中介者简化对象间的通信。
应用场景:聊天室。备忘录模式(Memento)
保存对象的状态以便以后恢复。
应用场景:撤销功能。观察者模式(Observer)
当对象状态发生变化时通知依赖对象。
应用场景:事件驱动。状态模式(State)
允许对象在内部状态改变时改变其行为。
应用场景:状态机。策略模式(Strategy)
定义一系列算法,将每个算法封装起来。
应用场景:支付方式。模板方法模式(Template Method)
定义算法骨架,将具体步骤延迟到子类实现。
应用场景:框架设计。访问者模式(Visitor)
为对象结构中的元素添加新的操作,而不改变其结构。
应用场景:语法树操作。
这些模式旨在解决常见的软件设计问题,并提高代码的可复用性、可维护性和扩展性。
文档信息
- 本文作者:Marshall