1. 单例模式 (Singleton)
作用:确保类只有一个实例,并提供全局访问点。
java
public class Singleton {
private static Singleton instance;
private Singleton() {} // 私有构造器
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
2. 工厂模式 (Factory)
作用:创建对象时不暴露实例化逻辑。
java
interface Shape {
void draw();
}
class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing Circle");
}
}
class ShapeFactory {
public Shape getShape(String type) {
if ("circle".equalsIgnoreCase(type)) {
return new Circle();
}
return null;
}
}
3.抽象工厂模式:创建多个相关产品的家族
- 只需实现新的具体工厂类
java
// 抽象产品:UI组件
interface UIComponent {
void render();
}
// 具体产品:移动端
class MobileButton implements UIComponent {
public void render() { System.out.println("Mobile button rendered"); }
}
class MobileTextField implements UIComponent {
public void render() { System.out.println("Mobile text field rendered"); }
}
// 具体产品:Web端
class WebButton implements UIComponent {
public void render() { System.out.println("Web button rendered"); }
}
class WebTextField implements UIComponent {
public void render() { System.out.println("Web text field rendered"); }
}
// 抽象工厂
interface UIFactory {
UIComponent createButton();
UIComponent createTextField();
}
// 具体工厂
class MobileUIFactory implements UIFactory {
public UIComponent createButton() { return new MobileButton(); }
public UIComponent createTextField() { return new MobileTextField(); }
}
class WebUIFactory implements UIFactory {
public UIComponent createButton() { return new WebButton(); }
public UIComponent createTextField() { return new WebTextField(); }
}
// 客户端代码
class Application {
private UIFactory factory;
public Application(UIFactory factory) {
this.factory = factory;
}
public void buildUI() {
UIComponent button = factory.createButton();
UIComponent textField = factory.createTextField();
button.render();
textField.render();
}
}
// 根据平台配置使用不同工厂
public class PlatformApp {
public static void main(String[] args) {
String platform = "web"; // 从配置读取
UIFactory factory;
if ("mobile".equals(platform)) {
factory = new MobileUIFactory();
} else {
factory = new WebUIFactory();
}
Application app = new Application(factory);
app.buildUI();
}
}
对比项 | 工厂方法模式 | 抽象工厂模式 |
---|---|---|
关注点 | 单个产品 | 多个产品组成的家族 |
接口数量 | 一个工厂接口 + 一个产品接口 | 一个工厂接口 + 多个产品接口 |
扩展难度 | 新增产品容易 | 新增产品种类难,新增产品族容易 |
适用范围 | 简单对象创建 | 多种对象协同工作的场景 |
4. 建造者模式 (Builder)
作用:分步构建复杂对象。
java
class Computer {
private String CPU;
private String RAM;
// 使用Builder构建
private Computer(Builder builder) {
this.CPU = builder.CPU;
this.RAM = builder.RAM;
}
public static class Builder {
private String CPU;
private String RAM;
public Builder setCPU(String cpu) {
this.CPU = cpu;
return this;
}
public Builder setRAM(String ram) {
this.RAM = ram;
return this;
}
public Computer build() {
return new Computer(this);
}
}
}
// 使用
Computer comp = new Computer.Builder()
.setCPU("Intel i7")
.setRAM("16GB")
.build();
5. 原型模式 (Prototype)
作用:通过克隆现有对象创建新对象。
java
class Sheep implements Cloneable {
private String name;
public Sheep(String name) {
this.name = name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
// 使用
Sheep original = new Sheep("Dolly");
Sheep cloned = (Sheep) original.clone();
6. 适配器模式 (Adapter)
作用:使不兼容接口协同工作。
java
class LegacyPrinter {
void printDocument() {
System.out.println("Legacy Printing");
}
}
interface ModernPrinter {
void print();
}
class PrinterAdapter implements ModernPrinter {
private LegacyPrinter legacyPrinter;
public PrinterAdapter(LegacyPrinter printer) {
this.legacyPrinter = printer;
}
@Override
public void print() {
legacyPrinter.printDocument(); // 适配调用
}
}
7. 责任链模式 (Chain of Responsibility)
作用:将请求沿处理链传递。
java
abstract class Logger {
public static int INFO = 1;
public static int DEBUG = 2;
protected int level;
protected Logger nextLogger;
public void setNextLogger(Logger next) {
this.nextLogger = next;
}
public void logMessage(int level, String message) {
if (this.level <= level) {
write(message);
}
if (nextLogger != null) {
nextLogger.logMessage(level, message);
}
}
abstract protected void write(String message);
}
class ConsoleLogger extends Logger {
public ConsoleLogger(int level) {
this.level = level;
}
protected void write(String message) {
System.out.println("Console: " + message);
}
}