OOP(๊ฐ์ฒด์ง€ํ–ฅ์„ค๊ณ„)

๊ฐœ๋ฐฉ-ํ์‡„ ์›์น™(Open-Closed Principle, OCP)

9taetae9 2025. 1. 20. 08:00
728x90

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. ์žฅ์ 

  1. ์œ ์ง€๋ณด์ˆ˜์„ฑ ํ–ฅ์ƒ
    • ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ฒ„๊ทธ ๋ฐœ์ƒ ์œ„ํ—˜์ด ์ค„์–ด๋“ ๋‹ค.
    • ๊ธฐ์กด ์ฝ”๋“œ ์žฌ์ปดํŒŒ์ผ์ด๋‚˜ ์žฌ๋ฐฐํฌ๊ฐ€ ํ•„์š” ์—†๋‹ค.
  2. ํ™•์žฅ์„ฑ ๊ฐœ์„ 
    • ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
    • ์ƒˆ๋กœ์šด ์ฝ”๋“œ๋งŒ ์ž‘์„ฑํ•˜์—ฌ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  3. ํ…Œ์ŠคํŠธ ์šฉ์ด์„ฑ
    • ๊ธฐ์กด ์ฝ”๋“œ๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ธฐ์กด ํ…Œ์ŠคํŠธ๊ฐ€ ๊นจ์ง€์ง€ ์•Š๋Š”๋‹ค.
    • ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ๋งŒ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.

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. ๋งˆํ‹ด ์ €/์†ก์ค€์ด ์—ญ)

728x90