SRP(Single Responsibility Principle)

A class should have only one reason to change. // ํด๋ž˜์Šค๊ฐ€ ๋ณ€๊ฒฝ๋  ์ด์œ ๋Š” ๋‹จ ํ•˜๋‚˜์—ฌ์•ผ ํ•œ๋‹ค.

Example

์•„๋ž˜์˜ Book์ด๋ผ๋Š” ํด๋ž˜์Šค๋Š” ๋งค์šฐ ๋…ผ๋ฆฌ์ ์ด๊ณ  ๊ฐ„๋‹จํ•œ ํด๋ž˜์Šค์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ œ๋ชฉ์„ ์–ป์„ ์ˆ˜๋„ ์žˆ๊ณ , ์ž‘๊ฐ€, ๋‹ค์ŒํŽ˜์ด์ง€, ํ˜„์žฌ ํŽ˜์ด์ง€ ๋‚ด์šฉ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

class Book {
 
    function getTitle() {
        return "A Great Book";
    }
 
    function getAuthor() {
        return "John Doe";
    }
 
    function turnPage() {
        // pointer to next page
    }
 
    function printCurrentPage() {
        echo "current page content";
    }
}

ํ•˜์ง€๋งŒ ์ž‘์€ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•ด๋ณด์•˜์„๋•Œ Book ํด๋ž˜์Šค๋ฅผ ๋‹ค๋ฃจ๋Š” ํ–‰์œ„์ž๊ฐ€ ์ฑ…์„ ๊ด€๋ฆฌํ•˜๋Š” ์‚ฌ๋žŒ์ด๊ฑฐ๋‚˜, ์ฑ…์„ ๋ณด์—ฌ์ฃผ๋Š” ์‚ฌ๋žŒ ๋‘ ๋ถ„๋ฅ˜๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘ ๋ถ„๋ฅ˜์˜ ํ–‰์œ„์ž๊ฐ€ ์›ํ•˜๋Š” bussiness logic์ด Bookํด๋ž˜์Šค์— ์„ž์—ฌ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ๋Š” SRP๋ฅผ ์œ„๋ฐ˜ํ•œ ๊ฒฝ์šฐ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

class Book {
 
    function getTitle() {
        return "A Great Book";
    }
 
    function getAuthor() {
        return "John Doe";
    }
 
    function turnPage() {
        // pointer to next page
    }
 
    function getCurrentPage() {
        return "current page content";
    }
 
}
 
interface Printer {
 
    function printPage($page);
}
 
class PlainTextPrinter implements Printer {
 
    function printPage($page) {
        echo $page;
    }
 
}
 
class HtmlPrinter implements Printer {
 
    function printPage($page) {
        echo '<div style="single-page">' . $page . '</div>';
    }
 
}

๋‘ ๊ฐ€์ง€์˜ bussiness logic์„ ๋ถ„๋ฅ˜ํ•จ์œผ๋กœ์จ design์˜ ์œ ์—ฐ์„ฑ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜์˜ ๊ฒฝ์šฐ ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋œ Book์ž…๋‹ˆ๋‹ค.

class Book {
 
    function getTitle() {
        return "A Great Book";
    }
 
    function getAuthor() {
        return "John Doe";
    }
 
    function turnPage() {
        // pointer to next page
    }
 
    function getCurrentPage() {
        return "current page content";
    }
 
    function save() {
        $filename = '/documents/'. $this->getTitle(). ' - ' . $this->getAuthor();
        file_put_contents($filename, serialize($this));
    }
 
}

์šฐ๋ฆฌ๊ฐ€ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐ”๊ฟ€๋• Book ํด๋ž˜์Šค๋ฅผ ๋ฐ”๊ฟ”์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์šฐ๋ฆฌ๊ฐ€ ๋‹ค์Œ ํŽ˜์ด์ง€๋ฅผ ๋„˜๊ธฐ๋Š” ๊ณผ์ •์„ ๋ฐ”๊ฟ€๋• Book ํด๋ž˜์Šค๋ฅผ ๋ฐ”๊ฟ”์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ํด๋ž˜์Šค๋ฅผ ๋ฐ”๊ฟ”์•ผํ•˜๋Š” ์ด์œ ๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€์˜ ๋ณ€๊ฒฝ์˜ ์ถ•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

save๋ฅผ SimpleFilePersistence ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌ์‹œ์ผœ ๊ฐ ํด๋ž˜์Šค๊ฐ€ ๋ณ€๊ฒฝ๋  ์ด์œ ๋ฅผ ์ตœ์†Œํ™”ํ•ฉ๋‹ˆ๋‹ค.

class Book {
 
    function getTitle() {
        return "A Great Book";
    }
 
    function getAuthor() {
        return "John Doe";
    }
 
    function turnPage() {
        // pointer to next page
    }
 
    function getCurrentPage() {
        return "current page content";
    }
 
}
 
class SimpleFilePersistence {
 
    function save(Book $book) {
        $filename = '/documents/' . $book->getTitle() . ' - ' . $book->getAuthor();
        file_put_contents($filename, serialize($book));
    }
}

๊ฒฐ๋ก 

  • SRP์„ ์ง€ํ‚ด์œผ๋กœ์จ low coupled design์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
  • ํ•˜์ง€๋งŒ ๊ณผ๋„ํ•œ SRP๋Š” ์ตœ์ ํ™” ๋””์ž์ธ ๋Œ€์‹  ๋ฏธ์™„์„ฑ๋œ ์ตœ์ ํ™” ๋””์ž์ธ์œผ๋กœ ์ด๋Œ ์ˆ˜ ์žˆ๋‹ค.
  • ๋˜ํ•œ ๋ถ„์‚ฐ๋œ ์ž‘์€ class๋กœ ์ธํ•ด ์ดํ•ดํ•˜๊ธฐ ํž˜๋“  ๋””์ž์ธ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.

Reference