您的位置:首页 > 编程语言 > PHP开发

DDD:四色原型中Role的 “六” 种实现方式和PHP的Swoole扩展

2013-08-28 15:17 337 查看

目录

背景六种实现方式第一种:未显式体现角色的模式。第二种:使用“显式接口”显式体现角色的模式。第三种:使用“扩张方法”显式体现角色的模式。第四种:使用“领域服务”显式体现角色的模式。第五种:使用“包装类型”显式体现角色的模式。第六种:使用“动态代理”显式体现角色的模式。如何设计Context?备注

背景返回目录

一个实体在不同的上下文中具备不同的职责,如:产品在“生产完成上下文”中具备的一些职责,在“质检相关上下文”中具备另外一些职责。四色原型、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也不同。

备注返回目录

仓促写完,还没有具体深入分析如何做出不同的选择和折中,群里有朋友实战过,有机会再写一篇这样的文章。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: