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

 

目录

背景六种实现方式第一种:未显式体现角色的模式。第二种:使用“显式接口”显式体现角色的模式。第三种:使用“扩张方法”显式体现角色的模式。第四种:使用“领域服务”显式体现角色的模式。第五种:使用“包装类型”显式体现角色的模式。第六种:使用“动态代理”显式体现角色的模式。如何设计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也不同。

备注返回目录

仓促写完,还没有具体深入分析如何做出不同的选择和折中,群里有朋友实战过,有机会再写一篇这样的文章。

Node.js的颠覆者:PHP的Swoole扩展

 
      最近2年Node.js很火,异步与协程是网络开发方面热门的话题。在追求新技术的同时,也应该反思下这里面存在的陷阱。Node.js确实是一门有趣好玩有个性的语言和技术,动态性,全异步回调的方式,闭包等等特性。值得所有程序员尝试一下。
 
     但本文将介绍的是一门另外的容易被大家轻视的编程语言:PHP。长期以来PHP都是作为一门为Web开发而生的前端脚本语言。PHP极其的简单,你可以在一周只能掌握它,一月内开发出一个功能丰富的网站。发展至今PHP其实已经完全不再是一门简单的语言了,PHP的功能保罗万象,常用的操作系统功能,如进程管理,信号,网络通信,多线程,ptrace、inotify、加密解密、压缩都有相应的扩展实现,而且PHP可以很好的与C/C++互相调用。PHP提供了ZendAPI,可以很方便地使用C来扩充PHP的功能。语言特性方面PHP5.4提供的namespace,phar打包,composer依赖管理,Trait,完整的面向对象编程语法,强大的魔术方法和常量,字符串与函数类对象直接转换,闭包和匿名函数等丰富的语言特性。在后端开发方面强大到堪比Java,C#,但开发效率更高。


PHP对比Node.js的优势:

1、PHP开发效率更高

PHP比Node.js更简单直接,这一点有点像C了。使用PHP开发一个功能,几乎是所有语言中效率最高的,没有之一。
 

2、PHP程序员更多

PHP因为比较容易入门的原因,程序员数量远超其他语言。其他语言程序员也有很大一部分会PHP。
 

3、PHP开源项目多

PHP有大量开源的项目,有各种第三方库
 
Node.js最大的特色之一是内置了异步高性能的Socket Server/Client实现,在此基础上提供了内置的Web服务器。PHP里也有类似的神器,那就是Swoole扩展。使用Swoole扩展完全可以开发出一个高性能安全稳定的服务器程序来。丝毫不逊于Node.js,而且在某些方面比Node.js更强大。
 
Swoole使用C语言编写,以PHP扩展的方式来运行。Swoole的网络IO部分基于epoll/kqueue事件循环,是全异步非阻塞的。业务逻辑部分使用多进程同步阻塞方式来运行。这样既保证了Server能够应对高并发和大量TCP连接。又保证业务代码仍然可以简单的编写。
 

Swoole对比Node.js的优势:

1、swoole是原生支持多进程/多线程的

开发者只需要修改一个参数,配置下要启动多少个进程即可。而Node.js的网络库本身并没有提供多进程/多线程的实现。开发者需要自行创建进程。或者干脆使用单线程。这样无法充分利用多核。


2、swoole使用消息传递+多Worker进程,而不是多线程+共享内存+加锁

,共享内存的性能虽然很好,但存在安全问题,需要读写时加锁。锁的粒度过大会导致只有一个线程在运行。锁太复杂又会有死锁问题。所以开发者需要非常谨慎小心。


3、swoole的代码编写是同步,而不是嵌套异步回调

Node.js的代码如果太复杂,会嵌套多层回调,使代码丧失可读性,程序流程变得很乱。Swoole使用了传统Linux下半同步半异步多Worker的实现方式。业务代码按照同步方式编写,更简单。
Swoole也内置了Socket客户端的实现,但采用的是同步+并行方式来执行。PHP本身也提供了socket的功能,但某几个函数存在一些bug,而且比较复杂。Swoole内置的客户端类更加安全和简化。


4、swoole内置了Node.js所没有的额外特性

如CPU Affinity设置,守护进程化,混合UDP/TCP多端口监听,多定时器等。

Swoole项目地址:

原文地址:https://www.cnblogs.com/Leo_wl/p/3287271.html