在网上看到很多关于设计模式的文章。但是基本都是单独讲解某一个设计模式的应用场景和设计思路。可是真实的项目中,可能仅仅只按照书上的某种设计模式就能写出优雅的代码吗?我觉得是不可能的。我这篇文章希望跟大家分享的是我思考的过程,而非结果。首先,应该对书上的20多种的设计模式有了解,大概清楚它们都是怎么样一个设计思路。建议有想学习设计模式的Friends还是买本纸质书系统的学习。下面,我给出一个真实的项目来引出我对这个项目的思考。第一次写博客分享我的思路,在加上我的思维有些女性思维,是跳跃性的。可能有些表达不是很清楚。望大家见谅。
class="csharpcode"> /// <summary> /// 处理流程基类 /// </summary> public abstract class ProcessFlowBase : IProcessFlow { protected ProcessFlowBase(ProcessFlowBase processFlowWhenRight = default(ProcessFlowBase), ProcessFlowBase processFlowWhenWrong = default(ProcessFlowBase)) { this.NextProcessFlowWhenRight = processFlowWhenRight; this.NextProcessFlowWhenWrong = processFlowWhenWrong; this.IsRight = true; this.IsEnd = false; } protected IDbConnection DbConnection { get; set; } protected IDbCommand DbCommand { get; set; } protected IDbTransaction DbTransaction { private get; set; } /// <summary> /// 当前流程是否处理通过 /// </summary> private bool IsRight { get; set; } /// <summary> /// 流程是否结束 /// </summary> private bool IsEnd { get; set; } /// <summary> /// 下一个流程,当流程处理操作通过的时候 /// </summary> private ProcessFlowBase NextProcessFlowWhenRight { set; get; } /// <summary> /// 下一个流程,当流程处理操作不通过的时候 /// </summary> private ProcessFlowBase NextProcessFlowWhenWrong { get; set; } /// <summary> /// 处理 /// </summary> /// <param name="dataContext"></param> public void Process(DataContext dataContext) { this.ProcessTemplate(dataContext); this.RecordLog(dataContext); if (this.IsEnd) return; if (this.IsRight) { if (this.NextProcessFlowWhenRight == default(ProcessFlowBase)) return; this.NextProcessFlowWhenRight.SetParameters(this.DbConnection, this.DbCommand, this.DbTransaction); this.NextProcessFlowWhenRight.Process(dataContext); } else { if (this.NextProcessFlowWhenWrong == default(ProcessFlowBase)) return; this.NextProcessFlowWhenWrong.SetParameters(this.DbConnection, this.DbCommand, this.DbTransaction); this.NextProcessFlowWhenWrong.Process(dataContext); } } /// <summary> /// 流程处理模板方法,供子类实现 /// </summary> /// <param name="dataContext"></param> protected abstract void ProcessTemplate(DataContext dataContext); /// <summary> /// 记录日志,供子类实现 /// </summary> /// <param name="dataContext"></param> protected virtual void RecordLog(DataContext dataContext) { LogHelper.Record(dataContext); } /// <summary> /// 结束本次流程 /// </summary> protected void EndProcessFlow() { this.IsEnd = true; } /// <summary> /// 确定下一流程,后续候选流程名单在调用构造函数的时候已经初始化好 /// </summary> /// <param name="rightFlowOrWrongFlow"></param> protected void SetNextFlow(RightFlowOrWrongFlow rightFlowOrWrongFlow) { this.IsRight = rightFlowOrWrongFlow == RightFlowOrWrongFlow.Right; } /// <summary> /// 回滚操作流程 /// </summary> protected void RollBackProcessFlow() { this.DbTransaction.Rollback(); this.DbConnection.Close(); } /// <summary> /// 提交事物操作 /// </summary> protected void CommitProcessFlow() { this.DbTransaction.Commit(); this.DbConnection.Close(); } /// <summary> /// 设置下一流程单例属性 /// </summary> /// <param name="param"></param> private void SetParameters(params object[] param) { var dbConnection = param[0] as IDbConnection; var dbCommand = param[1] as IDbCommand; var dbTransaction = param[2] as IDbTransaction; this.DbConnection = dbConnection; this.DbCommand = dbCommand; this.DbTransaction = dbTransaction; } }
/// <summary> /// 检验授权码流程,此流程应当作为所有业务流程的入口流程。会在此类中做一些特别的处理 /// </summary> public class CheckToken : ProcessFlowBase { public CheckToken(ProcessFlowBase processFlowWhenRight = default(ProcessFlowBase), ProcessFlowBase processFlowWhenWrong = default(ProcessFlowBase)) : base(processFlowWhenRight, processFlowWhenWrong) { //这个流程作为所有业务处理的入口流程,所以,我在这里要写入一段初始化流程事务对象的代码,仅执行一次 base.DbConnection = LogHelper.CreateDbConnection(); base.DbConnection.Open(); base.DbCommand = base.DbConnection.CreateCommand(); base.DbTransaction = base.DbConnection.BeginTransaction(); } protected override void ProcessTemplate(DataContext dataContext) { //查询授权码是否存在 var sbSelectSql = new StringBuilder(); sbSelectSql.AppendFormat(@"Some SQL Here", dataContext.AuthCode, dataContext.PartCode); dataContext.SQL = base.DbCommand.CommandText = sbSelectSql.ToString(); var any = Convert.ToInt32(base.DbCommand.ExecuteScalar()); if (any > 0) { base.SetNextFlow(RightFlowOrWrongFlow.Right); } else { dataContext.StatusCode = "1001"; base.RollBackProcessFlow(); base.EndProcessFlow(); } } /// <summary> /// 记录日志 /// </summary> /// <param name="dataContext"></param> protected override void RecordLog(DataContext dataContext) { } }
/// <summary> /// 读取包含数据的XML文件 /// </summary> public class LoadXElement : ProcessFlowBase { public LoadXElement(ProcessFlowBase processFlowWhenRight = default(ProcessFlowBase), ProcessFlowBase processFlowWhenWrong = default(ProcessFlowBase)) : base(processFlowWhenRight, processFlowWhenWrong) { } protected override void ProcessTemplate(DataContext dataContext) { try { using (var memoryStream = new MemoryStream(dataContext.Buffer)) { dataContext.DataXElement = XElement.Load(memoryStream); } var folderPath = HttpContext.Current.Server.MapPath("/ModuleSources/FileStore/AppFile/TunnelMeasure/Surface"); if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath); var xmlPath = string.Format( folderPath + "/{0}_01_{1}.xml", dataContext.PartCode, DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss")); using (var fileStream = new FileStream(xmlPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { dataContext.DataXElement.Save(fileStream); } dataContext.XMLPath = xmlPath; } catch (Exception) { //读取文件流失败 dataContext.StatusCode = "1002"; base.RollBackProcessFlow(); base.EndProcessFlow(); return; } base.SetNextFlow(RightFlowOrWrongFlow.Right); } }
最后想说的就是设计模式都是死的,就好像风云里无名的万剑归宗。当忘了所有的形式,无招胜有招。在上述的例子中,其实并没有严格完全按照书上的设计模式的范例。其中可能有模板方法、职责链、策略模式、状态模式等等啦。在中国合伙人中,孟晓骏说他们80后那一代人最重要的是改变。我觉得,我们90后这一代人最重要的是质疑。有太多的陋规和成见被一代一代传下来。我们要怀着质疑的心态去看世间万物。就好像曾经某届人人大代表说中国房价还不够高。当时觉得那人是24K纯StringBuilder可是当我读了《市场的力量》之后,想法有些许的改变了。有个例子。中国古代一发生天灾人祸时,就闹饥荒。百姓饿死的很多。有些黑心的商人就囤积居奇。朝廷为了稳定粮价,稳住民生。就严格调控粮价。结果是什么呢?结果是有更多的百姓饿死。因为那些商人看到朝廷如此打压粮价。索性就不卖了。他们也许知道这是刚性需求。百姓不得不妥协。那如果,朝廷不出面干预,就会有人看到商机,从外面运进粮食出售,从一定程度上缓解了粮食危机。这时的中国政府就是个反面例子。那还有一个例子就是二战后的德国,民不聊生,人民连维持基本生命需求的食物都不能满足。结果导致黑市横行。政府怎么管都无法消灭黑市。这时一个聪明的办法出来了。美国政府跟黑市一起搞。那市民还是觉得政府会更可靠吧。黑市逐渐的也就消亡了。美国的政治智慧还是很伟大的。核心思想就是妥协,也就是保持平衡。折射到中国房地产,也许先进的中国房价还不够高。政府如何调控,房价都不会有明显的下降。即便是房价下来了,那商人们会不会在其他地方增加隐形收费来弥补房价下跌的这个缺口呢?我想会的。也许中国房价在高些,高得大部分人买不起了。市场机制就展现出他的力量了。市场倒逼价格下降,服务优化。还是多看历史吧。也许我的见解也很片面。