【发布时间】:2020-05-28 18:40:42
【问题描述】:
在尝试坚持单一职责规则时,我的课程开始看起来像这样
$productImage = new ProductImage(// holds all rules for product image only
new ImageFile( // makes sure given file is an image file
new ReadableFile( // checks given item is a readable file / permissions check
new UploadedFile( // validates given file is uploaded file
new FilePath( // validates given string is a valid file path
new Path( // validates for string to be a path
new NonEmptyString( // given string is not empty
'/tmp/xyzk7kjnbrukhg'
)
)
)
)
)
)
);
这只是一个示例。 从表面上看,它看起来很酷,因为它提供了非常简单和可测试的类。但是您会注意到代码的可读性或可用性很差。我需要编写无数行代码,甚至处理上传文件的简单初始化(如上面的代码所示)。
我开始觉得有些不对劲,我误解了单一责任原则的概念。
是如何处理对每个类单独负责的纯 OOP 还是我离题了?
【问题讨论】:
-
你已经走了。
标签: php architecture solid-principles single-responsibility-principle