개발공부/CS

OOP의 5가지 설계 원칙

카슈밀 2025. 2. 15. 23:17
반응형

SOLID 

 

  • SRP(Single Responsibility Principle): 단일 책임 원칙
  • OCP(Open Closed Priciple): 개방 폐쇄 원칙
  • LSP(Listov Substitution Priciple): 리스코프 치환 원칙
  • ISP(Interface Segregation Principle): 인터페이스 분리 원칙
  • DIP(Dependency Inversion Principle): 의존 역전 원칙

 

  • SRP(Single Responsibility Principle): 단일 책임 원칙
    • 클래스는 단 하나의 목적을 가지고, 하나의 기능에 하나의 책임을 가져야한다.

위반 :

문제점: 리포트 생성과 출력이 하나의 클래스에서 처리됨.

class Report {
    public void generateReport() { /* 리포트 생성 */ }
    public void printReport() { /* 리포트 출력 */ }
}

개선:

class ReportGenerator {
    public void generateReport() { /* 리포트 생성 */ }
}

class ReportPrinter {
    public void printReport() { /* 리포트 출력 */ }
}

 

  • OCP(Open Closed Priciple): 개방 폐쇄 원칙
    • 확장에는 열려있고 수정에는 닫혀있어야 한다.

위반:

class PaymentProcessor {
    public void processPayment(String type) {
        if (type.equals("credit")) { /* 신용카드 결제 */ }
        else if (type.equals("paypal")) { /* 페이팔 결제 */ }
    }
}

 

개선: 

interface Payment {
    void pay();
}

class CreditCardPayment implements Payment {
    public void pay() { /* 신용카드 결제 */ }
}

class PayPalPayment implements Payment {
    public void pay() { /* 페이팔 결제 */ }
}

class PaymentProcessor {
    public void processPayment(Payment payment) {
        payment.pay();
    }
}

 

  • LSP(Listov Substitution Priciple): 리스코프 치환 원칙
    • 하위 클래스는 상위 클래스를 대체할 수 있어야 한다.

위반:

class Bird {
    public void fly() { System.out.println("날고 있어!"); }
}

class Penguin extends Bird { }

개선:

abstract class Bird { }

class FlyingBird extends Bird {
    public void fly() { System.out.println("날고 있어!"); }
}

class Penguin extends Bird { /* 펭귄은 날지 않음 */ }

 

  • ISP(Interface Segregation Principle): 인터페이스 분리 원칙
    • 인터페이스는 클라이언트가 필요한 것만 제공해야한다.

위반:

interface Worker {
    void work();
    void eat();
}
class Robot implements Worker {
    public void work() { /* 로봇이 일함 */ }
    public void eat() { /* 로봇은 안 먹는데? */ }
}

 

개선:

interface Workable {
    void work();
}

interface Eatable {
    void eat();
}

class Human implements Workable, Eatable {
    public void work() { /* 사람 일함 */ }
    public void eat() { /* 사람 밥 먹음 */ }
}

class Robot implements Workable {
    public void work() { /* 로봇이 일함 */ }
}

 

 

  • DIP(Dependency Inversion Principle): 의존 역전 원칙
    • 고수준 모듈(비즈니스 로직)이 저수준 모듈(세부 구현)에 의존하지 말고, 추상화(인터페이스)에 의존해야 한다.

위반:

class Keyboard { }
class Monitor { }

class Computer {
    private Keyboard keyboard;
    private Monitor monitor;

    public Computer() {
        this.keyboard = new Keyboard();
        this.monitor = new Monitor();
    }
}

 

개선:

interface Keyboard { }
interface Monitor { }

class MechanicalKeyboard implements Keyboard { }
class LEDMonitor implements Monitor { }

class Computer {
    private Keyboard keyboard;
    private Monitor monitor;

    public Computer(Keyboard keyboard, Monitor monitor) {
        this.keyboard = keyboard;
        this.monitor = monitor;
    }
}

 

 

 

정리

 

원칙 핵심 개념 해결 방법

SRP (단일 책임 원칙) 하나의 클래스는 하나의 역할만 해야 함 클래스별로 역할을 분리

OCP (개방-폐쇄 원칙) 기존 코드를 수정하지 않고 확장 가능해야 함 다형성(인터페이스, 상속) 활용

LSP (리스코프 치환 원칙) 하위 클래스가 상위 클래스를 대체 가능해야 함 올바른 상속 구조 설계

ISP (인터페이스 분리 원칙) 클라이언트가 필요 없는 메서드에 의존하지 않아야 함 인터페이스를 작게 나누기

DIP (의존 역전 원칙) 고수준 모듈이 저수준 모듈의 구현이 아닌 추상화에 의존해야 함 인터페이스 활용, 의존성 주입

 

 

728x90

'개발공부 > CS' 카테고리의 다른 글

Docker란?  (0) 2025.02.15
API란?  (0) 2025.02.15
TDD: 테스트 주도 개발.  (0) 2025.02.15
RESTful API?  (0) 2025.02.15
OOP란?  (0) 2025.02.15