1 回答
TA贡献1863条经验 获得超2个赞
你完全远离()。如何在代码中完全看不到工作。没关系,你有他们负责不同工作的课程。可能是或我猜,它们是通过尊重来实现的。除了假设之外,SRP 在代码中的可见性要低得多。SRPSingle Responsibility PrincipleSRPSRP
在 中,类依赖于其他类。这是完全正常的。 在您的代码中完全可见。但是你不能像构建复杂结构时那样通过构造函数方法来维护。这应该是以下方式的一些内容:OOPDependency InjectionDependency Injection
<?php
// given string is not empty
$nonEmptyString = new NonEmptyString('/tmp/xyzk7kjnbrukhg');
// validates for string to be a path
$path = new Path($nonEmptyString);
// validates given string is a valid file path
$filePath = new FilePath($path);
// validates given file is uploaded file
$uploadedFile = new UploadedFile($filePath);
// checks given item is a readable file / permissions check
$readableFile = new ReadableFile($uploadedFile);
// makes sure given file is an image file
$imageFile = new ImageFile($readableFile);
// holds all rules for product image only
$productImage = new ProductImage($imageFile);
但这也不是正确的方法。要以正确的方式执行此操作,您需要使用 。 实际创建其他对象。假设您有一个工厂方法模式实现,并且该实现将负责创建具有依赖项的对象。假设您在以下代码段中导入了所需的所有类:Factory Method Design PatternFactory Method Design PatternImageFileProductImageImageFile
<?php
class ImageFileFactory implements FactoryInterface
{
public static function make($string)
{
// given string is not empty
$nonEmptyString = new NonEmptyString($string);
// validates for string to be a path
$path = new Path($nonEmptyString);
// validates given string is a valid file path
$filePath = new FilePath($path);
// validates given file is uploaded file
$uploadedFile = new UploadedFile($filePath);
// checks given item is a readable file / permissions check
$readableFile = new ReadableFile($uploadedFile);
// makes sure given file is an image file
return new ImageFile($readableFile);
}
}
// Creates ImageFile instance
$imageFile = ImageFileFactory::make('/tmp/xyzk7kjnbrukhg');
// holds all rules for product image only
$productImage = new ProductImage($imageFile);
哦!我有一个写在媒体上.如果你可以读它。这是战略调整计划的链接SRP
希望这会帮助你!祝您编码愉快!
- 1 回答
- 0 关注
- 186 浏览
添加回答
举报
