背景
一個實體在不同的上下文中具備不同的職責,如:產品在“生產完成上下文”中具備的一些職責,在“質檢相關上下文”中具備另外一些職責。四色原型、DIC和“UML事物模式”在不同的維度闡述了這一情況,在代碼層面到底該如何表達呢?本文給出了一些思路。
六種實現方式
因為:MI(Manufacture和QualityTesting)和Context(ManufactureContext、QualityTestingBeginningContext和QualityTestingCompletingContext)都是空實現且每種風格中的代碼都一樣,后面只給出跟PPT和Role相關的代碼。
第一種:未顯式體現角色的模式。
類圖
代碼
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DCIStudy.V1 8 { 9 class Product 10 { 11 public void CompleteManufacture(ManufactureContext context) { } 12 13 public void BeginQualityTesting(QualityTestingBeginningContext context) { } 14 15 public void CompleteQualityTesting(QualityTestingCompletingContext context) { } 16 } 17 }
第二種:使用“顯式接口”顯式體現角色的模式。
類圖
代碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DCIStudy.V2 { interface IManufactureProduct { void CompleteManufacture(ManufactureContext context); } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DCIStudy.V2 { interface IQualityTestingProduct { void BeginQualityTesting(QualityTestingBeginningContext context); void CompleteQualityTesting(QualityTestingCompletingContext context); } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DCIStudy.V2 { class Product : IManufactureProduct, IQualityTestingProduct { void IManufactureProduct.CompleteManufacture(ManufactureContext context) { } void IQualityTestingProduct.BeginQualityTesting(QualityTestingBeginningContext context) { } void IQualityTestingProduct.CompleteQualityTesting(QualityTestingCompletingContext context) { } } }
第三種:使用“擴張方法”顯式體現角色的模式。
類圖
代碼
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DCIStudy.V3 8 { 9 static class ManufactureProductExtentions 10 { 11 public static void CompleteManufacture(this Product that, ManufactureContext context) { } 12 } 13 } 14 15 using System; 16 using System.Collections.Generic; 17 using System.Linq; 18 using System.Text; 19 using System.Threading.Tasks; 20 21 namespace DCIStudy.V3 22 { 23 static class QualityTestingProductExtentions 24 { 25 public static void BeginQualityTesting(Product that, QualityTestingBeginningContext context) { } 26 27 public static void CompleteQualityTesting(Product that, QualityTestingCompletingContext context) { } 28 } 29 }
第四種:使用“領域服務”顯式體現角色的模式。
類圖
代碼
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DCIStudy.V4 8 { 9 class ManufactureProductService 10 { 11 public void CompleteManufacture(Product product, ManufactureContext context) { } 12 } 13 } 14 15 using System; 16 using System.Collections.Generic; 17 using System.Linq; 18 using System.Text; 19 using System.Threading.Tasks; 20 21 namespace DCIStudy.V4 22 { 23 class QualityTestingProductService 24 { 25 public void BeginQualityTesting(Product product, QualityTestingBeginningContext context) { } 26 27 public void CompleteQualityTesting(Product product, QualityTestingCompletingContext context) { } 28 } 29 }
第五種:使用“包裝類型”顯式體現角色的模式。
類圖
代碼
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DCIStudy.V5 8 { 9 class ManufactureProduct 10 { 11 private Product _product; 12 13 public ManufactureProduct(Product product) 14 { 15 _product = product; 16 } 17 18 public void CompleteManufacture(ManufactureContext context) { } 19 } 20 } 21 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 25 using System.Text; 26 using System.Threading.Tasks; 27 28 namespace DCIStudy.V5 29 { 30 class QualityTestingProduct 31 { 32 private Product _product; 33 34 public QualityTestingProduct(Product product) 35 { 36 _product = product; 37 } 38 39 public void BeginQualityTesting(QualityTestingBeginningContext context) { } 40 41 public void CompleteQualityTesting(QualityTestingCompletingContext context) { } 42 } 43 }
第六種:使用“動態代理”顯式體現角色的模式。
時間不夠了,這種實現方式需要獨立寫一篇文章。
如何設計Context?
PPT對應的Role會參與到一個到多個Context中,一般來說一個Context涉及一個MI,如果MI為“Moment”,多數情況需要一個Context,如果MI為“Interval”,多數情況需要兩個Context,根據MI的業務生命周期不同,所需的Context也不同。
備注
倉促寫完,還沒有具體深入分析如何做出不同的選擇和折中,群里有朋友實戰過,有機會再寫一篇這樣的文章。