今天看了十分之七的7in10笔记:泡妞之桥梁模式 ,
----------
泡妞的例子
BRIDGE —早上碰到MM,要说早上好,晚上碰到MM,要说晚上好;碰到MM穿了件新衣服,要说你的衣服好漂亮哦,碰到MM新做的发型,要说你的头发好漂亮哦。不要问我“早上碰到MM新做了个发型怎么说”这种问题,自己用BRIDGE组合一下不就行了。
---------------
看完后想到一个问题,使用桥梁模式,按照十分之七的例子,的确可以解决“早上碰到MM新做了个发型怎么说”这种问题,但如果进一步,遇到“早上遇到MM做了个新发型,同时衣服也是一件新的”怎么办呢? 好像不好解决。
如是,按照自己的理解写了以下方案,设计代码为:
请各位指教!
附:程序文件https://files.cnblogs.com/huangzhenwu/BoyMM.rar
----------
泡妞的例子
BRIDGE —早上碰到MM,要说早上好,晚上碰到MM,要说晚上好;碰到MM穿了件新衣服,要说你的衣服好漂亮哦,碰到MM新做的发型,要说你的头发好漂亮哦。不要问我“早上碰到MM新做了个发型怎么说”这种问题,自己用BRIDGE组合一下不就行了。
---------------
看完后想到一个问题,使用桥梁模式,按照十分之七的例子,的确可以解决“早上碰到MM新做了个发型怎么说”这种问题,但如果进一步,遇到“早上遇到MM做了个新发型,同时衣服也是一件新的”怎么办呢? 好像不好解决。
如是,按照自己的理解写了以下方案,设计代码为:
1
using System;
2
using System.Collections;
3
using System.Data ;
4
5
public class MyClass
6
{
7
public static void Main()
8
{
9
Enviroment env = EnvFactory.GeTCurrentEnv(DateTime.Now );
10
11
MM mm = new MM( true , true )
12
13
Me i = new Me();
14
15
env.碰面( mm , i );
16
17
RL();
18
}
19
20
Helper methods
38
}
39
40
public class MM
41
{
42
public MM()
43
{
44
//MMPropertyAndGoodWords.xml从动态构建MM的属性
45
}
46
47
public MM(bool is新发型 , bool is穿新衣服)
48
{
49
//动态构建MM的属性
50
51
//设置Is新发型 = is新发型;
52
//Is穿新衣服 = is穿新衣服;
53
}
54
//public bool Is卷发 = false;
55
//public bool Is穿新衣服 = false;
56
}
57
58
public class Me
59
{
60
public void SayToMM(Enviroment ev, MM mm)
61
{
62
SayHello(ev);
63
SayGood();
64
}
65
66
public void SayHello(Enviroment ev)
67
{
68
Console.WriteLine ( ev.HelloWords() );
69
}
70
71
public void SayGood(MM mm)
72
{
73
//读配置文件
74
DateSet ds = new DataSet();
75
ds.ReadXml(Application.StartupPath + "\\MMPropertyAndGoodWords.xml");
76
foreach( Property prt in mm.Properties)
77
{
78
if( prt == true )
79
{
80
DataRow dr = ((DataRow[])ds.Tables[0].Select("Property='" + prt.ToString()+"'"))[0] ;
81
Console.WriteLine ( dr["赞美词"] );
82
}
83
}
84
}
85
}
86
87
88
public class EnvFactory
89
{
90
public Enviroment GeTCurrentEnv(DateTime time)
91
{
92
Enviroment env = null;
93
94
switch( time.Hour)
95
{
96
case 大于0小于12 :
97
env = new Morning(); break;
98
case 大于等于12小于14 :
99
env = new Moon() ; break;
100
case 大于等于14小于18 :
101
env = new AfterNoon(); break;
102
case 大于等于18小于24 :
103
env = new Night(); break;
104
default:
105
env = new Envirpment();
106
break;
107
}
108
}
109
}
110
111
public class Enviroment
112
{
113
public virtual string HelloWords()
114
{
115
}
116
117
public void 碰面(MM mm, Me i)
118
{
119
i.SayToMM(this, mm);
120
}
121
}
122
123
public class Night : Enviroment
124
{
125
public override string HelloWords()
126
{
127
return "晚上好!";
128
}
129
}
130
131
public class Morning : Enviroment
132
{
133
public override string HelloWords()
134
{
135
return "早上好!";
136
}
137
}
138
139
public class Noon : Enviroment
140
{
141
public override string HelloWords()
142
{
143
return "中午好!";
144
}
145
}
146
147
public class AfterNoon : Enviroment
148
{
149
public override string HelloWords()
150
{
151
return "下午好!";
152
}
153
}
部分地方,使用说明和伪码表示。
using System;2
using System.Collections;3
using System.Data ;4

5
public class MyClass6
{7
public static void Main()8
{9
Enviroment env = EnvFactory.GeTCurrentEnv(DateTime.Now );10
11
MM mm = new MM( true , true )12
13
Me i = new Me();14
15
env.碰面( mm , i );16
17
RL();18
}19
20
Helper methods38
}39

40
public class MM41
{42
public MM()43
{44
//MMPropertyAndGoodWords.xml从动态构建MM的属性45
}46
47
public MM(bool is新发型 , bool is穿新衣服)48
{49
//动态构建MM的属性50
51
//设置Is新发型 = is新发型;52
//Is穿新衣服 = is穿新衣服; 53
}54
//public bool Is卷发 = false;55
//public bool Is穿新衣服 = false; 56
}57

58
public class Me59
{ 60
public void SayToMM(Enviroment ev, MM mm)61
{62
SayHello(ev);63
SayGood();64
}65
66
public void SayHello(Enviroment ev)67
{68
Console.WriteLine ( ev.HelloWords() );69
}70
71
public void SayGood(MM mm)72
{73
//读配置文件74
DateSet ds = new DataSet();75
ds.ReadXml(Application.StartupPath + "\\MMPropertyAndGoodWords.xml");76
foreach( Property prt in mm.Properties)77
{78
if( prt == true )79
{80
DataRow dr = ((DataRow[])ds.Tables[0].Select("Property='" + prt.ToString()+"'"))[0] ;81
Console.WriteLine ( dr["赞美词"] );82
}83
}84
}85
}86

87

88
public class EnvFactory89
{90
public Enviroment GeTCurrentEnv(DateTime time)91
{92
Enviroment env = null;93
94
switch( time.Hour)95
{96
case 大于0小于12 :97
env = new Morning(); break;98
case 大于等于12小于14 :99
env = new Moon() ; break;100
case 大于等于14小于18 :101
env = new AfterNoon(); break;102
case 大于等于18小于24 :103
env = new Night(); break;104
default:105
env = new Envirpment();106
break;107
}108
}109
}110

111
public class Enviroment112
{ 113
public virtual string HelloWords()114
{115
}116
117
public void 碰面(MM mm, Me i)118
{ 119
i.SayToMM(this, mm);120
}121
}122

123
public class Night : Enviroment124
{125
public override string HelloWords()126
{127
return "晚上好!";128
}129
}130

131
public class Morning : Enviroment132
{133
public override string HelloWords()134
{135
return "早上好!";136
}137
}138

139
public class Noon : Enviroment140
{141
public override string HelloWords()142
{143
return "中午好!";144
}145
}146

147
public class AfterNoon : Enviroment148
{149
public override string HelloWords()150
{151
return "下午好!";152
}153
}请各位指教!
附:程序文件https://files.cnblogs.com/huangzhenwu/BoyMM.rar


浙公网安备 33010602011771号