# 2. OCP

## Open/Closed Principle (OCP; 개방-폐쇄 원칙)

> 소프트웨어 요소는 확장에는 열려 있으나, 변경에는 단혀 있어야 하다.

모듈 중 하나를 수정했는데, 그 모듈을 사용하는 모든 모듈의 코드를 수정하는 일이 있으면 안된다는 뜻이다.

조금만 떠올려봐도 얼마나 끔찍한 일인 지 알 수 있다.

### **확장에 대해 열려 있다.**

> 이것은 모듈의 동작을 확장할 수 있다는 것을 의미한다.&#x20;
>
> 애플리케이션의 요구 사항이 변경될 때, 이 변경에 맞게 새로운 동작을 추가해 모듈을 확장할 수 있다. 즉, 모듈이 하는 일을 변경할 수 있다.

### **수정에 대해 닫혀 있다.**

> 모듈의 소스 코드나 바이너리 코드를 수정하지 않아도 모듈의 기능을 확장하거나 변경할 수 있다.
>
> 그 모듈의 실행 가능한 바이너리 형태나 링크 가능한 라이브러리 (예를 들어, Windows의 DLL, 자바의 jar ...)를 건드릴 필요가 없다.

### 추상화

객체 지향 프로그래밍 언어에서는 "추상화"를 통해서 가능할 수 있다.

추상화 클래스나 인터페이스를 만들어, 상위 클래스는 **수정할 수 없도록 만들되** 서브 클래스에서는 기능을 추가하여 **확장할 수 있도록 만들기** 때문이다.

## Links

* [https://ko.wikipedia.org/wiki/SOLID\_(객체\_지향\_설계)](https://ko.wikipedia.org/wiki/SOLID_\(%EA%B0%9D%EC%B2%B4_%EC%A7%80%ED%96%A5_%EC%84%A4%EA%B3%84\))
* [https://ko.wikipedia.org/wiki/개방-폐쇄\_원칙](https://ko.wikipedia.org/wiki/%EA%B0%9C%EB%B0%A9-%ED%8F%90%EC%87%84_%EC%9B%90%EC%B9%99)


---

# 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://devlog.joonas.io/design-pattern/solid-priciple/2-ocp.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.
