๊ฐ๋ฐฉ-ํ์ ์์น(Open-Closed Principle, OCP)
1. ๊ฐ๋ฐฉ ํ์ ์์น(Open-Closed Principle)์ด๋?
1988๋ Bertrand Meyer๊ฐ ๊ทธ์ ์ ์ "Object Oriented Software Construction"์์ ์ฒ์ ์๊ฐํ ์ด ์์น์ ๋ค์๊ณผ ๊ฐ์ด ์ ์๋๋ค.
software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
"์ํํธ์จ์ด ์ํฐํฐ(ํด๋์ค, ๋ชจ๋, ํจ์ ๋ฑ)๋ ํ์ฅ์ ๋ํด์๋ ์ด๋ ค ์์ด์ผ ํ๊ณ , ์์ ์ ๋ํด์๋ ๋ซํ ์์ด์ผ ํ๋ค."
- ํ์ฅ์ ์ด๋ ค์๋ค(Open for extension): ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ ์ ์์ด์ผ ํจ
- ์์ ์ ๋ซํ์๋ค(Closed for modification): ๊ธฐ์กด ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ์ง ์์์ผ ํจ
2. ์ฅ์
- ์ ์ง๋ณด์์ฑ ํฅ์
- ๊ธฐ์กด ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ์ง ์์ผ๋ฏ๋ก ๋ฒ๊ทธ ๋ฐ์ ์ํ์ด ์ค์ด๋ ๋ค.
- ๊ธฐ์กด ์ฝ๋ ์ฌ์ปดํ์ผ์ด๋ ์ฌ๋ฐฐํฌ๊ฐ ํ์ ์๋ค.
- ํ์ฅ์ฑ ๊ฐ์
- ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ ๋ ๊ธฐ์กด ์ฝ๋๋ฅผ ์์ ํ์ง ์์๋ ๋๋ค.
- ์๋ก์ด ์ฝ๋๋ง ์์ฑํ์ฌ ๊ธฐ๋ฅ์ ์ถ๊ฐํ ์ ์๋ค.
- ํ
์คํธ ์ฉ์ด์ฑ
- ๊ธฐ์กด ์ฝ๋๊ฐ ๋ณ๊ฒฝ๋์ง ์์ผ๋ฏ๋ก ๊ธฐ์กด ํ ์คํธ๊ฐ ๊นจ์ง์ง ์๋๋ค.
- ์๋ก์ด ๊ธฐ๋ฅ์ ๋ํ ํ ์คํธ๋ง ์ถ๊ฐํ๋ฉด ๋๋ค.
3. ํ๋ ์ํํธ์จ์ด์์์ OCP ํ์ฉ
ํ๋ฌ๊ทธ์ธ ์ํคํ ์ฒ
ํ๋ฌ๊ทธ์ธ ์์คํ ์ OCP์ ์ด์์ ์ธ ๊ตฌํ ์ฌ๋ก์ด๋ค. Eclipse, IntelliJ, Vim, Minecraft ๋ฑ ์๋ง์ ํด์ด ํ๋ฌ๊ทธ์ธ ๊ธฐ๋ฐ์ผ๋ก ์ค๊ณ๋์ด ํ์ฅ์ฑ์ ๊ทน๋ํํ๊ณ ์๋ค.
- ํต์ฌ ์์คํ
๊ณผ ํ๋ฌ๊ทธ์ธ์ ๋ถ๋ฆฌ
- ํต์ฌ ์์คํ : ๊ธฐ๋ณธ ๊ธฐ๋ฅ๊ณผ ์ธํฐํ์ด์ค ์ ์
- ํ๋ฌ๊ทธ์ธ: ์ค์ ๊ตฌํ์ฒด๋ค
- ํ์คํ๋ ์ธํฐํ์ด์ค
- ๋ชจ๋ ํ๋ฌ๊ทธ์ธ์ด ๋ฐ๋ผ์ผ ํ๋ ๊ณตํต ๊ณ์ฝ
- ํต์ฌ ์์คํ ๊ณผ ํ๋ฌ๊ทธ์ธ ๊ฐ์ ์ํต ๋ฐฉ์ ์ ์
- ๋์ ํ์ฅ์ฑ
- ์์คํ ์์ ์์ด ์๋ก์ด ๊ธฐ๋ฅ ์ถ๊ฐ ๊ฐ๋ฅ
- ๊ธฐ์กด ์ฝ๋ ๋ณ๊ฒฝ ์์ด ์๋ก์ด ํ๋ฌ๊ทธ์ธ ์ถ๊ฐ ๊ฐ๋ฅ
์์ ์ฝ๋ - ํ๋ฌ๊ทธ์ธ ๊ธฐ๋ฐ ์ค๊ณ ๋ฐฉ์
// ํต์ฌ ์์คํ
์ธํฐํ์ด์ค
public interface Plugin {
void execute();
}
// ํต์ฌ ์์คํ
- ํ๋ฌ๊ทธ์ธ์ ๋ก๋ํ๊ณ ์คํ
public class PluginSystem {
private List<Plugin> plugins = new ArrayList<>();
// ํ๋ฌ๊ทธ์ธ ๋ฑ๋ก
public void registerPlugin(Plugin plugin) {
plugins.add(plugin);
}
// ๋ชจ๋ ํ๋ฌ๊ทธ์ธ ์คํ
public void runPlugins() {
for (Plugin plugin : plugins) {
plugin.execute();
}
}
}
// ํ๋ฌ๊ทธ์ธ 1
public class GreetingPlugin implements Plugin {
@Override
public void execute() {
System.out.println("Hello, world!");
}
}
// ํ๋ฌ๊ทธ์ธ 2
public class FarewellPlugin implements Plugin {
@Override
public void execute() {
System.out.println("Goodbye, world!");
}
}
// ํด๋ผ์ด์ธํธ ์ฝ๋
public class Main {
public static void main(String[] args) {
PluginSystem system = new PluginSystem();
// ํ๋ฌ๊ทธ์ธ ๋ฑ๋ก
system.registerPlugin(new GreetingPlugin());
system.registerPlugin(new FarewellPlugin());
// ํ๋ฌ๊ทธ์ธ ์คํ
system.runPlugins();
}
}
์๋ก์ด ํ๋ฌ๊ทธ์ธ์ ์ถ๊ฐํ๋ ค๋ฉด Plugin ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค๋ฅผ ์์ฑํ๊ณ , ์ด๋ฅผ PluginSystem์ ๋ฑ๋กํ๊ธฐ๋ง ํ๋ฉด ๋๋ค.
๋ฐ๋ผ์ ๊ธฐ์กด ์ฝ๋๋ฅผ ์์ ํ์ง ์๊ณ ๋ ํ์ฅ์ด ๊ฐ๋ฅํ๋ค.
์ด์ฒ๋ผ OCP๋ฅผ ์ ์ค์ํ๋ฉด ์์คํ ์ ํ์ฅ์ฑ๊ณผ ์ ์ง๋ณด์์ฑ์ ๊ทน๋ํํ ์ ์๋ค.
์ฐธ๊ณ ์๋ฃ :
https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle
Open–closed principle - Wikipedia
From Wikipedia, the free encyclopedia Concept in object-oriented programming The open-closed principle was introduced by Bertrand Meyer. In object-oriented programming, the open–closed principle (OCP) states "software entities (classes, modules, function
en.wikipedia.org
https://blog.cleancoder.com/uncle-bob/2014/05/12/TheOpenClosedPrinciple.html
Clean Coder Blog
The Open Closed Principle 12 May 2014 In 1988 Bertrand Meyer defined one of the most important principles of software engineering. The Open Closed Principle (OCP). In his book Object Oriented Software Construction[1] he said: A satisfactory modular decompo
blog.cleancoder.com
ํด๋ฆฐ ์ํคํ ์ฒ: ์ํํธ์จ์ด ๊ตฌ์กฐ์ ์ค๊ณ์ ์์น(2019) - (๋ก๋ฒํธ C. ๋งํด ์ /์ก์ค์ด ์ญ)