visitor模式之c#实现
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication2
{
/// <summary>
/// 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);
}
}
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
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
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[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);
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication2
{
/// <summary>
/// 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);
}
}
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
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
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[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);
}
}
}
...全文
相关推荐
C#设计模式之Visitor
**C#设计模式之Visitor****一、设计模式概述**设计模式是软件开发中的经验总结,它提供了解决常见问题的可复用解决方案。
解析布尔代数表达式的C#实现
本文介绍了如何在C#中使用ANTLR解析和评估布尔代数表达式。首先定义了一个布尔代数的ANTLR语法规则,包括逻辑运算优先级。然后通过ANTLR生成解析器并实现Visitor模式来计算表达式值。文中展示了具体的C#实现代码,包括各个逻辑运算符的处理方法,并提供了测试用例验证解析器功能。这种结合ANTLR和Visitor模式的方法简化了复杂布尔表达式的处理过程,可扩展应用于其他表达式解析任务,体现了C#处理语法结构的强大能力。
c#设计模式论文
在C#中,设计模式的运用广泛,例如Observer模式用于实现对象之间的发布/订阅关系,而Visitor模式允许在不修改类结构的情况下,增加对类的操作。
C#面向对象设计模式纵横谈(24):(行为型模式) Visitor 访问者模式
### C#面向对象设计模式纵横谈(24):(行为型模式) Visitor 访问者模式#### 概述在本篇文章中,我们将深入探讨面向对象设计模式中的一个非常重要的模式——**Visitor(访问者)模式
C#设计模式源码
在C#中,命令模式常用于实现GUI中的事件处理。4.


