Skip to content

Latest commit

Β 

History

History
58 lines (46 loc) Β· 1.58 KB

lsp.md

File metadata and controls

58 lines (46 loc) Β· 1.58 KB

LSP

Liskov Substitution Principle (λ¦¬μŠ€μ½”ν”„ μΉ˜ν™˜ 원칙) ν”„λ‘œκ·Έλž¨μ—μ„œ μžλ£Œν˜• Sκ°€ μžλ£Œν˜• T의 ν•˜μœ„ν˜•μ΄λΌλ©΄, ν•„μš”ν•œ ν”„λ‘œκ·Έλž¨μ˜ 속성 λ³€κ²½ 없이 Tνƒ€μž…μ˜ 객체λ₯Ό Sνƒ€μž…μ˜ 객체둜 μΉ˜ν™˜ν•  수 μžˆμ–΄μ•Ό ν•œλ‹€λŠ” 원칙이닀.

LSPλ₯Ό μœ„λ°˜ν•˜λŠ” μ „ν˜•μ μΈ 예둜, λ„ˆλΉ„μ™€ λ†’μ΄μ˜ getter, setter λ©”μ„œλ“œλ₯Ό 가진 μ§μ‚¬κ°ν˜• ν΄λž˜μŠ€λ‘œλΆ€ν„° μ •μ‚¬κ°ν˜• 클래슀λ₯Ό νŒŒμƒν•˜λŠ” 경우λ₯Ό λ“€ 수 μžˆλ‹€.

// Rectangle.ts
class Rectangle {
  private width: number;
  private height: number;

  constructor(width: number, height: number) {
    this.width = width;
    this.height = height;
  }

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

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

  public area(): number {
    return this.width * this.height;
  }
}
// Square.ts
class Square extends Rectangle {
  constructor(width: number, height: number) {
    super(width, height);
  }

  public setWidth(width: number): void {
    this.width = width;
    this.height = width;
  }

  public setHeight(height: number): void {
    this.width = height;
    this.height = height;
  }

  public area(): number {
    return this.width * this.height;
  }
}

LSP μœ„λ°˜μ€ LSPλ₯Ό μœ„λ°˜ν•œ 클래슀λ₯Ό μ‚¬μš©ν•˜λŠ” μ½”λ“œκ°€ μ‹€μ œλ‘œ κΈ°λŒ€ν•˜λŠ” 쑰건에 따라 λ¬Έμ œκ°€ 될 μˆ˜λ„ 있고 μ•„λ‹μˆ˜λ„ μžˆλ‹€.
μ—¬κΈ°μ„œ μ€‘μš”ν•œ μ‚¬μ•ˆμ€ 가변성이닀.
μ •μ‚¬κ°ν˜•κ³Ό μ§μ‚¬κ°ν˜•μ΄ 쑰회 λ©”μ„œλ“œλ§Œ 가진닀면, LSP μœ„λ°˜μ„ λ°œμƒν•˜μ§€ μ•ŠλŠ”λ‹€.