我的一个visitor实作

03-04-05 VRGL

public interface Visitable

{

void accept(Visitor visitor);

}

public interface Visitor

{

void visit(Visitable visitable);

}

public class VisitableA implements Visitable

{

public String Call(){

return "AAA";

}

public void accept(Visitor visitor) {

visitor.visit(this);

}

}

public class VisitorA implements Visitor

{ public void visit(Visitable visitable) {

if (visitable instanceof VisitableA)

{

System.out.println(((VisitableA)visitable).Call());

}

}

}

public class Client{

public static void main(String args[])

{

Visitor visitor=new VisitorA();

Visitable visitable=new VisitableA();

visitable.accept(visitor);

}

}

    

1
banq
2003-04-05 17:17
不错,关键是要结合自己的项目能应用,因为这时你就有两件事要协调和处理,一边是visitor模式各个角色,一边是实际项目的各个角色,如何对号入坐是比较费劲的。

VRGL
2003-04-06 17:43
delphi版本的:

unit UVisitor;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, forms,

Dialogs, StdCtrls;

type

Tform1 = class(Tform)

Button1: TButton;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

form1: Tform1;

implementation

type

TVisitor=class;

TVisitable=class

procedure accept(visitor:TVisitor);virtual;abstract;

end;

TVisitor=class

procedure visit(visitable:TVisitable);virtual;abstract;

end;

TVisitableA=class(TVisitable)

public

procedure accept(visitor:TVisitor);override;

function Call():string;

end;

TVisitorA=class(TVisitor)

public

procedure visit(visitable:TVisitable);override;

end;

{$R *.dfm}

{ TVisitableA }

procedure TVisitableA.accept(visitor: TVisitor);

begin

inherited;

visitor.visit(self);

end;

function TVisitableA.Call: string;

begin

Result:='AAA';

end;

{ TVisitorA }

procedure TVisitorA.visit(visitable: TVisitable);

begin

inherited;

if visitable is TVisitableA then

showmessage(TVisitableA(visitable).Call);

end;

procedure Tform1.Button1Click(Sender: TObject);

var

v:TVisitor;

vb:TVisitable;

begin

v:=TVisitorA.Create;

vb:=TVisitableA.Create;

vb.accept(v);

v.Free;

vb.Free;

end;

end.

VRGL
2003-04-06 17:43
c#版本的:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.forms;

using System.Data;

namespace WindowsApplication2

{

///

/// form1 的摘要说明。

///

public interface IVisitable

{

void accept(IVisitor visitor);

}

public interface IVisitor

{

void visit(IVisitable visitable);

}

public class VisitorA:IVisitor

{

public void visit(IVisitable visitable){

if (visitable is VisitableA){

MessageBox.Show(((VisitableA)visitable).Call(),"");

}

}

}

public class VisitableA:IVisitable

{

public String Call()

{

return "AAA";

}

public void accept(IVisitor visitor){

visitor.visit(this);

}

}

///

public class form1 : System.Windows.forms.form

{

private System.Windows.forms.Button button1;

///

/// 必需的设计器变量。

///

private System.ComponentModel.Container components = null;

public form1()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

//

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码

//

}

///

/// 清理所有正在使用的资源。

///

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows form Designer generated code

///

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

this.button1 = new System.Windows.forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(80, 72);

this.button1.Name = "button1";

this.button1.TabIndex = 0;

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(292, 272);

this.Controls.AddRange(new System.Windows.forms.Control[] {

this.button1});

this.Name = "form1";

this.Text = "form1";

this.ResumeLayout(false);

}

#endregion

///

/// 应用程序的主入口点。

///

[STAThread]

static void Main()

{

Application.Run(new form1());

}

private void button1_Click(object sender, System.EventArgs e)

{

IVisitor v=new VisitorA();

IVisitable vb=new VisitableA();

vb.accept(v);

}

}

}

VRGL
2003-04-06 17:44
vb.net版本的:

Public Class form1

Inherits System.Windows.forms.form

Public MustInherit Class Visitor

Public MustOverride Sub visit(ByRef vb As Visitable)

End Class

Public MustInherit Class Visitable

Public MustOverride Sub accept(ByRef v As Visitor)

End Class

Public Class VisitorA

Inherits Visitor

Public Overrides Sub visit(ByRef vb As Visitable)

If vb.GetType Is GetType(VisitableA) Then

MessageBox.Show(CType(vb, VisitableA).CallOp, "")

End If

End Sub

End Class

Public Class VisitableA

Inherits Visitable

Public Function CallOp() As String

Return "AAA"

End Function

Public Overrides Sub accept(ByRef v As Visitor)

v.visit(Me)

End Sub

End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim vb As Visitable

Dim v As Visitor

vb = New VisitableA()

v = New VisitorA()

vb.accept(v)

vb = Nothing

v = Nothing

End Sub

End Class

猜你喜欢
3Go 1 2 3 下一页