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