当我们设计一个类时,首先要规划这个类主要是干什么用的,并且对外要放出哪些接口。这时就要设计这个类的对外接口都有哪些了。在进行多层次开发的时候,由于下层对上层是透明的,上层无须知道下层的操作方式以及代码,因此我主张层与层之间的交互主要靠的是接口。尽量不要用类。下面的代码是设计的接口,类,以及其他的层的调用。
1
public interface IMaster
2
{
3
/// <summary>
4
/// 获得名称
5
/// </summary>
6
string GetName();
7
/// <summary>
8
/// 设置名称
9
/// </summary>
10
/// <param name="str">名称</param>
11
void SetName(string str);
12
}
13
public class Master:IMaster
14
{
15
/// <summary>
16
/// 底层
17
/// </summary>
18
private Master()
19
{
20
//
21
// TODO: 在此处添加构造函数逻辑
22
//
23
}
24
private static Master _instance = null;
25
/// <summary>
26
///
27
/// </summary>
28
/// <returns></returns>
29
public static Master Instance()
30
{
31
if(_instance == null)
32
{
33
_instance = new Master();
34
}
35
return _instance;
36
}
37
private string Name;
38
39
IMaster接口
51
}
52
53
/// <summary>
54
/// 上层
55
/// </summary>
56
public class User
57
{
58
IMaster MasterExample = null;
59
public User()
60
{
61
MasterExample = Master.Instance();
62
}
63
64
public string GetUseName()
65
{
66
return MasterExample.GetName();
67
}
68
public string SetUseName(string str)
69
{
70
MasterExample.SetName(str);
71
}
72
}

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

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72
