Language/JAVA

[JAVA] OCP (Open Closed Principle) ๊ฐœ๋ฐฉ ํ์‡„์˜ ์›์น™

๊ต ๋ฏผ 2024. 8. 26. 12:36

๐Ÿ“Œ ๊ฐœ๋ฐฉ ํ์‡„์˜ ์›์น™ (OCP) ?

: ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„ ์›์น™ ์ค‘ ํ•˜๋‚˜๋กœ Open for extention + Closed for modification ์ฆ‰, ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์˜ ์ถ”๊ฐ€๋‚˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์ƒ๊ฒผ์„ ๋•Œ ๊ธฐ์กด ์ฝ”๋“œ๋Š” ํ™•์žฅํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜์ง€๋งŒ ๊ธฐ์กด์˜ ์ฝ”๋“œ๋Š” ์ˆ˜์ •๋˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š” ์›์น™์ด๋‹ค. ํ™•์žฅ์—๋Š” ์—ด๋ ค์žˆ๊ณ  ๋ณ€๊ฒฝ์—๋Š” ๋‹ซํ˜€์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. 

  • Open for extention : ํ™•์žฅ์€ ์—ด๋ ค์žˆ๋‹ค๋Š” ๋œป์œผ๋กœ, ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ ์ˆ˜์ •์„ ์ œ์™ธํ•œ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ์„ ์˜๋ฏธ.
  • Closed for modification : ์ฝ”๋“œ ์ˆ˜์ •์€ ๋‹ซํ˜€์žˆ๋‹ค๋Š” ์˜๋ฏธ๋กœ, ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ ์ˆ˜์ • ๊ธˆ์ง€๋ฅผ ์˜๋ฏธ. 

 

์žฅ์ 

  • ์ฝ”๋“œ์˜ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ๋†’์ด๊ณ  ๋ณ€๊ฒฝ์œผ๋กœ ์ธํ•œ ์˜ค๋ฅ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
  • ๋‹คํ˜•์„ฑ์„ ํ™œ์šฉํ•˜๊ณ  ์—ญํ• ๊ณผ ๊ตฌํ˜„์„ ์ž˜ ๋ถ„๋ฆฌํ•˜์˜€๊ธฐ์— ํ•ต์‹ฌ ์ฝ”๋“œ๋“ค์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ…Œ์ŠคํŠธ ์šฉ์ด์„ฑ : ๊ฐ ํด๋ž˜์Šค๊ฐ€ ๋…๋ฆฝ์ ์œผ๋กœ ํ…Œ์ŠคํŠธ๋  ์ˆ˜ ์žˆ๋‹ค.
  • ํ˜‘์—… ์šฉ์ด์„ฑ : ์„œ๋กœ์˜ ์ฝ”๋“œ๋ฅผ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ณ  ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ธฐ์—, ์ถฉ๋Œ์„ ์ค„์ด๊ณ  ๊ฐœ๋ฐœ ์†๋„๊ฐ€ ๋†’์•„์ง„๋‹ค.

 

๐Ÿ“„  ocp๋ฅผ ์œ„๋ฐ˜ํ•œ ๊ฒฝ์šฐ ์ฝ”๋“œ ์˜ˆ์‹œ

class Shape {
    public String type;
    
    public Shape(String type) {
        this.type = type;
    }
}

class AreaCalculator {
    public double calculateArea(Shape shape) {
        if (shape.type.equals("circle")) {
            return Math.PI * 5 * 5; // ๋ฐ˜์ง€๋ฆ„์ด 5์ธ ์›์˜ ๋„“์ด
        } else if (shape.type.equals("rectangle")) {
            return 10 * 5; // ๊ฐ€๋กœ 10, ์„ธ๋กœ 5์ธ ์‚ฌ๊ฐํ˜•์˜ ๋„“์ด
        }
        return 0;
    }
}

public class Main {
    public static void main(String[] args) {
        Shape circle = new Shape("circle");
        Shape rectangle = new Shape("rectangle");

        AreaCalculator calculator = new AreaCalculator();
        System.out.println("Circle Area: " + calculator.calculateArea(circle));
        System.out.println("Rectangle Area: " + calculator.calculateArea(rectangle));
    }
}

 

๋ฌธ์ œ์ 

:  AreaCalculator ํด๋ž˜์Šค๊ฐ€ ์ƒˆ๋กœ์šด ๋„ํ˜•์ด ์ถ”๊ฐ€๋  ๋•Œ๋งˆ๋‹ค ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค.

 → ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋กœ, OCP ์›์น™ ์œ„๋ฐ˜

 

 

๐Ÿ“„  ocp๋ฅผ ์ค€์ˆ˜ํ•œ ๊ฒฝ์šฐ ์ฝ”๋“œ ์˜ˆ์‹œ

// ์ถ”์ƒ ํด๋ž˜์Šค ๋˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋กœ ํ˜•ํƒœ ์ •์˜
abstract class Shape {
    abstract double calculateArea();
}

class Circle extends Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    double calculateArea() {
        return Math.PI * radius * radius;
    }
}

class Rectangle extends Shape {
    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    double calculateArea() {
        return width * height;
    }
}

public class Main {
    public static void main(String[] args) {
        Shape circle = new Circle(5);
        Shape rectangle = new Rectangle(10, 5);

        System.out.println("Circle Area: " + circle.calculateArea());
        System.out.println("Rectangle Area: " + rectangle.calculateArea());
    }
}

 

: ์ƒˆ๋กœ์šด ๋„ํ˜•์ด ์ƒ๊ฒผ์„ ๊ฒฝ์šฐ AreaCalculator ํด๋ž˜์Šค๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  Shape ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ์ƒˆ๋กœ์šด ๋„ํ˜• ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  calculateArea๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๊ตฌ์กฐ.

→ ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ธฐ์— OCP ์ค€์ˆ˜

 

โ–ถ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋˜๋ฉด ๊ธฐ์กด ์ฝ”๋“œ์˜ ์ˆ˜์ •์€ ๋ถˆ๊ฐ€ํ”ผํ•˜๋‹ค. ์ด๋•Œ, ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์ฝ”๋“œ ( ์œ„ ์ฝ”๋“œ์—์„œ๋Š” AreaCalculator)๋ฅผ ์ˆ˜์ •ํ–ˆ๋Š”์ง€๊ฐ€ OCP๋ฅผ ์ง€์ผฐ๋Š”์ง€ ์œ„๋ฐ˜ํ–ˆ๋Š”์ง€์˜ ์ง€ํ‘œ! โ—€