4

使用 Delphi 2009 Enterprise,我在模型视图中为 GoF 访问者模式创建了代码,并将代码分成两个单元:一个用于域模型类,一个用于访问者(因为我可能需要其他单元来实现不同的访问者,所有内容都在一个单位?前面有“大泥球”!)。

unit VisitorUnit;

interface

uses
  ConcreteElementUnit;

type
  IVisitor = interface;

  IElement = interface
  procedure Accept(AVisitor :IVisitor);
  end;

  IVisitor = interface
  procedure VisitTConcreteElement(AElement :TConcreteElement);
  end;

  TConcreteVisitor = class(TInterfacedObject, IVisitor)
  public
    procedure VisitTConcreteElement(AElement :TConcreteElement);
  end;

implementation

procedure TConcreteVisitor.VisitTConcreteElement(AElement :TConcreteElement);
begin
  { provide implementation here }
end;

end.

以及商业模式课程的第二单元

unit ConcreteElementUnit;

interface

uses
  VisitorUnit;

type
  TConcreteElement = class(TInterfacedObject, IElement)
  public
    procedure Accept(AVisitor :IVisitor); virtual;
  end;

  Class1 = class(TConcreteElement)
  public
    procedure Accept(AVisitor :IVisitor);
  end;

implementation

{ Class1 }

procedure Class1.Accept(AVisitor: IVisitor);
begin
  AVisitor.VisitTConcreteElement(Self);
end;

end.

看到问题了吗?循环单位参考。有没有优雅的解决方案?我想它需要“n+1”个带有基本接口/基类定义的附加单元来避免 CR 问题,以及硬转换之类的技巧?

4

4 回答 4

4

我使用以下方案来实现灵活的访问者模式:

基本访客类型声明

unit uVisitorTypes;
type
  IVisited = interface
  { GUID }
    procedure Accept(Visitor: IInterface);
  end;

  IVisitor = interface
  { GUID }
    procedure Visit(Instance: IInterface);
  end;

  TVisitor = class(..., IVisitor)
    procedure Visit(Instance: IInterface);
  end;

procedure TVisitor.Visit(Instance: IInterface);
var
  visited: IVisited;
begin
  if Supports(Instance, IVisited, visited) then 
    visited.Accept(Self)
  else
    // raise exception or handle error elsewise    
end;

元素类的单位

unit uElement;

type
  TElement = class(..., IVisited)
    procedure Accept(Visitor: IInterface);
  end;

  // declare the visitor interface next to the class-to-be-visited declaration   
  IElementVisitor = interface
  { GUID }
    procedure VisitElement(Instance: TElement);
  end;

procedure TElement.Accept(Visitor: IInterface);
var
  elementVisitor: IElementVisitor;
begin
  if Supports(Visitor, IElementVisitor, elementVisitor) then
    elementVisitor.VisitElement(Self)
  else
    // if override call inherited, handle error or simply ignore
end;

实际的访问者实现

unit MyVisitorImpl;

uses
  uVisitorTypes, uElement;

type
  TMyVisitor = class(TVisitor, IElementVisitor)
    procedure VisitElement(Instance: TElement);
  end;

procedure TMyVisitor.VisitElement(Instance: TElement);
begin
  // Do whatever you want with Instance 
end;

给来访者打电话

uses
  uElement, uMyElementVisitor;

var
  visitor: TMyVisitor;
  element: TElement;
begin
  // get hands on some element

  visitor := TMyVisitor.Create;
  try
    visitor.Visit(element);
  finally
    visitor.Free;
  end;
end;
于 2010-03-01T15:07:43.610 回答
1

以下实现在访问者接口上使用泛型类型来解决Visitor模式的循环引用问题:

Visitor.Intf.pas

unit Visitor.Intf;

interface

type
  IVisitor<T> = interface
    procedure Visit_Element(o: T);
  end;

implementation

end.

Element.pas

unit Element;

interface

uses Visitor.Intf;

type
  TElement = class
    procedure Accept(const V: IVisitor<TElement>);
  end;

implementation

procedure TElement.Accept(const V: IVisitor<TElement>);
begin
  V.Visit_Element(Self);
end;

end.

Visitor.Concrete.pas

unit Visitor.Concrete;

interface

uses Element, Visitor.Intf;

type
  TConcreteVisitor = class(TInterfacedObject, IVisitor<TElement>)
  protected
    procedure Visit_Element(o: TElement);
  end;

implementation

procedure TConcreteVisitor.Visit_Element(o: TElement);
begin
  // write implementation here
end;

end.

使用 TElement 和 TConcreteVisitor 类:

var E: TElement;
begin
  E := TElement.Create;
  E.Accept(TConcreteVisitor.Create as IVisitor<TElement>);
  E.Free;
end;
于 2014-08-26T09:34:54.150 回答
1

为什么不定义 IVisitor

IVisitor = interface
  procedure VisitElement(AElement :IElement);
end; 

然后 TConcreteElement 在它自己的单元中:

unit ConcreteElementUnit;

interface

uses
  VisitorUnit;

type
  TConcreteElement = class(TInterfacedObject, IElement)
  public
    procedure Accept(AVisitor :IVisitor); virtual;
  end;

  Class1 = class(TConcreteElement)
  public
    procedure Accept(AVisitor :IVisitor);
  end;

implementation

{ Class1 }

procedure Class1.Accept(AVisitor: IVisitor);
begin
  AVisitor.VisitElement(Self);
end;

end.

这样你就不会混合类和接口引用(总是一个坏主意)

于 2010-03-01T14:32:46.627 回答
0

TConcreteElement 的声明应该在 VisitorUnit (或第三个单元)

或更好

IVisitator 应更改为:

 IVisitor = interface
  procedure VisitTConcreteElement(AElement :IElement);
 end;
于 2010-03-01T14:31:44.707 回答