# 通俗易懂设计模式解析——访问者模式

## 访问者模式介绍

### 四、访问者模式代码示例

namespace Visitor_Pattern
{
class VisitorPattern
{
}

///
/// 抽象节点类
///
public abstract class Element
{
public abstract void CalculatedArea();
}

///
/// 长方形计算面积输出
///
public class Rectangle : Element
{
public double _long;
public double _wide;

public  Rectangle(double Long, double Wide)
{
this._long = Long;
this._wide = Wide;
}
public override void CalculatedArea()
{
Console.WriteLine($"长方形面积是：{_long*_wide}"); } } /// /// 圆形计算面积输出 /// public class Circular : Element { public double _r; public Circular(double r) { this._r = r; } public override void CalculatedArea() { Console.WriteLine($"圆形面积是：{Math.PI * _r*_r}");
}
}

///
/// 结构对象
///
public class Graphical
{
public List elements = new List();
public List Elements
{
get { return elements; }
}

public Graphical()
{
Element element = new Rectangle(10,5);
Element element1= new Circular(5);
}
}
}

namespace Visitor_Pattern
{
class Program
{
static void Main(string[] args)
{
Graphical graphical = new Graphical();
foreach (var item in graphical.Elements)
{
item.CalculatedArea();
}

}
}
}


namespace Visitor_Pattern
{
class UseVisitorPattern
{
}

#region 访问者

///
/// 抽象访问者
///
public interface IVistor
{
void Visit(UseRectangle rectangle);
void Visit(UseCircular  useCircular);
}

///
/// 具体访问者
///
public class Vistor : IVistor
{
public void Visit(UseRectangle rectangle)
{
rectangle.CalculatedArea();
Console.WriteLine($"长方形长是：{rectangle._long}"); Console.WriteLine($"长方形宽是：{rectangle._wide}");
Console.WriteLine($"长方形周长是：{2*(rectangle._long+rectangle._wide)}"); } public void Visit(UseCircular useCircular) { useCircular.CalculatedArea(); Console.WriteLine($"圆形的半径是：{useCircular._r}");
Console.WriteLine($"圆形的周长是是：{2*Math.PI*useCircular._r}"); } } #endregion #region 节点类 /// /// 抽象节点类 /// public abstract class UseElement { public abstract void Accept(IVistor vistor); public abstract void CalculatedArea(); } /// /// 长方形计算面积输出 /// public class UseRectangle : UseElement { public double _long; public double _wide; public UseRectangle(double Long, double Wide) { this._long = Long; this._wide = Wide; } public override void Accept(IVistor vistor) { vistor.Visit(this); } public override void CalculatedArea() { Console.WriteLine($"长方形面积是：{_long * _wide}");
}
}

///
/// 圆形计算面积输出
///
public class UseCircular : UseElement
{
public double _r;

public UseCircular(double r)
{
this._r = r;
}
public override void Accept(IVistor vistor)
{
vistor.Visit(this);
}
public override void CalculatedArea()
{
Console.WriteLine(\$"圆形面积是：{Math.PI * _r * _r}");
}
}

#endregion

///
/// 结构对象
///
public class UseGraphical
{
public List elements = new List();
public List Elements
{
get { return elements; }
}

public UseGraphical()
{
UseElement element = new UseRectangle(10, 5);
UseElement element1 = new UseCircular(5);
}
}
}

namespace Visitor_Pattern
{
class Program
{
static void Main(string[] args)
{

UseGraphical graphical = new UseGraphical();
foreach (var item in graphical.Elements)
{
item.Accept(new Vistor());
}
}
}
}