首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 封裝模式實戰:以 Wrapper 類別實現介面相容與多重繼承模擬

     編輯:Delphi

前言

在 Delphi 物件導向設計中,封裝模式 是一種結構型設計模式,透過將一個類別「包裹」在另一個類別內部,將被包裹類別的介面轉換為客戶端期望的介面。這在以下情境極為實用:

  • 第三方元件介面不符合專案規範
  • 需將非 VCL 類別整合進元件庫系統
  • 模擬多重繼承效果(Delphi 不支援多重繼承)

經典案例:將 TObject 後代納入元件調色盤

問題情境

假設有一個獨立類別 TSample 繼承自 TObject,我們希望它能像 TComponent 後代一樣放入 Delphi 元件調色盤、支援設計時屬性編輯器。但 無法修改 TSample 祖先類別(例如無原始碼)。

Wrapper 解決方案

建立 TSampleWrapper 繼承 TComponent,內部持有 TSample 實例並轉發介面:

步驟 1:定義被包裹類別

type
  TSample = class(TObject)
  private
    FSomeValue: Integer;
  public
    function SomeAction(const Data: string): Boolean;
    property SomeValue: Integer read FSomeValue write FSomeValue;
  end;

步驟 2:建立 Wrapper 基礎架構

type
  TSampleWrapper = class(TComponent)
  private
    FSample: TSample;
  public
    property Sample: TSample read FSample; // 暴露內部實例(可選)
  end;

步驟 3:轉發介面成員(核心實作)

type
  TSampleWrapper = class(TComponent)
  private
    FSample: TSample;
  protected
    // 存取器轉發
    function GetSomeValue: Integer;
    procedure SetSomeValue(Value: Integer);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    // 方法轉發
    function SomeAction(const Data: string): Boolean;
    // 重新發布屬性
    property Sample: TSample read FSample;
    property SomeValue: Integer read GetSomeValue write SetSomeValue;
  end;

implementation

constructor TSampleWrapper.Create(AOwner: TComponent);
begin
  inherited;
  FSample := TSample.Create; // 建立被包裹實例
end;

destructor TSampleWrapper.Destroy;
begin
  FSample.Free;
  inherited;
end;

function TSampleWrapper.GetSomeValue: Integer;
begin
  Result := FSample.SomeValue; // 讀取轉發
end;

procedure TSampleWrapper.SetSomeValue(Value: Integer);
begin
  FSample.SomeValue := Value; // 寫入轉發
end;

function TSampleWrapper.SomeAction(const Data: string): Boolean;
begin
  Result := FSample.SomeAction(Data); // 方法轉發
end;

進階應用:介面與多型

若專案使用介面導向設計,Wrapper 可實作介面並委派給內部物件:

type
  ISample = interface
    ['{GUID}']
    function SomeAction(const Data: string): Boolean;
    property SomeValue: Integer read GetSomeValue write SetSomeValue;
  end;

  TSampleWrapper = class(TComponent, ISample)
  private
    FSample: TSample;
    function GetSomeValue: Integer;
    procedure SetSomeValue(Value: Integer);
  public
    function SomeAction(const Data: string): Boolean;
  end;

此時 TSampleWrapper 可直接作為 ISample 使用,實現介面多型。

設計原則與最佳實務

原則說明
單一職責Wrapper 僅負責介面轉換與生命週期管理,不添加業務邏輯
介面隔離只轉發必要成員,避免暴露內部實作細節
組合優於繼承透過組合關係實現功能擴展,降低耦合度
生命週期同步Wrapper 建立/釋放時同步處理內部物件

常見變體

  • 裝飾模式:Wrapper 在轉發前後添加額外行為(日誌、快取、驗證)
  • 介面卡:將多個不相關類別包裝為統一介面
  • 資料傳輸物件包裝:為記錄型資料添加方法與屬性存取器

結語

Wrapper 模式是 Delphi 開發者處理「介面不兼容」與「缺乏多重繼承」的利器。透過組合與轉發,既能保持原有類別結構不變,又能滿足 VCL 架構對 TComponent 繼承鏈的要求。善用此模式,能讓舊代碼、第三方庫與新架構無縫協作。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved