posts - 5,  comments - 17,  trackbacks - 0
  2008年8月27日

    读了一些设计模式,发现不是设计模式难读,而是难以应用,在以前的项目中,只用到过命令模式,单件模式和MVC模式

    想要融会贯通,合理应用GOF23中的设计模式,那么必须在写每一行代码以前做思考

    深刻体验到了思考后编码的重要性今天记录下策略模式,写下一篇就当是学完以后的巩固

    我从来不记笔记,因为我从来认为自己记代码的能力过目不忘,我想也应该改变下了

                                                                                                          -------------------思考和记录

 

    策略模式,我的理解是封装变化

    这个例子,是我在台球房的时候突然想到的

    他是这么计算消费金额的,上午12点以前是15块一个小时,12点到下午16点是18块一个小时,16点以后是25块一个小时

    当然我假设他这样,其实他还分周末,平时,一三五,二四六

 

   继续设计这样一个简单功能,那么结帐的时候,肯定会有GetPrice(int playTime)这个方法,

   方法的实现无非是得到了消费金额,当然不同时段用到的计算金额的变法不相同,专业术语谓之 算法

   所以说,策略模式的作用就是,封装算法,让其相互独立

 

   写一个类

Code

  计算消费金额的类,通过抽象类来具体实现

 

  12点以前消费计算算法

Code

 

  12点到16点消费计算算法

Code

 

   16点以后消费计算算法

Code

 

  应用场景类

Code

 

  客户端调用

Code

 

  优点比较明显,如果现在客户要求增加一个算法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

 

  上述代码短消息形式,即上面提到的横向扩展

 

  下述代码即纵向扩展

Code

 

            protected MessageForm myMessageForm;

            public MessageForm MyMessageForm
            {
                set { myMessageForm = value; }
            }

  这个就是桥的作用拉,连接交错,降低偶合

 

  这些就是个人对桥接模式的理解了,并不深刻,因为本人也只是编码者,非设计者也,但在努力成为设计者

 

  纯熟当作个人学习笔记的记录

 

  最后给上客户端调用

 

Code
posted @ 2008-08-25 11:07 Ray Gu 阅读(7) | 评论 (0)编辑
  2008年7月8日

write a entity class

 1public class Student
 2{
 3    private int id;
 4
 5    public int Id
 6    {
 7        get return id; }
 8        set { id = value; }
 9    }

10    private string name;
11
12    public string Name
13    {
14        get return name; }
15        set { name = value; }
16    }

17    private string score;
18
19    public string Score
20    {
21        get return score; }
22        set { score = value; }
23    }

24}

A method to get some data
 1private Student[] GetStudent()
 2    {
 3        Student[] students = new Student[2];
 4        Student stu = new Student();
 5        stu.Id = 1;
 6        stu.Name = "zhangsan";
 7        stu.Score = "60";
 8        students.SetValue(stu, 0);
 9        Student stu2 = new Student();
10        stu2.Id = 2;
11        stu2.Name = "lisi";
12        stu2.Score = "80";
13        students.SetValue(stu2, 1);
14        return students;
15    }

比较方法获取匹配数据
 1private Student[] Compare(string name)
 2    {
 3        Student[] stu = this.GetStudent();
 4        System.Collections.ArrayList al = new System.Collections.ArrayList();
 5        if (!String.IsNullOrEmpty(name))
 6        {
 7            foreach (Student stud in stu)
 8            {
 9                if (stud.Name.IndexOf(name) > -1)
10                {
11                    al.Add(stud);
12                }

13            }

14        }

15        else
16        {
17            return this.GetStudent();
18        }

19        return (Student[])al.ToArray(typeof(Student));
20    }

测试事件
1protected void Button1_Click(object sender, EventArgs e)
2    {
3        this.GridView1.DataSource = this.Compare(this.TextBox1.Text);
4        this.GridView1.DataBind();
5    }

这个例子实现的功能是从GridView里查找和Textbox里模糊匹配的数据集
posted @ 2008-07-08 11:58 Ray Gu 阅读(23) | 评论 (1)编辑
  2008年7月3日
不想多说了,经过反复实验,绝对不会有乱码问题,和导出数据量大情况下的没反映问题
当然,是通过查看很多网上代码和园子里的高人代码,最后找到的解决办法
而且在03和07中均可性的方案

 1if (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

下面是关键
 1private 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