公告
目前研究技术WCF,extJS,Ajax,JQuery,Ioc
|
|
| 31 | 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 | 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
常用链接
留言簿
我参与的团队
随笔档案
最新评论

阅读排行榜
评论排行榜

2008年8月27日
读了一些设计模式,发现不是设计模式难读,而是难以应用,在以前的项目中,只用到过命令模式,单件模式和MVC模式
想要融会贯通,合理应用GOF23中的设计模式,那么必须在写每一行代码以前做思考
深刻体验到了思考后编码的重要性今天记录下策略模式,写下一篇就当是学完以后的巩固
我从来不记笔记,因为我从来认为自己记代码的能力过目不忘,我想也应该改变下了
-------------------思考和记录
策略模式,我的理解是封装变化
这个例子,是我在台球房的时候突然想到的
他是这么计算消费金额的,上午12点以前是15块一个小时,12点到下午16点是18块一个小时,16点以后是25块一个小时
当然我假设他这样,其实他还分周末,平时,一三五,二四六
继续设计这样一个简单功能,那么结帐的时候,肯定会有GetPrice(int playTime)这个方法,
方法的实现无非是得到了消费金额,当然不同时段用到的计算金额的变法不相同,专业术语谓之 算法
所以说,策略模式的作用就是,封装算法,让其相互独立
写一个类

Code
public abstract class Price
{
private int playTime;
protected int PlayTime
{
get { return playTime; }
set { playTime = value; }
}
public abstract int GetPrice(int playTime);
}
计算消费金额的类,通过抽象类来具体实现
12点以前消费计算算法

Code
public class BeforeTwelve : Price
{
public override int GetPrice(int playTime)
{
return 15 * playTime;
}
}
12点到16点消费计算算法

Code
public class AfterTwelveBeforSixteen : Price
{
public override int GetPrice(int playTime)
{
return 18 * playTime;
}
}
16点以后消费计算算法

Code
public class AfterSixteen : Price
{
public override int GetPrice(int playTime)
{
return 25 * playTime;
}
}
应用场景类

Code
public class AccountContext
{
Price myPrice;
public AccountContext(Price myPrice)
{
this.myPrice = myPrice;
}
public void WritePrice(int playTime)
{
Console.WriteLine(myPrice.GetPrice(playTime));
}
}
客户端调用

Code
static void Main(string[] args)
{
AccountContext context1 = new AccountContext(new BeforeTwelve());
context1.WritePrice(1);
AccountContext context2 = new AccountContext(new AfterTwelveBeforSixteen());
context2.WritePrice(2);
AccountContext context3 = new AccountContext(new AfterSixteen());
context3.WritePrice(3);
}
优点比较明显,如果现在客户要求增加一个算法22点以后,每小时30块,那么只需要增加一个price类的继承类
实现算法,供客户端调用就可以了,而不需要在大堆逻辑代码中查找修改了
以上纯熟个人对策略模式的看法
posted @
2008-08-27 10:18 Ray Gu 阅读(10) |
评论 (0) |
编辑

2008年8月25日
桥接模式
满足类的单一职责性,实现纵向和横向扩展,应对多维变化,符合了开放-封闭原则,增强可扩展性,降低偶合关系
一个应用例子 发送短消息
短消息可以是电脑发送给电脑的,比如即时通信软件
也可以是电脑发送给手机的,通过GMS网络发送
------暂时称其为纵向发展
当然不一定只是电脑和手机,也有可能是其他通信设备
短消息的形式可以分为彩色信息,就是所谓的彩信
也可以是普通的文字信息
------暂时称其为横向发展
当然不一定只是短信和彩信,当然还可能是视频信息(3G)
通常编码者(非设计者)是这样理解这个过程的
message--> MessageToComputer -->CommonMessage
-->MulticolorMessage
message--> MessageToPhone -->CommonMessage
-->CommonMessage
这样做的缺点很显而易见:
假如增加一个发信息给其他通信设备的例子
那么必须增加一个类 MessageToOther
必然必须增加CommonMessage,CommonMessage两个类
如果想增机一个发视频信息的功能
那么必须在每一个MessageToXX的类下面增加VideoMessage的类
增加N个的话就是个非常麻烦的事情,代码的重复利用性大大降低,扩展性降低,强偶合明显
通过桥接模式,可以如此理解(见代码)

Code
public abstract class MessageForm
{
public abstract void Execute(string message);
}
public class CommonMessage : MessageForm
{
public override void Execute(string message)
{
Console.WriteLine("短消息内容为:" + message);
}
}
public class MulticolorMessage : MessageForm
{
public override void Execute(string message)
{
Console.WriteLine("彩信内容为:" + message);
}
}
上述代码短消息形式,即上面提到的横向扩展
下述代码即纵向扩展

Code
public abstract class Message
{
protected MessageForm myMessageForm;
public MessageForm MyMessageForm
{
set { myMessageForm = value; }
}
public virtual void Send(string message)
{
this.myMessageForm.Execute(message);
}
}
public class MessageToComputer : Message
{
public override void Send(string message)
{
this.myMessageForm.Execute(message);
}
}
public class MessageToPhone : Message
{
public override void Send(string message)
{
this.myMessageForm.Execute(message);
}
}
protected MessageForm myMessageForm;
public MessageForm MyMessageForm
{
set { myMessageForm = value; }
}
这个就是桥的作用拉,连接交错,降低偶合
这些就是个人对桥接模式的理解了,并不深刻,因为本人也只是编码者,非设计者也,但在努力成为设计者
纯熟当作个人学习笔记的记录
最后给上客户端调用

Code
static void Main(string[] args)
{
Message myMessage = new MessageToComputer();
myMessage.MyMessageForm = new MulticolorMessage();
myMessage.Send("你好!测试发送至手机已成功");
}
posted @
2008-08-25 11:07 Ray Gu 阅读(7) |
评论 (0) |
编辑

2008年7月3日
不想多说了,经过反复实验,绝对不会有乱码问题,和导出数据量大情况下的没反映问题
当然,是通过查看很多网上代码和园子里的高人代码,最后找到的解决办法
而且在03和07中均可性的方案
1
if (dtData != null)
2
{
3
DataGrid dgExport = new DataGrid();
4
dgExport.DataSource = dtData;
5
dgExport.DataBind();
6
dgExport.AllowPaging = false;
7
System.Web.HttpResponse httpResponse = Page.Response;
8
httpResponse.Clear();
9
httpResponse.Buffer = true;
10
httpResponse.Charset = "gb2312";
11
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssms") + ".xls";
12
httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
13
httpResponse.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
14
httpResponse.ContentType = "application/ms-excel";
15
System.IO.StringWriter tw = new System.IO.StringWriter();
16
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
17
dgExport.RenderControl(hw);
18
string filePath = Server.MapPath("..") + fileName;
19
System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
20
sw.Write(tw.ToString());
21
sw.Close();
22
DownFile(httpResponse, fileName, filePath);
23
httpResponse.End();
24
}
25
下面是关键
1
private bool DownFile(System.Web.HttpResponse Response, string fileName, string fullPath)
2
{
3
System.IO.FileStream fs = System.IO.File.OpenRead(fullPath);
4
try
5
{
6
Response.ContentType = "application/octet-stream";
7
Response.AppendHeader("Content-Disposition", "attachment;filename=" +
8
HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ";charset=GB2312");
9
long fLen = fs.Length;
10
int size = 102400;//每100K同时下载数据
11
byte[] readData = new byte[size];//指定缓冲区的大小
12
if (size > fLen) size = Convert.ToInt32(fLen);
13
long fPos = 0;
14
bool isEnd = false;
15
while (!isEnd)
16
{
17
if ((fPos + size) > fLen)
18
{
19
size = Convert.ToInt32(fLen - fPos);
20
readData = new byte[size];
21
isEnd = true;
22
}
23
fs.Read(readData, 0, size);//读入一个压缩块
24
if (readData.Length > 0)
25
Response.BinaryWrite(readData);
26
fPos += size;
27
}
28
return true;
29
}
30
catch
31
{
32
return false;
33
}
34
finally
35
{
36
fs.Close();
37
System.IO.File.Delete(fullPath);
38