Bob大叔提出并发扬了S.O.L.I.D五大原则,用来更好地进行面向对象编程,五大原则分别是:

  1. The Single Responsibility Principle(单一职责SRP)
  2. The Open/Closed Principle(开闭原则OCP)
  3. The Liskov Substitution Principle(里氏替换原则LSP)
  4. The Interface Segregation Principle(接口分离原则ISP)
  5. The Dependency Inversion Principle(依赖反转原则DIP)

单一职责SRP:

单一职责的描述如下:

A class should have only one reason to change
类发生更改的原因应该只有一个

一个类(JavaScript下应该是一个对象)应该有一组紧密相关的行为的意思是什么?遵守单一职责的好处是可以让我们很容易地来维护这个对象,当一个对象封装了很多职责的话,一旦一个职责需要修改,势必会影响该对象想的其它职责代码。通过解耦可以让每个职责工更加有弹性地变化。

不过,我们如何知道一个对象的多个行为构造多个职责还是单个职责?我们可以通过参考Object Design: Roles, Responsibilies, and Collaborations一书提出的Role Stereotypes概念来决定,该书提出了如下Role Stereotypes来区分职责:

  1. Information holder – 该对象设计为存储对象并提供对象信息给其它对象。
  2. Structurer – 该对象设计为维护对象和信息之间的关系
  3. Service provider – 该对象设计为处理工作并提供服务给其它对象
  4. Controller – 该对象设计为控制决策一系列负责的任务处理
  5. Coordinator – 该对象不做任何决策处理工作,只是delegate工作到其它对象上
  6. Interfacer – 该对象设计为在系统的各个部分转化信息(或请求)

一旦你知道了这些概念,那就很容易知道你的代码到底是多职责还是单一职责了。

开闭原则OCP

 

开闭原则的描述是:

Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.
软件实体(类,模块,方法等等)应当对扩展开放,对修改关闭,即软件实体应当在不修改的前提下扩展。

open for extension(对扩展开放)的意思是说当新需求出现的时候,可以通过扩展现有模型达到目的。而Close for modification(对修改关闭)的意思是说不允许对该实体做任何修改,说白了,就是这些需要执行多样行为的实体应该设计成不需要修改就可以实现各种的变化,坚持开闭原则有利于用最少的代码进行项目维护。

 

 里氏替换原则LSP

 Liskov于1987年提出了一个关于继承的原则“Inheritance should ensure that any property proved about supertype objects also holds for subtype objects.”——“继承必须确保超类所拥有的性质在子类中仍然成立。”也就是说,当一个子类的实例应该能够替换任何其超类的实例时,它们之间才具有is-A关系。

  该原则称为Liskov Substitution Principle——里氏替换原则

接口分离原则ISP

Clients should not be forced to depend upon interfaces that they do not use.
不能强迫用户去依赖那些他们不使用的接口。换句话说,使用多个专门的接口比使用单一的总接口总要好。
它包含了2层意思:
- 接口的设计原则:接口的设计应该遵循最小接口原则,不要把用户不使用的方法塞进同一个接口里。
如果一个接口的方法没有被使用到,则说明该接口过胖,应该将其分割成几个功能专一的接口。
- 接口的依赖(继承)原则:如果一个接口a依赖(继承)另一个接口b,则接口a相当于继承了接口b的方法,那么继承了接口b后的接口a也应该遵循上述原则:不应该包含用户不使用的方法。
反之,则说明接口a被b给污染了,应该重新设计它们的关系。

如果用户被迫依赖他们不使用的接口,当接口发生改变时,他们也不得不跟着改变。换而言之,一个用户依赖了未使用但被其他用户使用的接口,当其他用户修改该接口时,依赖该接口的所有用户都将受到影响。这显然违反了开闭原则,也不是我们所期望的。

 

依赖倒置原则(Dependence Inversion Principle,DIP)的原始定义: 

 

 

  • 高层模块不应该依赖底层模块,两者都应该依赖其抽象;
  • 抽象不应该依赖细节;
  • 细节应该依赖抽象。


 

 

posted @ 2012-01-20 09:45 chenleinet 阅读(6) 评论(0) 编辑

开始做了,才认识到wcf和webservice的又一个本质区别,wcf支持接口可以让我做到与服务端的解耦合。技术就是这样,做一做,才能理解的更好,更深入。

互联网都往java方向转,让我想去做企业了。 

posted @ 2011-11-04 19:22 chenleinet 阅读(12) 评论(0) 编辑

在ie下,需要在ajax之前,设置settimeout函数,设置超时处理函数,不超时的话,利用ajax的回调函数进行cleartimeout 的调用,取消超时处理函数的调用。

在火狐下,如果请求过长,则火狐的js引擎会立即中断。

但为了兼容多种浏览器,还是做超时设置好一些

posted @ 2011-08-26 16:50 chenleinet 阅读(9) 评论(0) 编辑

xml文件主要结构如下

<config>

<item key="test" value="test"/>

 

<item key="test" value="test"/>

 

</config>

一 读取

XmlDocument xmlDoc = new XmlDocument();

  xmlDoc.Load(fileName);

二 转换到list

先建立一个实体类

 

public  class QueueXml
    {
      
public string Key
      {
          
get;
          
set;
      }

      
public string Value
      {
          
get;
          
set;
      }
    }

 

然后

 

 List<QueueXml> list = new List<QueueXml>();
        
            xmlDoc.Load(fileName);

            foreach (XmlNode node in xmlDoc.SelectNodes("/Config/Item"))
            {
                QueueXml queueXml = new QueueXml();
                queueXml.Key=node.Attributes["key"].Value;
                queueXml.Value=node.Attributes["value"].Value;
                list.Add(queueXml);
            }

 

这样就可以了。

三 寻找节点

 string xPath=string.Format("Config/Item[@key='{0}']",“test”)

 XmlNode node=xmlDoc.SelectSingleNode(xPath);

四 移除节点并保存

string xPath=string.Format("Config/Item[@key='{0}']",“test”)

 XmlNode node=xmlDoc.SelectSingleNode(xPath);
   xmlDoc.SelectSingleNode("/Config").RemoveChild(node);
    this.xmlDoc.Save(fileName);

五 建立节点并保存

 

 XmlAttribute attrKey= xmlDoc.CreateAttribute("key");
                    attrKey.Value = str;
                    XmlAttribute attrValue= xmlDoc.CreateAttribute("value");
                    attrValue.Value = str;
                   
                    XmlNode node= xmlDoc.CreateElement("Item");
                    node.Attributes.Append(attrKey);
                    node.Attributes.Append(attrValue);

                    xmlDoc.SelectSingleNode("/Config").AppendChild(node);

                    this.xmlDoc.Save(fileName);

 

posted @ 2011-07-19 15:11 chenleinet 阅读(16) 评论(0) 编辑

  private void ExecuteCMD(string cmd)
        {
         
            Process process = new Process();

            try
            {
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
               // process.StartInfo.CreateNoWindow = true;

                process.Start();
                process.StandardInput.WriteLine(cmd);
                process.StandardInput.WriteLine("exit");
                process.WaitForExit();

                string readProOutResult = process.StandardOutput.ReadToEnd();

            }
            catch (Exception ex)
            {
                MessageBox.Show("操作失败");
            }
            finally
            {
                process.Close();
            }

posted @ 2011-07-19 15:03 chenleinet 阅读(49) 评论(0) 编辑

自己写的一个批处理文件用于windows服务的安装管理

@echo off
:manager
echo 1=安装,2=开始,3=卸载,4=挂起
set /p para=
if %para%==1 goto install
if %para%==2 sc start 读取队列
if %para%==3 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -u bin\Debug\EmailService.exe
if %para%==4 sc stop 读取队列
pause
goto end
:install
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe bin\Debug\EmailService.exe
sc start 读取队列
pause
goto end

posted @ 2011-07-19 15:01 chenleinet 阅读(55) 评论(0) 编辑

自己写的一个批处理文件用于windows服务的安装管理

@echo off
:manager
echo 1=安装,2=开始,3=卸载,4=挂起
set /p para=
if %para%==1 goto install
if %para%==2 sc start 读取队列
if %para%==3 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -u bin\Debug\EmailService.exe
if %para%==4 sc stop 读取队列
pause
goto end
:install
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe bin\Debug\EmailService.exe
sc start 读取队列
pause
goto end

posted @ 2011-07-19 15:00 chenleinet 阅读(44) 评论(0) 编辑

 

换了新公司,有了新的开始。

现在开始用消息队列,做邮件的异步发送。

本系统,通过webservice访问本地消息队列,没有用wcf的消息队列访问,也没有用公共队列。

不过由于有webservice的支持,访问本地消息队列,没有任何的问题。

以下为队列操作代码

 

 /// <summary>
    
/// 队列操作类
    
/// </summary>
   public  class OperateQueue:Interface.IStore
    {
       
/// <summary>
       
/// 建立队列
       
/// </summary>
       
/// <param name="queueName">队列名称</param>
       
/// <returns>是否成功</returns>
       private  ResultInfo<bool> CreateQueue(string queueName)
       {
           ResultInfo
<bool> result = new ResultInfo<bool>();
           result.Data 
= false;
           result.Result 
= false;

           
try
           {
              MessageQueue.Create(queueName, 
true);
              result.Data 
= true;
              result.Result 
= true;
           }
           
catch(Exception ex)
           {
               
throw ex;
           }

           
return result;
       }

       
/// <summary>
       
/// 判断队列是否存在
       
/// </summary>
       
/// <param name="queueName">队列名字</param>
       
/// <returns></returns>
       private  bool IsQueueExists(string queueName)
       {
           
return MessageQueue.Exists(queueName);
       }

       
/// <summary>
       
/// 发送消息
       
/// </summary>
       
/// <param name="queueName">队列名称</param>
       
/// <returns>是否成功</returns>
       public ResultInfo<bool> StoreMessage(string queueName, string message)
       {
           
if (!this.IsQueueExists(queueName))
           {
               
this.CreateQueue(queueName);
           }

           MessageQueue queue 
= new MessageQueue() ;
           ResultInfo
<bool> result = new ResultInfo<bool>();
           result.Data 
= false;
           result.Result 
= false;

           MessageQueueTransaction mts 
= new MessageQueueTransaction();
           
           
try
           {
               mts.Begin();

               queue
= new MessageQueue(queueName);

               queue.Send(message,mts);

               result.Data 
= true;
               result.Message 
= "消息存储成功";
               result.Result 
= true;

               mts.Commit();
           }
           
catch (Exception ex)
           {
               mts.Abort();
               
throw ex;
           }
           
finally
           {
               queue.Close();
               queue.Dispose();
           }

           
return result;
       }

       
/// <summary>
       
/// 读取消息
       
/// </summary>
       
/// <param name="queueName">队列名称</param>
       
/// <returns>读取到的消息</returns>
       public  ResultInfo<string> ReadMessage(string queueName)
       {
           ResultInfo
<string> result = new ResultInfo<string>();
           result.Data 
= string.Empty;
           result.Result 
= false;

           MessageQueue queue 
= new MessageQueue();

           
try
           {
               queue 
= new MessageQueue(queueName);
               Message message 
= new Message();
               
               message 
= queue.Receive();
               message.Formatter 
= new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });

               result.Data 
= message.Body.ToString();
               result.Result 
= true;
           }
           
catch(Exception ex)
           {
               result.Message 
= ex.Message;   
           }
           
finally
           {
               queue.Close();
               queue.Dispose();
           }

           
return result;
       }
    }

 

采用了队列的事务操作,istore用来与上层调用进行隔离,这样做是为了保证,当需要使用其它消息队列时,做到最小改动。

webservice和队列部署在同一台服务器上,本打算利用

  System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(@"FormatName:DIRECT=tcp:192.168.16.85\private$\Action");

去进行局域网内的访问,但是没有成功,所以暂时放弃。

posted @ 2011-07-19 14:56 chenleinet 阅读(32) 评论(0) 编辑

花了一年时间,在英孚学了英语从0级到6级(是英孚的级别)感觉还是不错的,至少还是能跟老外扯几句了,看英文文档也还不错了,不过词汇量还需要提高,所以继续看视频,继续看文挡,提高词汇量和听力。

学英语不一定要去外企,我学英语是为了更好的学习外国的技术,毕竟外国的编程技术要比国内的高出很多。

所以在以后,我的所学能用在技术学习上甚至能为我带来经济利益,则我这一年的时间,没有白费。

这一年,为了英语,放弃了几乎所有的技术学习计划,造成当年的计划,无一实现。

希望以后,重新开始技术学习后,能把英语所学,运用其中。

晚上是一定要学习的,不管学啥,肯定要学,切不可用肥皂剧,游戏打发时间,人生有限,当努力,适度的看些肥皂剧,玩些游戏可以,但不可用全部的时间。

 

posted @ 2011-05-22 16:25 chenleinet 阅读(26) 评论(0) 编辑
摘要: 今天又见识了一个新错误,数据库是mysql 的,在旧表导入新表时,出现,唯一列,不能插入重复值问题,感觉很诡异,旧表是不可能有重复值的,于是怀疑新表,因为新表在导入之前,已经有了一些值,于是决定,彻底删除新表的值,但结果依然。故开始分析错误提示,发现,问题竟然是旧表与新表的字段,长度不一致,造成。因为旧表在往新表导入时,旧表的字段大于新表的字段长度,新表于是做了截取,结果截取的部分,就与旧表中的一些数据,重复了。然后调整字段长度后,问题解决。于是,今天又知道了一种造成唯一键冲突的原因,特记于此。阅读全文
posted @ 2011-04-08 19:48 chenleinet 阅读(21) 评论(0) 编辑