| 作者 |
内容 |
| vrgl |
我的一个visitor实作
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);
}
} |
| 03/04/05 17:17 |
酷帖! 臭帖! 回复 |
酷帖评价: 臭帖评价: |
| 返回页首 |
|
| vrgl |
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. |
| 03/04/05 23:23 |
酷帖! 臭帖! 回复 |
酷帖评价: 臭帖评价: |
| 返回页首 |
|
| vrgl |
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);
}
}
} |
| 03/04/05 23:56 |
酷帖! 臭帖! 回复 |
酷帖评价: 臭帖评价: |
| 返回页首 |
|
| vrgl |
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 |
| 03/04/06 17:24 |
酷帖! 臭帖! 回复 |
酷帖评价: 臭帖评价: |
| 返回页首 |
|