为了账号安全,请及时绑定邮箱和手机立即绑定

单一职责原则和代码可读性

单一职责原则和代码可读性

PHP
繁花不似锦 2022-09-17 21:54:15
在试图坚持单一责任规则的同时,我的课程已经开始看起来像这样$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,还是我偏离了目标?
查看完整描述

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


希望这会帮助你!祝您编码愉快!


查看完整回答
反对 回复 2022-09-17
  • 1 回答
  • 0 关注
  • 186 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号