1. 引言
面向对象设计模式是软件开发中一种重要的知识体系,它通过总结前人的经验,为解决常见问题提供了一系列的解决方案。本文将深入解析23种面向对象设计模式,并结合实际应用进行实战讲解。
2. 设计模式概述
设计模式分为三大类:创建型模式、结构型模式和行为型模式。
2.1 创建型模式
创建型模式关注对象的创建过程,主要目的是为了减少系统中对象的创建与依赖关系,使系统更加灵活。
2.1.1 工厂方法模式(Factory Method)
工厂方法模式定义了一个接口,让子类决定实例化哪一个具体类。
public interface Creator {
Product factoryMethod();
}
public class ConcreteCreatorA implements Creator {
public Product factoryMethod() {
return new ConcreteProductA();
}
}
public class ConcreteCreatorB implements Creator {
public Product factoryMethod() {
return new ConcreteProductB();
}
}
2.1.2 抽象工厂模式(Abstract Factory)
抽象工厂模式提供一系列相关的或相互依赖的对象,它们都共同属于一个产品家族。
public interface AbstractFactory {
ProductA createProductA();
ProductB createProductB();
}
public class ConcreteFactoryA implements AbstractFactory {
public ProductA createProductA() {
return new ConcreteProductA();
}
public ProductB createProductB() {
return new ConcreteProductB();
}
}
public class ConcreteFactoryB implements AbstractFactory {
public ProductA createProductA() {
return new ConcreteProductA();
}
public ProductB createProductB() {
return new ConcreteProductB();
}
}
2.1.3 单例模式(Singleton)
单例模式确保一个类只有一个实例,并提供全局访问点。
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
2.1.4 建造者模式(Builder)
建造者模式将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建不同的表示。
public class Builder {
public void createProduct() {
// 构建步骤
}
}
public class Director {
private Builder builder;
public Director(Builder builder) {
this.builder = builder;
}
public void construct() {
builder.createProduct();
}
}
2.1.5 原型模式(Prototype)
原型模式通过复制现有的对象来创建新对象,减少重复代码。
public class Prototype implements Cloneable {
public Prototype clone() {
try {
return (Prototype) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
2.2 结构型模式
结构型模式关注对象组合和协作,主要目的是为了提高系统的可扩展性和可维护性。
2.2.1 桥接模式(Bridge)
桥接模式将抽象部分与实现部分分离,使它们可以独立地变化。
public interface Abstraction {
void operation();
}
public class RefinedAbstraction implements Abstraction {
private Implementor implementor;
public RefinedAbstraction(Implementor implementor) {
this.implementor = implementor;
}
public void operation() {
implementor.operationImpl();
}
}
public interface Implementor {
void operationImpl();
}
public class ConcreteImplementorA implements Implementor {
public void operationImpl() {
// 实现细节
}
}
public class ConcreteImplementorB implements Implementor {
public void operationImpl() {
// 实现细节
}
}
2.2.2 适配器模式(Adapter)
适配器模式使一个接口匹配另一个接口,以便于调用者无需知道细节。
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
public void request() {
adaptee-specificRequest();
}
}
2.2.3 装饰器模式(Decorator)
装饰器模式动态地给对象添加一些额外的职责,比继承更灵活。
public class ConcreteComponent implements Component {
public void operation() {
// 基本操作
}
}
public class ConcreteDecoratorA extends Decorator {
public ConcreteDecoratorA(Component component) {
super(component);
}
public void operation() {
super.operation();
addedBehavior();
}
private void addedBehavior() {
// 新增职责
}
}
2.2.4 组合模式(Composite)
组合模式将复杂对象视为由简单对象组成的树形结构。
public interface Component {
void operation();
}
public class Leaf implements Component {
public void operation() {
// 叶子节点操作
}
}
public class Composite implements Component {
private List<Component> components = new ArrayList<>();
public void add(Component component) {
components.add(component);
}
public void operation() {
for (Component component : components) {
component.operation();
}
}
}
2.2.5 享元模式(Flyweight)
享元模式通过共享技术有效地支持大量细粒度的对象,减少内存占用。
public class Flyweight {
private String intrinsicState;
public Flyweight(String intrinsicState) {
this.intrinsicState = intrinsicState;
}
public void operation(String extrinsicState) {
// 使用内部状态和外部状态
}
}
2.2.6 外观模式(Facade)
外观模式提供一个统一的接口,用来访问子系统的一组接口。
public class Facade {
private SubsystemA subsystemA;
private SubsystemB subsystemB;
public Facade() {
subsystemA = new SubsystemA();
subsystemB = new SubsystemB();
}
public void operation() {
subsystemA.operation();
subsystemB.operation();
}
}
2.2.7 代理模式(Proxy)
代理模式为其他对象提供一种代理以控制对这个对象的访问。
public class Proxy implements Subject {
private RealSubject realSubject;
public Proxy(RealSubject realSubject) {
this.realSubject = realSubject;
}
public void request() {
realSubject.request();
}
}
2.3 行为型模式
行为型模式关注对象之间的交互方式,主要目的是为了提高系统的灵活性和可扩展性。
2.3.1 模板方法模式(Template Method)
模板方法模式定义算法的骨架,而将一些步骤延迟到子类中实现。
public abstract class AbstractClass {
protected abstract void step1();
protected abstract void step2();
public final void templateMethod() {
step1();
step2();
}
}
public class ConcreteClass extends AbstractClass {
public void step1() {
// 实现步骤1
}
public void step2() {
// 实现步骤2
}
}
2.3.2 策略模式(Strategy)
策略模式定义一组算法,让它们之间可以互换。
public interface Strategy {
void algorithm();
}
public class ConcreteStrategyA implements Strategy {
public void algorithm() {
// 实现策略A
}
}
public class ConcreteStrategyB implements Strategy {
public void algorithm() {
// 实现策略B
}
}
public class Context {
private Strategy strategy;
public Context(Strategy strategy) {
this.strategy = strategy;
}
public void execute() {
strategy.algorithm();
}
}
2.3.3 状态模式(State)
状态模式根据对象的状态改变其行为。
public interface State {
void handle();
}
public class ConcreteStateA implements State {
public void handle() {
// 状态A处理
}
}
public class ConcreteStateB implements State {
public void handle() {
// 状态B处理
}
}
public class Context {
private State state;
public void setState(State state) {
this.state = state;
}
public void request() {
state.handle();
}
}
2.3.4 观察者模式(Observer)
观察者模式实现了对对象的发布-订阅机制。
public interface Observer {
void update();
}
public class ConcreteObserver implements Observer {
public void update() {
// 观察者处理
}
}
public class Subject {
private List<Observer> observers = new ArrayList<>();
public void addObserver(Observer observer) {
observers.add(observer);
}
public void notifyObservers() {
for (Observer observer : observers) {
observer.update();
}
}
}
3. 总结
本文详细解析了23种面向对象设计模式的核心技术,并结合实际应用进行了实战讲解。希望读者能够通过学习这些设计模式,提高自己在软件开发中的设计能力,为构建更加灵活、可扩展和可维护的系统贡献力量。