在網上看到很多關於設計模式的文章。但是基本都是單獨講解某一個設計模式的應用場景和設計思路。可是真實的項目中,可能僅僅只按照書上的某種設計模式就能寫出優雅的代碼嗎?我覺得是不可能的。我這篇文章希望跟大家分享的是我思考的過程,而非結果。首先,應該對書上的20多種的設計模式有了解,大概清楚它們都是怎麼樣一個設計思路。建議有想學習設計模式的Friends還是買本紙質書系統的學習。下面,我給出一個真實的項目來引出我對這個項目的思考。第一次寫博客分享我的思路,在加上我的思維有些女性思維,是跳躍性的。可能有些表達不是很清楚。望大家見諒。
/// <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); } }
可是當我讀了《市場的力量》之後,想法有些許的改變了。有個例子。中國古代一發生天災人禍時,就鬧饑荒。百姓餓死的很多。有些黑心的商人就囤積居奇。朝廷為了穩定糧價,穩住民生。就機。這時的中國政府就是個反面例子。那還有一個例子就是二戰後的德國,民不聊生,人民連維持基本生命需求的食物都不能滿足。結果導致黑市橫行。政府怎麼管都無法消滅黑市。這時一個聰明的辦法出來了。美國政府跟黑市一起搞。那市民還是覺得政府會更可靠吧。黑市逐漸的也就消亡了。美國的政治智慧還是很偉大的。核心思想就是妥協,也就是保持平衡。折射到中國房地產,也許先進的中國房價還不夠高。政府如何調控,房價都不會有明顯的下降。即便是房價下來了,那商人們會不會在其他地方增加隱形收費來彌補房價下跌的這個缺口呢?我想會的。也許中國房價在高些,高得大部分人買不起了。市場機制就展現出他的力量了。市場倒逼價格下降,服務優化。還是多看歷史吧。也許我的見解也很片面。