程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi 根據字符串創建對象

Delphi 根據字符串創建對象

編輯:Delphi

 本文示例源代碼或素材下載

  我們可以通過ClassRegistry單元的TClassRegistry類很輕松的根據字符串創建出對象。

  下面是該類幾個主要函數的說明:

// 獲取TClassRegistry自身的單例引用
class function GetClassRegistry: TClassRegistry;
// 注冊需要動態創建的類
procedure RegisterClass(ClassName: UnicodeString; ObjectClass: TClass); overload;
// 判斷給定的類名是否注冊
function HasClass(ClassName: UnicodeString): Boolean;
// 根據類名創建對象實例
function CreateInstance(ClassName: UnicodeString): TObject;

  當使用工廠模式或者抽象工廠模式創建產品的時候,如果我們要擴展新的具體產品,在創建方法中添加分支結構

  需要改動原有的工廠類。如果能夠根據字符串實現動態創建那麼只要添加新的具體產品單元就可以了。

  以下是一個用Delphi簡單實現的Demo:

  UnitCar單元代碼:

  UnitCar

 1Delphi 根據字符串創建對象unit UnitCar;
 2Delphi 根據字符串創建對象
 3Delphi 根據字符串創建對象interface
 4Delphi 根據字符串創建對象
 5Delphi 根據字符串創建對象type
 6Delphi 根據字符串創建對象
 7Delphi 根據字符串創建對象  TCar = class abstract
 8Delphi 根據字符串創建對象    function Run: string; virtual; abstract;
 9Delphi 根據字符串創建對象  end;
10Delphi 根據字符串創建對象
11Delphi 根據字符串創建對象  TRedCar = class(TCar)
12Delphi 根據字符串創建對象    function Run: string; override;
13Delphi 根據字符串創建對象  end;
14Delphi 根據字符串創建對象
15Delphi 根據字符串創建對象  TBlueCar = class(TCar)
16Delphi 根據字符串創建對象    function Run: string; override;
17Delphi 根據字符串創建對象  end;
18Delphi 根據字符串創建對象
19Delphi 根據字符串創建對象  TBlackCar = class(TCar)
20Delphi 根據字符串創建對象    function Run: string; override;
21Delphi 根據字符串創建對象  end;
22Delphi 根據字符串創建對象
23Delphi 根據字符串創建對象implementation
24Delphi 根據字符串創建對象
25Delphi 根據字符串創建對象uses
26Delphi 根據字符串創建對象  ClassRegistry;
27Delphi 根據字符串創建對象
28Delphi 根據字符串創建對象{ TBlackCar }
29Delphi 根據字符串創建對象
30Delphi 根據字符串創建對象function TBlackCar.Run: string;
31Delphi 根據字符串創建對象begin
32Delphi 根據字符串創建對象  Result := 'BlackCar Ready to run!'
33Delphi 根據字符串創建對象end;
34Delphi 根據字符串創建對象
35Delphi 根據字符串創建對象{ TBlueCar }
36Delphi 根據字符串創建對象
37Delphi 根據字符串創建對象function TBlueCar.Run: string;
38Delphi 根據字符串創建對象begin
39Delphi 根據字符串創建對象  Result := 'BlueCar Ready to run!'
40Delphi 根據字符串創建對象end;
41Delphi 根據字符串創建對象
42Delphi 根據字符串創建對象{ TRedCar }
43Delphi 根據字符串創建對象
44Delphi 根據字符串創建對象function TRedCar.Run: string;
45Delphi 根據字符串創建對象begin
46Delphi 根據字符串創建對象  Result := 'RedCar Ready to run!'
47Delphi 根據字符串創建對象end;
48Delphi 根據字符串創建對象
49Delphi 根據字符串創建對象initialization
50Delphi 根據字符串創建對象  TClassRegistry.GetClassRegistry.RegisterClass(TRedCar.ClassName, TRedCar);
51Delphi 根據字符串創建對象  TClassRegistry.GetClassRegistry.RegisterClass(TBlueCar.ClassName, TBlueCar);
52Delphi 根據字符串創建對象  TClassRegistry.GetClassRegistry.RegisterClass(TBlackCar.ClassName, TBlackCar);
53Delphi 根據字符串創建對象end.

在這裡我們聲明了一個抽象的基類TCar和繼承自TCar的三個具體的類。

  在單元初始化處用RegisterClass方法注冊了這三個子類。

  UnitMain調用處代碼:

  UnitMain

 1Delphi 根據字符串創建對象procedure TDemoForm.CreateClick(Sender: TObject);
 2Delphi 根據字符串創建對象var
 3Delphi 根據字符串創建對象  ClassRegistry: TClassRegistry;
 4Delphi 根據字符串創建對象  Car: TCar;
 5Delphi 根據字符串創建對象begin
 6Delphi 根據字符串創建對象  ClassRegistry := TClassRegistry.GetClassRegistry;
 7Delphi 根據字符串創建對象  if ClassRegistry.HasClass(Name.Text) then
 8Delphi 根據字符串創建對象  begin
 9Delphi 根據字符串創建對象    Car := ClassRegistry.CreateInstance(Name.Text) as TCar;
10Delphi 根據字符串創建對象    Memo.Lines.Add(Car.Run);
11Delphi 根據字符串創建對象    Car.Free;
12Delphi 根據字符串創建對象  end else
13Delphi 根據字符串創建對象    Memo.Lines.Add(Format('Class %s has not registerd!', [Name.Text]));
14Delphi 根據字符串創建對象end;

  效果圖:

Delphi 根據字符串創建對象

  如果我們需要擴展個TYellowCar那麼我們只要添加個繼承自TCar的TYellowCar類並且注冊該類就可以了。



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved