forked from AmazingCode/DesignModel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28、访问者模式.cs
69 lines (67 loc) · 2.01 KB
/
28、访问者模式.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
/// <summary>
/// 一个作用于某对象结构中各元素的操作,它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作
/// 利用双分派技术 实现处理与数据机构的分离
/// </summary>
class _28_访问者模式
{
public void Main()
{
//特点:Action和Human抽象类中的函数互为各自的参数,但是要保证Action中的方法是稳定的。是两个方法就是2个方法,万一增加了双性恋者,就不适用了
//如果要增加一种"结婚"状态,只要重写一个"结婚"类继承自Action就可以
}
}
/// <summary>
/// 状态类(访问者)(个人感觉将Action看成一种数据更易理解) 增加访问者很方便
/// </summary>
abstract class Action
{
public abstract void GetManConclusion(Human human);
public abstract void GetWomanConclusion(Human human);
}
abstract class Human
{
public abstract void Accept(Action action);
}
class Success:Action
{
public override void GetManConclusion(Human human)
{
Console.WriteLine("Man Success");
}
public override void GetWomanConclusion(Human human)
{
Console.WriteLine("Woman Sucess");
}
}
class Fail:Action
{
public override void GetManConclusion(Human human)
{
Console.WriteLine("Man Faile");
}
public override void GetWomanConclusion(Human human)
{
Console.WriteLine("Woman Fail");
}
}
class Man:Human
{
public override void Accept(Action action)
{
action.GetManConclusion(this);
}
}
class Woman:Human
{
public override void Accept(Action action)
{
action.GetWomanConclusion(this);
}
}
}