-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigEditors.pas
More file actions
81 lines (65 loc) · 2.18 KB
/
ConfigEditors.pas
File metadata and controls
81 lines (65 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
unit ConfigEditors;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
System.JSON, JSONHelpers, JSONConfig, INIConfig, System.TypInfo, System.Generics.Collections,
ConfigEditorsBase;
type
// 配置编辑器工厂
TConfigEditorFactory = class
private
class var FEditors: TDictionary<TConfigType, TConfigEditorCreateFunc>;
class constructor Create;
class destructor Destroy;
public
class procedure RegisterEditor(EditorType: TConfigType; CreateFunc: TConfigEditorCreateFunc);
class function CreateEditor(ConfigType: TConfigType): IConfigEditor;
class procedure ClearRegistrations;
end;
// 注册编辑器函数
procedure RegisterConfigEditor(EditorType: TConfigType; CreateFunc: TConfigEditorCreateFunc);
implementation
// 注册编辑器函数
procedure RegisterConfigEditor(EditorType: TConfigType; CreateFunc: TConfigEditorCreateFunc);
begin
if Assigned(CreateFunc) then
TConfigEditorFactory.RegisterEditor(EditorType, CreateFunc);
end;
{ TConfigEditorFactory }
class procedure TConfigEditorFactory.ClearRegistrations;
begin
FEditors.Clear;
end;
class constructor TConfigEditorFactory.Create;
begin
FEditors := TDictionary<TConfigType, TConfigEditorCreateFunc>.Create;
end;
class function TConfigEditorFactory.CreateEditor(ConfigType: TConfigType): IConfigEditor;
var
CreateFunc: TConfigEditorCreateFunc;
begin
Result := nil;
// 查找注册的创建函数
if FEditors.TryGetValue(ConfigType, CreateFunc) then
begin
// 调用创建函数
Result := CreateFunc();
Exit;
end;
// 如果没有找到创建函数,返回nil
// 具体的编辑器类型将通过RegisterEditor方法注册
end;
class destructor TConfigEditorFactory.Destroy;
begin
FEditors.Free;
end;
class procedure TConfigEditorFactory.RegisterEditor(EditorType: TConfigType; CreateFunc: TConfigEditorCreateFunc);
begin
// 如果已存在相同类型的创建函数,则先移除
if FEditors.ContainsKey(EditorType) then
FEditors.Remove(EditorType);
// 添加创建函数
FEditors.Add(EditorType, CreateFunc);
end;
end.