【揭秘23種面向對象設計模式】核心技術解析與應用實戰

提問者:用戶RVFV 發布時間: 2025-06-08 02:38:24 閱讀時間: 3分鐘

最佳答案

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種面向東西計劃形式的核心技巧,並結合現實利用停止了實戰講解。盼望讀者可能經由過程進修這些計劃形式,進步本人在軟體開辟中的計劃才能,為構建愈加機動、可擴大年夜跟可保護的體系奉獻力量。

相關推薦