如何使用DUnitWizard中包含的XPObserver单元来实现观察者模式甚至是MVC模式?

6

感谢Stackoverflow上明智的问题和回答,使得Delphi中的Observer设计模式有了很好的例子,例如如何在Delphi中实现Observer设计模式有没有视频/播客或其他资源介绍如何在Delphi中使用接口?。从这些stackoverflow问题中,提取了以下指导性材料的链接:

  1. Joanna Carter的博客

  2. SourceMaking网站

  3. TDelphiHobbyist的博客

  4. itte.no网站

  5. dunit的DUnitWizard

在第二个stackoverflow问题中,mghie将描述为非常有趣,其他值得更仔细地研究。但是,XPObserver单元只在两个位置引用,在中,测试的唯一兴趣似乎是检查引用计数,以及在中,仅使用了在单元中声明的IXPFamily类型。

因此,我想知道使用这个单元的最佳实践。

例如:设计问题,如:

(1) 如何使用单元实现一个执行某些操作的观察者模式?

(2) 如何使用实现MVC模式?

或者像编码问题一样:

(3)声称XPObserver的TXPSubjects提供了启用单个观察者<->多个主题关系的功能。但是,FSubjects被声明为私有。也没有getter方法。我想知道这是按设计来的吗?(例如,作者在TXPSubject.DeleteObserver中写道“// ... ***DON'T*** refactor this method!!”,因此我不自信修改代码,因为我可能无法完全理解这个和其他部分)。如果是这样,请问如何使用TXPSubjects以启用单个观察者<->多个主题关系?
非常感谢您的时间和建议!
1个回答

1

让我举个例子,如何使用XPObserver单元。首先,模拟数据模型需要几个接口:

type
  IColorChannel = interface(IXPSubject)
    function GetValue: byte;
    procedure RandomChange;
  end;

  IColorChannelObserver = interface(IXPObserver)
    ['{E1586F8F-32FB-4F77-ACCE-502AFDAF0EC0}']
    procedure Changed(const AChannel: IColorChannel);
  end;

  IColor = interface(IXPSubject)
    function GetValue: TColor;
  end;

  IColorObserver = interface(IXPObserver)
    ['{0E5D2FEC-5585-447B-B242-B9B57FC782F2}']
    procedure Changed(const AColor: IColor);
  end;

IColorChannel 只是包装了一个 byte 值,它有返回值和随机更改值的方法。实现 IColorChannelObserver 接口的对象可以注册自己来观察它。

IColor 只是包装了一个 TColor 值,它只有一个返回值的方法。实现 IColorObserver 接口的对象可以注册自己来观察它。

实现 IColorChannel 的类并不困难:

type
  TColorChannel = class(TXPSubject, IColorChannel)
    function GetValue: byte;
    procedure RandomChange;
  private
    fValue: byte;
  end;

function TColorChannel.GetValue: byte;
begin
  Result := fValue;
end;

procedure TColorChannel.RandomChange;
var
  Value, Idx: integer;
  Icco: IColorChannelObserver;
begin
  Value := Random(256);
  if fValue <> Value then begin
    fValue := Value;
    for Idx := 0 to ObserverCount - 1 do begin
      // Or use the Supports() function instead of QueryInterface()
      if GetObserver(Idx).QueryInterface(IColorChannelObserver, Icco) = S_OK then
        Icco.Changed(Self);
    end;
  end;
end;

现在有一个实现RGB的IColor类,它将包含并观察三个TColorChannel实例 - 即单个观察者多个主题关系:

type
  TRGBColor = class(TXPSubject, IColor, IColorChannelObserver)
    function GetValue: TColor;
  private
    fRed: IColorChannel;
    fGreen: IColorChannel;
    fBlue: IColorChannel;
    fValue: TColor;
    function InternalUpdate: boolean;
  public
    constructor Create(ARed, AGreen, ABlue: IColorChannel);

    procedure Changed(const AChannel: IColorChannel);
  end;

constructor TRGBColor.Create(ARed, AGreen, ABlue: IColorChannel);
begin
  Assert(ARed <> nil);
  Assert(AGreen <> nil);
  Assert(ABlue <> nil);
  inherited Create;
  fRed := ARed;
  fRed.AddObserver(Self, fRed);
  fGreen := AGreen;
  fGreen.AddObserver(Self, fGreen);
  fBlue := ABlue;
  fBlue.AddObserver(Self, fBlue);
  InternalUpdate;
end;

procedure TRGBColor.Changed(const AChannel: IColorChannel);
var
  Idx: integer;
  Ico: IColorObserver;
begin
  if InternalUpdate then
    for Idx := 0 to ObserverCount - 1 do begin
      if GetObserver(Idx).QueryInterface(IColorObserver, Ico) = S_OK then
        Ico.Changed(Self);
    end;
end;

function TRGBColor.GetValue: TColor;
begin
  Result := fValue;
end;

function TRGBColor.InternalUpdate: boolean;
var
  Value: TColor;
begin
  Result := False;
  Value := RGB(fRed.GetValue, fGreen.GetValue, fBlue.GetValue);
  if fValue <> Value then begin
    fValue := Value;
    Result := True;
  end;
end;

如果三个通道值中的任何一个发生变化,颜色将应用更改,并通知其所有观察者。
现在使用这些类的数据模块:
type
  TDataModule1 = class(TDataModule)
    procedure DataModuleCreate(Sender: TObject);
  private
    fRed: IColorChannel;
    fGreen: IColorChannel;
    fBlue: IColorChannel;
    fColor: IColor;
  public
    property BlueChannel: IColorChannel read fBlue;
    property GreenChannel: IColorChannel read fGreen;
    property RedChannel: IColorChannel read fRed;
    property Color: IColor read fColor;
  end;

procedure TDataModule1.DataModuleCreate(Sender: TObject);
begin
  Randomize;

  fRed := TColorChannel.Create;
  fGreen := TColorChannel.Create;
  fBlue := TColorChannel.Create;

  fColor := TRGBColor.Create(fRed, fGreen, fBlue);
end;

最后,一个使用了数据模块的表单,只知道接口,对实现类一无所知。
type
  TForm1 = class(TForm, IXPObserver, IColorChannelObserver, IColorObserver)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    StatusBar1: TStatusBar;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ButtonClick(Sender: TObject);
  public
    procedure Changed(const AChannel: IColorChannel); overload;
    procedure Changed(const AColor: IColor); overload;
    procedure ReleaseSubject(const Subject: IXPSubject;
      const Context: pointer);
  private
    fChannels: array[0..2] of IColorChannel;
    fColor: IColor;
  end;

procedure TForm1.FormCreate(Sender: TObject);
var
  Idx: integer;
begin
  Button1.Caption := 'red';
  Button1.Tag := 0;
  fChannels[0] := DataModule1.RedChannel;

  Button2.Caption := 'green';
  Button2.Tag := 1;
  fChannels[1] := DataModule1.GreenChannel;

  Button3.Caption := 'blue';
  Button3.Tag := 2;
  fChannels[2] := DataModule1.BlueChannel;

  for Idx := 0 to 2 do
    fChannels[Idx].AddObserver(Self, fChannels[Idx]);

  fColor := DataModule1.Color;
  fColor.AddObserver(Self, fColor);
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  Idx: integer;
begin
  for Idx := Low(fChannels) to High(fChannels) do
    fChannels[Idx].DeleteObserver(Self);
  fColor.DeleteObserver(Self);
end;

procedure TForm1.ButtonClick(Sender: TObject);
var
  Button: TButton;
begin
  Button := Sender as TButton;
  if (Button.Tag >= Low(fChannels)) and (Button.Tag <= High(fChannels)) then
    fChannels[Button.Tag].RandomChange;
end;

procedure TForm1.Changed(const AChannel: IColorChannel);
var
  Idx: integer;
begin
  Assert(AChannel <> nil);
  for Idx := Low(fChannels) to High(fChannels) do
    if fChannels[Idx] = AChannel then begin
      while StatusBar1.Panels.Count <= Idx do
        StatusBar1.Panels.Add;
      StatusBar1.Panels[Idx].Text := IntToStr(AChannel.GetValue);
      break;
    end;
end;

procedure TForm1.Changed(const AColor: IColor);
begin
  Assert(AColor <> nil);
  Color := AColor.GetValue;
end;

procedure TForm1.ReleaseSubject(const Subject: IXPSubject;
  const Context: pointer);
var
  Idx: integer;
begin
  // necessary if the objects implementing IXPSubject are not reference-counted
  for Idx := Low(fChannels) to High(fChannels) do begin
    if Subject = fChannels[Idx] then
      fChannels[Idx] := nil;
  end;
  if Subject = fColor then
    fColor := nil;
end;

该表单实现了接口,但没有引用计数。它会注册自己以观察数据模块的四个属性中的每一个,每当颜色通道发生变化时,它会在状态栏窗格中显示该值,当颜色发生变化时,它会更新自己的背景颜色。还有按钮可以随机更改颜色通道。
数据模块属性可能会有更多的观察者和其他更改数据的方式。
使用FastMM4在Delphi 5和Delphi 2009中进行了测试,没有内存泄漏。如果表单中的每个AddObserver()没有匹配的DeleteObserver()调用,则会出现泄漏。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接