title: **"**OCP : The Open Closed Principle**"**
description: "OCP : The Open Closed Principle"
cleanUrl: /sw-engineer/ocp
ogImage: "<https://anyflower.notion.site/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F5ee81007-eab4-4186-973f-4bda7be30678%2FUntitled.png?table=block&id=632955d5-aa2e-41fd-9636-4a90affce3aa&spaceId=7570d2fc-66b1-4e23-bb3c-ff7b56842b0d&width=770&userId=&cache=v2>"
floatFirstTOC: right

Definition

OCP violated sample

Untitled

Class Client
{
    void DoCommunicate(Server1 server1) { //.. }
    //...
}
 
//타 server(Server2)를 참조해야 할 경우의 변경
Class Client
{
    void DoCommunicate(Server2 server2) { //.. }
    //...
}

OCP complied sample

Untitled

Class Server1 : IClient {}
 
Class Client
{
    void DoCommunicate(IClient server) { //.. }
    //...
}
 
//새 기능을 담당할 Server2. client 변경 필요 무
Class Server2 : IClient {}

Usage examples for OCP

Conclusion