【揭秘23种面向对象设计模式】核心技术解析与应用实战

发布时间:2025-06-08 02:38:24

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种面向东西计划形式的核心技巧,并结合现实利用停止了实战讲解。盼望读者可能经由过程进修这些计划形式,进步本人在软件开辟中的计划才能,为构建愈加机动、可扩大年夜跟可保护的体系奉献力量。