๐Ÿ“š SOLID

CodeByHanยท2025๋…„ 3์›” 27์ผ

์ž๋ฐ”

๋ชฉ๋ก ๋ณด๊ธฐ
2/13

๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(OOP)์—์„œ ์†Œํ”„ํŠธ์›จ์–ด ์„ค๊ณ„๋ฅผ ๋” ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ณ , ์œ ์—ฐํ•˜๋ฉฐ, ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์šฉ์ดํ•˜๋„๋ก ๋งŒ๋“œ๋Š” ๋‹ค์„ฏ ๊ฐ€์ง€ ์›์น™

1) SRP(Single Responsibility Principle )

  • ๋‹จ์ผ ์ฑ…์ž„ ์›์น™
  • ํ•œ ํด๋ž˜์Šค๋Š” ํ•˜๋‚˜์˜ ์ฑ…์ž„๋งŒ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.

SRP๋ฅผ ๋”ฐ๋ฅด์ง€ ์•Š๋Š” ์˜ˆ์‹œ

public class Product {
    public void addProduct() {
        // ๋ฌผ๊ฑด ์ถ”๊ฐ€ ๋กœ์ง
    }
    
    public void displayProduct() {
        // ๋ฌผ๊ฑด ์ •๋ณด ์ถœ๋ ฅ ๋กœ์ง
    }
}
  • Product ํด๋ž˜์Šค๊ฐ€ ๋ฌผ๊ฑด ์ถ”๊ฐ€์™€ ์ •๋ณด ์ถœ๋ ฅ ๋‘ ๊ฐ€์ง€ ์ฑ…์ž„์„ ๋ชจ๋‘ ๋‹ด๋‹น

SRP๋ฅผ ๋”ฐ๋ฅด๋Š” ์˜ˆ์‹œ

public class Product {
    public void addProduct() {
        // ๋ฌผ๊ฑด ์ถ”๊ฐ€ ๋กœ์ง
    }
}

public class ProductDisplay {
    public void displayProduct(Product product) {
        // ๋ฌผ๊ฑด ์ •๋ณด ์ถœ๋ ฅ ๋กœ์ง
    }
}
  • Product ํด๋ž˜์Šค๋Š” ๋ฌผ๊ฑด ์ถ”๊ฐ€๋งŒ, ProductDisplay ํด๋ž˜์Šค๋Š” ๋ฌผ๊ฑด ์ •๋ณด ์ถœ๋ ฅ๋งŒ ๋‹ด๋‹น

SRP๋ฅผ ์ค€์ˆ˜ํ•˜๋ฉด ํ•œ ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ์ด ๋‹ค๋ฅธ ๊ธฐ๋Šฅ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ์ผ์ด ์ค„์–ด๋“ค๊ณ , ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€ ๋ณด์ˆ˜์„ฑ์ด ํ–ฅ์ƒ๋˜๋ฉฐ, ํ…Œ์ŠคํŠธํ•˜๊ธฐ๋„ ๋” ์‰ฌ์›Œ์ง„๋‹ค.

2) OCP(Open Closed Principle)

ํ™•์žฅ์— ๋Œ€ํ•ด์„œ๋Š” ๊ฐœ๋ฐฉ์ (Open)์ด๊ณ , ์ˆ˜์ •์— ๋Œ€ํ•ด์„œ๋Š” ํ์‡„์ (closed)์ด์–ด์•ผ ํ•œ๋‹ค.

  • ๊ฐœ๋ฐฉ-ํ์‡„ ์›์น™
  • ๊ธฐ์กด์˜ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์œผ๋ฉด์„œ, ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„ํ•ด์•ผํ•œ๋‹ค.

OCP๋ฅผ ๋”ฐ๋ฅด์ง€ ์•Š๋Š” ์˜ˆ์‹œ

class Shape {

    private String type;
  
    public Shape(String type) {
        this.type = type;
    }
  
    public void draw() {
        if (type.equals("circle")) {
            drawCircle();
        } else if (type.equals("rectangle")) {
            drawRectangle();
        } 
    }
  
    private void drawCircle() {
        System.out.println("์›์„ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค.");
    }
  
    private void drawRectangle() {
        System.out.println("์‚ฌ๊ฐํ˜•์„ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค.");
    }
}
  • ๊ธฐ๋Šฅ(type)์„ ์ถ”๊ฐ€๋ฅผ ํ•˜๋Ÿฌ๋ฉด draw ๋ฉ”์„œ๋“œ()๋ฅผ ์ˆ˜์ • -> ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผํ•˜๋ฏ€๋กœ OCP ์œ„๋ฐ˜

OCP๋ฅผ ๋”ฐ๋ฅด๋Š” ์˜ˆ์‹œ

interface Shape {
    void draw();
}

class Circle implements Shape {
    public void draw() {
        System.out.println("์›์„ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค.");
    }
}

class Rectangle implements Shape {
    public void draw() {
        System.out.println("์‚ฌ๊ฐํ˜•์„ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค.");
    }
}

// ์ƒˆ๋กœ์šด ๋„ํ˜•์„ ์ถ”๊ฐ€ํ•ด๋„ ๊ธฐ์กด ์ฝ”๋“œ ์ˆ˜์ • ์—†์ด ํ™•์žฅ ๊ฐ€๋Šฅ!
class Triangle implements Shape {
    public void draw() {
        System.out.println("์‚ผ๊ฐํ˜•์„ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค.");
    }
}
  • ์ƒˆ๋กœ์šด ๋„ํ˜•์„ ์ถ”๊ฐ€ํ•˜๋”๋ผ๋„ ๊ธฐ์กด Shape ํด๋ž˜์Šค๋ฅผ ์ˆ˜์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Œ
  • Shape ํด๋ž˜์Šค๋Š” ํ™•์žฅ(Extension)์—๋Š” ์—ด๋ ค ์žˆ๊ณ , ๋ณ€๊ฒฝ(Modification)์—๋Š” ๋‹ซํ˜€ ์žˆ์Œ

3) LSP(Liskov Substitution Principle)

๋ถ€๋ชจ ๊ฐ์ฒด์™€ ์ž์‹ ๊ฐ์ฒด๊ฐ€ ์žˆ์„ ๋•Œ ๋ถ€๋ชจ ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋™์ž‘์—์„œ ์ž์‹ ๊ฐ์ฒด๊ฐ€ ๋ถ€๋ชจ ๊ฐ์ฒด๋ฅผ ์™„์ „ํžˆ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์›์น™

  • ๋ฆฌ์Šค์ฝ”ํ”„ ์น˜ํ™˜ ์›์น™

LSP๋ฅผ ์œ„๋ฐ˜ํ•œ ์˜ˆ์‹œ

// ๋ถ€๋ชจ ํด๋ž˜์Šค: Rectangle(์ง์‚ฌ๊ฐํ˜•)
class Rectangle {
    protected int width;
    protected int height;
  
    public void setWidth(int width) {
        this.width = width;
    }
  
    public void setHeight(int height) {
        this.height = height;
    }
  
    public int getWidth() {
        return width;
    }
  
    public int getHeight() {
        return height;
    }
  
    public int getArea() {
        return width * height;
    }
}

// ์ž์‹ ํด๋ž˜์Šค: Square (์ •์‚ฌ๊ฐํ˜•)
// Rectangle์„ ์ƒ์†ํ•˜๋ฉด์„œ, ์ •์‚ฌ๊ฐํ˜•์˜ ํŠน์„ฑ(๋„ˆ๋น„์™€ ๋†’์ด๊ฐ€ ํ•ญ์ƒ ๋™์ผ)์„ ๊ฐ•์ œํ•˜๊ธฐ ์œ„ํ•ด ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•จ
class Square extends Rectangle {
    @Override
    public void setWidth(int width) {
        super.setWidth(width);
        super.setHeight(width); // ๋„ˆ๋น„๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๋†’์ด๋„ ๋ณ€๊ฒฝ
    }
  
    @Override
    public void setHeight(int height) {
        super.setWidth(height); // ๋†’์ด๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๋„ˆ๋น„๋„ ๋ณ€๊ฒฝ
        super.setHeight(height);
    }
}
public class Main {
    public static void main(String[] args) {

        Rectangle rectangle = new Rectangle();
        rectangle.setHeight(5);
        rectangle.setWidth(10);

        System.out.println(rectangle.getArea());
        
        // --------------------------------------
        
        Rectangle square = new Square();
        
        square.setHeight(5);
        square.setWidth(10);



        System.out.println(square.getArea());
        

    }
    
} 

50
25

  • ๋ฆฌ์Šค์ฝ”ํ”„ ์น˜ํ™˜ ์›์น™์€ ๋ถ€๋ชจ ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋™์ž‘์—์„œ ์ž์‹ ๊ฐ์ฒด๊ฐ€ ๋ถ€๋ชจ ๊ฐ์ฒด๋ฅผ ์™„์ „ํžˆ ๋Œ€์ฒด ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์›์น™ -> ๊ฐ™์€ ๊ฐ’์ด ๋‚˜์™€์•ผํ•จ
  • ํด๋ผ์ด์–ธํŠธ๋Š” ๋ถ€๋ชจ ๊ฐ์ฒด์ธ Rectangle์ฒ˜๋Ÿผ ๋™์ž‘ํ•  ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€ํ–ˆ์ง€๋งŒ, Square๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ์ •์‚ฌ๊ฐํ˜•์˜ ์ œ์•ฝ ์กฐ๊ฑด์„ ์ ์šฉํ•ด ์˜๋„์™€ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ
  • ์‰ฝ๊ฒŒ ๋งํ•ด์„œ ์ž์‹ํด๋ž˜์Šค(Square)๊ฐ€ ๋ถ€๋ชจ ํด๋ž˜์Šค(Rectangle)๋กœ ์™„์ „ํžˆ ๋Œ€์ฒด ๋ถˆ๊ฐ€๋Šฅ

๐Ÿ˜ฅ ์ด๋Š” ๋ฐ”๋กœ ์ •์‚ฌ๊ฐํ˜•์ด ์ง์‚ฌ๊ฐํ˜•์„ ์ƒ์† ๋ฐ›๋Š” ๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅธ ์ƒ์† ๊ด€๊ณ„๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์„ ์˜๋ฏธ! ์ž์‹ ๊ฐ์ฒด๊ฐ€ ๋ถ€๋ชจ ๊ฐ์ฒด์˜ ์—ญํ• ์„ ์™„์ „ํžˆ ๋Œ€์ฒดํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ์˜๋ฏธํ•œ๋‹ค.

LSP๋ฅผ ์ ์šฉ ์˜ˆ์‹œ

public class Shape {

    public int width;
    public int height;


    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }


    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }


    public int getArea() {
        return width * height;
    }
}
//์ง์‚ฌ๊ฐํ˜• ํด๋ž˜์Šค
public class Rectangle extends Shape {

    public Rectangle(int width, int height) {
        setWidth(width);
        setHeight(height);
    }

}

//์ •์‚ฌ๊ฐํ˜• ํด๋ž˜์Šค
public class Square extends Shape{
    
    public Square(int length) {
        setWidth(length);
        setHeight(length);
    }
    
}
public class Main {
    public static void main(String[] args) {
    
        Shape rectangle = new Rectangle(10, 5);
        Shape square = new Square(5);
        
        System.out.println(rectangle.getArea());
        System.out.println(square.getArea());
    }
}

50
25

  • ํด๋ผ์ด์–ธํŠธ๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์ธ Shape ํƒ€์ž…์œผ๋กœ ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃจ๋ฏ€๋กœ, ๊ตฌ์ฒด์ ์ธ ๋„ํ˜•์ด Rectangle์ธ์ง€ Square์ธ์ง€ ๋ชฐ๋ผ๋„ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉ
  • ์ž์‹ํด๋ž˜์Šค(Rectangle, Square)๊ฐ€ ๋ถ€๋ชจ ํด๋ž˜์Šค(Shape)๋กœ ์™„์ „ํžˆ ๋Œ€์ฒด๋  ์ˆ˜ ์žˆ์Œ์„ ๋ณด์—ฌ์ค€๋‹ค.

์ฒซ๋ฒˆ์งธ์˜ ์ง์‚ฌ๊ฐํ˜• ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ์ •์‚ฌ๊ฐํ˜• ๊ฐ์ฒด ์˜ˆ์ œ์ฒ˜๋Ÿผ, ์˜ฌ๋ฐ”๋ฅด์ง€ ๋ชปํ•œ ์ƒ์†๊ด€๊ณ„๋Š” ์ œ๊ฑฐํ•˜๊ณ  ๋ถ€๋ชจ ๊ฐ์ฒด์˜ ๋™์ž‘์„ ์™„๋ฒฝํžˆ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋Š” ๊ด€๊ณ„๋งŒ ์ƒ์†ํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์„ค๊ณ„

4) ISP(Interface Segregation Principle)

  • ๊ฐ์ฒด๋Š” ์ž์‹ ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ์—๋งŒ ์˜์กดํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋ฒ•์น™
  • ๊ฐ์ฒด๊ฐ€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์˜์กดํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค๋Š” ๋œป
  • ์ธํ„ฐํŽ˜์ด์Šค ๋ถ„๋ฆฌ ์›์น™

ISP๋ฅผ ์œ„๋ฐ˜ํ•œ ์˜ˆ์‹œ

interface Employee {
    void work();
    void eat();
}

class HumanEmployee implements Employee {
    @Override
    public void work() {
        // ์ž‘์—…ํ•˜๋Š” ์ฝ”๋“œ
    }

    @Override
    public void eat() {
        // ์‹์‚ฌํ•˜๋Š” ์ฝ”๋“œ
    }
}

class AIEmployee implements Employee {
    @Override
    public void work() {
        // ์ž‘์—…ํ•˜๋Š” ์ฝ”๋“œ
    }

    @Override
    public void eat() {
        // AI์€ ๋จน์ง€ ์•Š์ง€๋งŒ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ
        throw new UnsupportedOperationException("๋กœ๋ด‡์€ ๋จน์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
    }
}
  • AIEmployee ํด๋ž˜์Šค๋Š” eat ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ํ•„์š”๊ฐ€ ์—†์ง€๋งŒ, ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ -> ISP ์œ„๋ฐ˜

ISP ์ ์šฉ ์˜ˆ์‹œ


// ์ž‘์—…ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ •์˜ํ•œ ์ธํ„ฐํŽ˜์ด์Šค
interface Workable {
    void work();
}

// ์‹์‚ฌํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ •์˜ํ•œ ์ธํ„ฐํŽ˜์ด์Šค
interface Eatable {
    void eat();
}

class HumanEmployee implements Workable, Eatable {
    @Override
    public void work() {
        // ์ž‘์—…ํ•˜๋Š” ์ฝ”๋“œ
    }

    @Override
    public void eat() {
        // ์‹์‚ฌํ•˜๋Š” ์ฝ”๋“œ
    }
}

class AIEmployee implements Workable {
    @Override
    public void work() {
        // ์ž‘์—…ํ•˜๋Š” ์ฝ”๋“œ
    }
}
  • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด AIEmployee ํด๋ž˜์Šค๊ฐ€ ๋ถˆํ•„์š”ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฉฐ, ISP๋ฅผ ์ค€์ˆ˜ํ•˜๊ฒŒ ๋œ๋‹ค.

5) DIP(Dependency Inversion Principle)

  • ๊ณ ์ˆ˜์ค€ ๋ชจ๋“ˆ์€ ์ €์ˆ˜์ค€ ๋ชจ๋“ˆ์˜ ๊ตฌํ˜„์— ์˜์กดํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค.
  • ๊ตฌ์ฒดํ™”์— ์˜์กดํ•˜์ง€ ๋ง๊ณ , ์ถ”์ƒํ™”์— ์˜์กดํ•ด๋ผ.
    -> ์ž์‹ ๋ณด๋‹ค ๋ณ€ํ•˜๊ธฐ ์‰ฌ์šด ๊ฒƒ์— ์˜์กดํ•˜์ง€ ๋งˆ๋ผ
  • ์˜์กด์„ฑ ์—ญ์ „ ์›์น™

DIP ์œ„๋ฐ˜ ์˜ˆ์‹œ

public class Driver {
    private Lamborghini car;

    public void setLamborghini(Lamborghini car) {
        this.lamborghini = car;
    }

    public void drive() {
        System.out.println(car.toString());
    }
}

public class Main {
    public static void main(String[] args) {
        Lamborghini myLamborghini = new Lamborghini();
        Driver driver = new Driver();
        driver.setLamborghini(myLamborghini);
        driver.drive();
    }
}
  • ์œ„ ์ƒํ™ฉ์—์„œ Driver๋Š” ๋žŒ๋ณด๋ฅด๊ธฐ๋‹ˆ๋ผ๋Š” ๊ตฌ์ฒด์ ์ธ ๊ฐ์ฒด์— ์˜์กดํ•˜๊ณ  ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์šด์ „์ž๊ฐ€ ์šด์ „ํ•˜๋Š” ์ž๋™์ฐจ์˜ ์ข…๋ฅ˜๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด Driver ํด๋ž˜์Šค๋ฅผ ์ˆ˜์ •ํ•ด์•ผํ•œ๋‹ค.
public class Driver {
    private Lamborghini car;
    private Ferrari car
    
    // ์šด์ „์ž๊ฐ€ ์šด์ „ํ•˜๋Š” ์ž๋™์ฐจ ๋ธŒ๋žœ๋“œ ์ข…๋ฅ˜๋งŒํผ Driver ํด๋ž˜์Šค ๋‚ด์— ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•ด์•ผํ•จ.
    public void setLamborghini(Lamborghini car) {
        this.lamborghini = car;
    }
    
     public void setLamborghini(Ferrari car) {
        this.Ferrari = car;
    }

    public void drive() {
        System.out.println(car.toString());
    }
}
  • ์ด๋ ‡๊ฒŒ ์šด์ „์ž๋ผ๋Š” ๊ณ ์ˆ˜์ค€ ๋ชจ๋“ˆ์ด, ๋ณ€ํ•˜๊ธฐ ์‰ฌ์šด ์ž๋™์ฐจ๋ผ๋Š” ์ €์ˆ˜์ค€ ๋ชจ๋“ˆ์—๊ฒŒ ์˜์กดํ•˜๋ฉด ์˜ํ–ฅ์— ์ง์ ‘์ ์œผ๋กœ ๋…ธ์ถœ, ์ž๋™์ฐจ๋ฅผ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด Driver ํด๋ž˜์Šค์— ์ˆ˜์ •๊นŒ์ง€ ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ

DIP ์ ์šฉ ์˜ˆ์‹œ

// Car ์ธํ„ฐํŽ˜์ด์Šค: ์ถ”์ƒํ™” ๊ณ„์ธต์„ ์ •์˜
public interface Car {
    String toString();
}

// Driver ํด๋ž˜์Šค๋Š” ๊ตฌ์ฒด์ ์ธ ์ž๋™์ฐจ ๋Œ€์‹  ์ถ”์ƒํ™”๋œ Car์— ์˜์กดํ•จ
public class Driver {
    private Car car;

    public void setCar(Car car) {
        this.car = car;
    }

    public void drive() {
        System.out.println(car.toString());
    }
    
}

// Lamborghini ํด๋ž˜์Šค๋Š” Car ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„
public class Lamborghini implements Car {
    @Override
    public String toString() {
        return "Lamborghini car";
    }
}

// Ferrari ํด๋ž˜์Šค๋Š” Car ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„
public class Ferrari implements Car {
    @Override
    public String toString() {
        return "Ferrari car";
    }
}


public class Main {
    public static void main(String[] args) {
        // Ferrari ์ž๋™์ฐจ๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ
        Car myFerrari = new Ferrari();
        Driver driver = new Driver();
        driver.setCar(myFerrari);
        driver.drive();
        
        // Lamborghini ์ž๋™์ฐจ๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ
        Car myLamborghini = new Lamborghini();
        driver.setCar(myLamborghini);
        driver.drive();
    }
}
  • ์ƒˆ๋กœ์šด ์ž๋™์ฐจ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์ง€๋งŒ Driverํด๋ž˜์Šค๋Š” ์ฝ”๋“œ์— ์–ด๋– ํ•œ ๋ณ€ํ™”๋„ ์ผ์–ด๋‚˜์ง€ ์•Š๊ณ , ์–ด๋– ํ•œ ์˜ํ–ฅ๋„ ๋ฏธ์น˜์ง€ ์•Š๋Š”๋‹ค.

OCP์™€ DIP์˜ ์ฐจ์ด์ 

ํŠน์ง•OCP (Open-Closed Principle)DIP (Dependency Inversion Principle)
๋ชฉ์ ๊ธฐ๋Šฅ ํ™•์žฅ์€ ํ—ˆ์šฉํ•˜๋˜ ๊ธฐ์กด ์ฝ”๋“œ๋Š” ์ˆ˜์ •ํ•˜์ง€ ์•Š์Œ์ƒ์œ„ ๋ชจ๋“ˆ์ด ํ•˜์œ„ ๋ชจ๋“ˆ์— ์ง์ ‘ ์˜์กดํ•˜์ง€ ์•Š๊ณ  ์ถ”์ƒํ™”์— ์˜์กด
์ดˆ์ ์ฝ”๋“œ ๋ณ€๊ฒฝ ์—†์ด ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ๊ตฌ์กฐ ์„ค๊ณ„๋ชจ๋“ˆ ๊ฐ„ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๊ณ  ์œ ์—ฐ์„ฑ๊ณผ ํ…Œ์ŠคํŠธ ์šฉ์ด์„ฑ์„ ํ™•๋ณด
๋ฐฉ๋ฒ•๋‹คํ˜•์„ฑ๊ณผ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ™•์žฅ ๊ฐ€๋Šฅํ•˜๋„๋ก ์„ค๊ณ„์ธํ„ฐํŽ˜์ด์Šค๋‚˜ ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์˜์กด์„ฑ ์—ญ์ „ ๊ตฌํ˜„
์ ์šฉ ๋ฒ”์œ„๊ฐœ๋ณ„ ํด๋ž˜์Šค/๋ชจ๋“ˆ ์ˆ˜์ค€์—ฌ๋Ÿฌ ๋ชจ๋“ˆ ๊ฐ„ ๊ด€๊ณ„ ๋ฐ ์˜์กด์„ฑ ๊ด€๋ฆฌ
์˜ˆ์‹œ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ์‹œ ๊ธฐ์กด ํด๋ž˜์Šค ๋Œ€์‹  ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ์ƒ์œ„/ํ•˜์œ„ ๋ชจ๋“ˆ ๊ฐ„ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถค

์ฐธ๊ณ  :

profile
๋…ธ๋ ฅ์€ ๋ฐฐ์‹ ํ•˜์ง€ ์•Š์•„ ๐Ÿ”ฅ

0๊ฐœ์˜ ๋Œ“๊ธ€