# 클린 코더스 강의 14.1. OCP(Open-Closed Principle)

### **정의**: 타에는 열려있고 변경에는 닫혀있다.

#### OCP 위반 예제

![](/files/-LpLg5q6F1-oY75zUrjV)

새로운 타입의 `readConsole` 이 추가된다면 `copy` 함수는 변경을 요구하게 된다.

#### OCP 로 해결방법

![](/files/-LpLgcY57nXKggnJAvPh)

즉, 새로운 변경이 일어나 Copy 함수는 변경되지 않는다.

**OCP 를 사용할 경우 의존성의 방향**

![](/files/-LpLhFjZ0XI6ehXhusJR)

![](/files/-LpLh_RlIzN6bPG7qWUP)

main partition: main 함수에는 if else 가 존재하게 된다.\
Crystal ball: 우리는 미래를 예측할 수 없다.

![](/files/-LpLiGYIE4uO8T3TS6nd)

![](/files/-LpLiTBlh1gycAa3Yg6q)

## 나의 생각

`OCP`와 `LSP`는 밀접하게 연관되어 있다. \
OCP 원칙이 어긋났다는 것은 LSP 원칙을 선행해서 어긋나고 있는 것이다.

OCP 의 문제 중 Main partition 은 `생성 - 사용` 관계에서 모듈안에서의 `if ~ else` 를 제거하기 위하여 `main` 함수로 밀어내는 과정이다. \
Main 함수까지 `if ~ else` 를 밀어내면 유연한 변화를 가진 모듈이라고 말할 수 있으며 객체지향 설계의 숙련도를 측정할 수 있는 단위이다.

Crystall ball 의 문제는 LSP 와 연결되어 있다고 생각한다.\
**특정 역할을 수행하는 `Abstraction` 이 함부로 구체화된 내용을 포함하고 있지 않아야 한다**와 비슷한 것 같다.&#x20;

현 시대의 소프트웨어는 최종 배포가 코앞에 있어도 유연하게 변경될 수 있는 설계를 요구한다. 앞서 말했듯이 `Unknwon Unknowns` 상황이 항상 존재하기 때문에 유연한 변경을 수용할 수 있는 Agile design 을 해야 한다.&#x20;

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kimtaesu.gitbook.io/workspace/undefined/14.1.-ocp-open-closed-principle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
