第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

單一職責原則和代碼可讀性

單一職責原則和代碼可讀性

PHP
繁花不似錦 2022-09-17 21:54:15
在試圖堅持單一責任規(guī)則的同時,我的課程已經(jīng)開始看起來像這樣$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'                                        )                                    )                                )                            )                        )                    )                );這只是一個示例。從表面上看,它看起來很酷,因為它提供了非常簡單且可測試的類。但是正如您可以注意到代碼的可讀性或可用性一樣。我需要編寫無數(shù)行代碼,甚至需要處理上傳文件的簡單初始化(如上面的代碼所示)。我開始覺得有些不對勁,我誤解了單一責任原則的概念。是如何處理每個類的單一責任的純OOP,還是我偏離了目標?
查看完整描述

1 回答

?
小唯快跑啊

TA貢獻1863條經(jīng)驗 獲得超2個贊

你完全遠離()。如何在代碼中完全看不到工作。沒關系,你有他們負責不同工作的課程??赡苁腔蛭也拢鼈兪峭ㄟ^尊重來實現(xiàn)的。除了假設之外,SRP 在代碼中的可見性要低得多。SRPSingle Responsibility PrincipleSRPSRP


在 中,類依賴于其他類。這是完全正常的。 在您的代碼中完全可見。但是你不能像構(gòu)建復雜結(jié)構(gòu)時那樣通過構(gòu)造函數(shù)方法來維護。這應該是以下方式的一些內(nèi)容: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);

但這也不是正確的方法。要以正確的方式執(zhí)行此操作,您需要使用 。 實際創(chuàng)建其他對象。假設您有一個工廠方法模式實現(xiàn),并且該實現(xiàn)將負責創(chuàng)建具有依賴項的對象。假設您在以下代碼段中導入了所需的所有類: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); 

哦!我有一個寫在媒體上.如果你可以讀它。這是戰(zhàn)略調(diào)整計劃的鏈接SRP


希望這會幫助你!祝您編碼愉快!


查看完整回答
反對 回復 2022-09-17
  • 1 回答
  • 0 關注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號