jtwjs Dev Wiki
  • DEV_ROAD
    • πŸ’ͺ🏻 μƒμ‘΄ν•˜κΈ°
    • Week 1
      • 개발 ν™˜κ²½ μ„ΈνŒ…
      • νƒ€μž…μŠ€ν¬λ¦½νŠΈ
      • λ¦¬μ—‘νŠΈ
      • Testing Library
      • Parcel & ESLint
    • Week 2
      • JSX
      • Virtual DOM
    • Week 3
      • React Component
      • React State
    • Week 4
      • Express
      • Fetch API & CORS
      • React Hook
      • useRef & Custom Hook
    • Week 5
      • TDD
      • React Testing Library
      • MSW
      • Playwrite
      • Snapshot
    • Week 6
      • Separtion of Concerns
      • Principle
      • DI, (Dependency Injection)
      • Reflect-metadata
      • TSyringe
      • External Store
      • Follow Redux
      • usestore-ts
      • useSyncExternalStore
    • Week 7
      • Routing
      • Routes
      • Router
      • Navigation
    • Week 8
      • Design System
      • Style Basics
      • CSS-in-JS
      • Styled-Components
      • Global Style & Theme
    • Week 9
      • κ°œλ°œν•˜κΈ° μ „ μ€€λΉ„
      • μƒν’ˆ λͺ©λ‘ νŽ˜μ΄μ§€
      • μƒν’ˆ 상세 νŽ˜μ΄μ§€
      • μž₯λ°”κ΅¬λ‹ˆ νŽ˜μ΄μ§€
    • Week 10
      • 둜그인
      • λ‘œκ·Έμ•„μ›ƒ
      • νšŒμ›κ°€μž…
      • μ£Όλ¬Έ λͺ©λ‘ & μ£Όλ¬Έ 상세
    • Week 11
      • 배솑 정보 μž…λ ₯
      • ν¬νŠΈμ› 결제 μš”μ²­
      • 배솑 및 결제 정보 전달
    • Week 12
      • κ΄€λ¦¬μž μ›Ήμ‚¬μ΄νŠΈκ°œλ°œμ‹œμž‘
  • DEV_NOTE
    • TypeScript
      • 기본적 문법
        • Enum
        • λ‹€ν˜•μ„±
          • Untitled
        • ꡬ쑰적 타이핑
        • μ œλ„ˆλ¦­ νƒ€μž…
        • 컨디셔널 νƒ€μž…
        • ν•¨μˆ˜ λ©”μ„œλ“œ 타이핑
        • infer둜 νƒ€μž…μŠ€ν¬λ¦½νŠΈμ˜ μΆ”λ‘  직접 ν™œμš©
        • μž¬κ·€ νƒ€μž…
        • ν…œν”Œλ¦Ώ λ¦¬ν„°λŸ΄ νƒ€μž…
        • 좔가적인 νƒ€μž… 검사 satisfies μ—°μ‚°μž
        • νƒ€μž…μŠ€ν¬λ¦½νŠΈ 건망증
        • μ›μ‹œ μžλ£Œν˜•μ—λ„ λΈŒλžœλ”© 기법 μ‚¬μš© κ°€λŠ₯
        • νƒ€μž… 쒁히기
        • μœ μš©ν•œ νƒ€μž… λ§Œλ“€κΈ°
        • λ°μ½”λ ˆμ΄ν„° ν•¨μˆ˜
        • μ•°λΉ„μ–ΈνŠΈ 선언도 μ„ μ–Έ 병합이 λœλ‹€.
        • μ•°λΉ„μ–ΈνŠΈ 선언도 선언병합이 λœλ‹€.
    • Testing
      • Unit Testing
      • λ‹¨μœ„ ν…ŒμŠ€νŠΈμ˜ 두 λΆ„νŒŒ
      • 쒋은 λ‹¨μœ„ ν…ŒμŠ€νŠΈλ₯Ό κ΅¬μ„±ν•˜λŠ” 4λŒ€ μš”μ†Œ
      • ν…ŒμŠ€νŠΈ λŒ€μ—­κ³Ό 식별할 수 μžˆλŠ” λ™μž‘
      • λ‹¨μœ„ ν…ŒμŠ€νŠΈ μŠ€νƒ€μΌ
      • κ°€μΉ˜ μžˆλŠ” λ‹¨μœ„ ν…ŒμŠ€νŠΈλ₯Ό μœ„ν•œ λ¦¬νŒ©ν† λ§
      • 톡합 ν…ŒμŠ€νŠΈ
      • Cross Browsing Testing
      • κΈ°λŠ₯ ν…ŒμŠ€νŠΈ μ’…λ₯˜
      • React Testing Pattern
      • ν”„λ‘ νŠΈμ—”λ“œ ν…ŒμŠ€νŠΈ μž…λ¬Έ
        • ν…ŒμŠ€νŠΈ λ²”μœ„
        • λ‹¨μœ„ ν…ŒμŠ€νŠΈ 검증
        • Mock
        • UI μ»΄ν¬λ„ŒνŠΈ ν…ŒμŠ€νŠΈ
        • ν…ŒμŠ€νŠΈ 컀버리지
        • μ›Ή 톡합 ν…ŒμŠ€νŠΈ
        • MSW
        • μŠ€ν† λ¦¬λΆ
        • μ‹œκ°μ  νšŒκ·€ ν…ŒμŠ€νŠΈ
        • E2E ν…ŒμŠ€νŠΈ
        • Github Actions μ„€μ •
        • κΉƒν—ˆλΈŒ μ•‘μ…˜μ—μ„œ E2E
      • μ‹œν”„νŠΈ λ ˆν”„νŠΈ
        • ν…ŒμŠ€νŠΈ κΈ°λ³Έμ€‘μ˜ κΈ°λ³Έ
        • λ‹¨μœ„ ν…ŒμŠ€νŠΈ
        • μ½”λ“œ λ³΅μž‘λ„
        • λ¦¬νŒ©ν„°λ§
        • μ½”λ“œ 리뷰
        • 톡합 ν…ŒμŠ€νŠΈ νŒ¨ν„΄
        • μ‹œμŠ€ν…œ ν…ŒμŠ€νŠΈμ˜ μžλ™ν™”
        • 탐색적 ν…ŒμŠ€νŠΈ
      • Test Tip
      • vitest
      • playwright
      • Test Data Generator
      • MSW
    • Algorithm
      • coding test
      • Data Structure
    • Next.js
      • Data Fetching
      • Hydration
      • Next 13
      • Optimization
      • Next 15
    • Tailwind
      • Tailwind CSS
      • Theme
      • Directives
      • Tool
      • Design System
    • Storybook
      • Storybook
      • CSF3
      • CDD
      • Headless Component
    • Funtional Programming
      • ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°
      • μ°Έμ‘° 투λͺ…μ„±
      • λΆ€μˆ˜νš¨κ³Ό
      • ν•¨μˆ˜ ν•©μ„±
      • μ œλ„ˆλ¦­ νƒ€μž… ν™œμš©ν•˜κΈ°
      • 암묡적 μž…μΆœλ ₯
      • μ•‘μ…˜κ³Ό 계산, 데이터
      • κ³„μΈ΅ν˜• 섀계
      • 호좜 κ·Έλž˜ν”„
      • ν•¨μˆ˜ν˜• 섀계
      • λΆˆλ³€μ„±
      • 일급 ν•¨μˆ˜
      • ν•¨μˆ˜ν˜• 도ꡬ
    • Git
      • Github Actions
      • Conflict
      • Branch μ „λž΅
    • Contents Format
      • Audio
    • 3D Graphic
      • 3D keyword
      • Three.js
      • Geometry
      • Material
      • Light
      • Camera
      • Decal
      • Rotation
      • Text
      • Shadow
      • Fog
      • Post Processing
      • Animation
      • Math
        • Vector Space
        • λ²‘ν„°μ˜ μ—°μ‚°
        • νšŒμ „ 계산
      • 3D 컨텐츠가 λ§Œλ“€μ–΄μ§€λŠ” κ³Όμ •
      • R3F
      • Env
      • Scene
      • Transform
      • R3F
      • Interaction & Raycast
      • Rendering Algorithnm
      • Blender
      • Blender
    • Accessibility
      • μ ‘κ·Όμ„±μ΄λž€
    • Interactive Web
      • Parallax
      • Canvas
      • requestAnimationFrame
      • Effect
      • HSL
      • React.js + Canvas
      • Matter.js
    • AWS
      • DevOps
      • Amplify
      • S3
      • ν΄λΌμš°λ“œ μ»΄ν“¨νŒ…
        • μ˜¨ν”„λ ˆλ―ΈμŠ€μ™€ ν΄λΌμš°λ“œ
        • ν΄λΌμš°λ“œ λ„μž…νš¨κ³Ό
        • ν΄λΌμš°λ“œ μ»΄ν“¨νŒ…μ˜ λ²”μœ„
        • μ»΄ν“¨νŒ… μ˜΅μ…˜
          • EC2 - Virtual Machin
          • ECS, EKS - Container
          • Lambda - Serverless
        • λ„€νŠΈμ›Œν¬ 가상화
        • μŠ€ν† λ¦¬μ§€
        • λ°μ΄ν„°λ² μ΄μŠ€
        • 데이터 μˆ˜μ§‘
        • λ¨Έμ‹  λŸ¬λ‹ μ˜μ—­
        • IoT μ˜μ—­
        • 블둝체인 μ˜μ—­
      • ν΄λΌμš°λ“œ μ•„ν‚€ν…μ²˜ 섀계
    • Network
      • Web Server & WAS
    • System Design
      • System Design
      • Component
      • μ˜μ‘΄μ„±μ„ λ°°μ œν•œ 개발
      • Error Handling
      • Architecture
        • λͺ¨λ…Έλ‘œν‹± μ•„ν‚€ν…μ²˜
        • Clean Architecture
        • Layered Architecture
        • 이벀트 기반 μ•„ν‚€ν…μ²˜
      • 상황을 νŒŒμ•…ν•˜λŠ” 메타인지
      • 쀑볡 문제 ν•΄κ²°ν•˜κΈ°
      • Monorepo Arhitecture
        • λͺ¨λ…Έλ ˆν¬ 운영과 νŠΈλŸ¬λΈ”μŠˆνŒ…
        • Module Federation
      • μ½”λ“œ 병λͺ©μ§€μ 
      • API λŒ€μ‘
      • 곡톡 μ½”λ“œ
      • Infra ꡬ좕
      • λͺ¨λ“ˆ 기반의 개발 방식
      • Design System
        • μ΅œμ†Œ μˆ˜μ€€μ˜ μ•„ν‚€ν…μ²˜ μ„€μ •
        • 더 효율적인 λ””μžμΈμ‹œμŠ€ν…œ λ§Œλ“€κΈ°
        • λ””μžμΈ μ‹œμŠ€ν…œκ³Ό UI 라이브러리 λͺ©μ 
        • λ””μžμΈ 토큰
      • 효율적인 업무
        • 업무 ν”„λ‘œμ„ΈμŠ€ 병λͺ© νŒŒμ•…
      • Clean Code
      • Design Pattern
        • CQRS Pattern
        • Strangler Fig Pattern
        • λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄
        • μ»€λ§¨λ“œ νŒ¨ν„΄
        • μ „λž΅ νŒ¨ν„΄
        • μ˜΅μ €λ²„ νŒ¨ν„΄
      • A/B ν…ŒμŠ€νŒ…
      • λŒ€κ·œλͺ¨ λ¦¬μ—‘νŠΈ μ›Ήμ•± 개발
        • λ³΅μž‘μ„± 관리
        • λͺ¨λ“ˆμ„±
        • μ„±λŠ₯
        • λ””μžμΈ μ‹œμŠ€ν…œ
        • 데이터 패칭
        • μƒνƒœ 관리
        • κ΅­μ œν™”
        • μ½”λ“œ μ‘°μ§ν™”ν•˜κΈ°
        • κ°œμΈν™” A/B ν…ŒμŠ€νŒ…
        • ν™•μž₯ κ°€λŠ₯ν•œ μ›Ή μ•„ν‚€ν…μ²˜
        • ν…ŒμŠ€νŒ…
        • 툴링
        • 기술적 λ§ˆμ΄κ·Έλ ˆμ΄μ…˜
        • νƒ€μž…μŠ€ν¬λ¦½νŠΈ
        • λΌμš°νŒ…
        • μ‚¬μš©μž 쀑심 API λ””μžμΈ
        • λ¦¬μ•‘νŠΈ 미래
    • Performance
      • React DevTools
      • Component μ΅œμ ν™”
      • Page Load
      • API
    • MFA
      • MSA
      • MFA λ„μž…ν•˜κΈ°
      • Monorepo
        • Monorepo Tool
        • Yarn Berry Workspace
        • Turborepo
      • MFA Composition
      • SPA 톡합
      • Design System
      • Package Manager
        • Yarn
        • pnpm
      • Transpiler & Bundler
        • Babel
        • Rollup
        • esbuild
        • swc
        • Webpack
        • Vite
      • 뢄해와 톡합을 μœ„ν•œ μ—¬λŸ¬ 기술 비ꡐ
    • State Management
      • Zustand
    • React v18
      • Automatic batching
      • Suspense
      • Transition
    • SEO
      • Search Engine Optimization
      • Open Graph Element
      • Metadata
    • FE Develop
      • Scrubbing
      • Clipboard
    • Refactoring
      • λ¦¬νŒ©ν† λ§ 깊게 듀여닀보기
      • κΈ΄ μ½”λ“œ 쑰각내기
      • νƒ€μž… μ½”λ“œ μ²˜λ¦¬ν•˜κΈ°
      • μœ μ‚¬ν•œ μ½”λ“œ μœ΅ν•©ν•˜κΈ°
      • 데이터 보호
      • μ½”λ“œ μΆ”κ°€ 및 제거
    • OAuth 2.0
    • Analytics
      • Mixpanel
    • ETC
      • VSCode
    • React Hook In Action
      • useContext & Provider
      • μ»€μŠ€ν…€ ν›…
      • μ½”λ“œ λΆ„ν• ν•˜κΈ° with Suspense, lazy
      • Suspense와 이미지 μ μž¬ν•˜κΈ°
      • useTransition, uesDeferredValue
      • SuspenseList
Powered by GitBook
On this page
  • GoF의 λ””μžμΈ νŒ¨ν„΄
  • 생성 νŒ¨ν„΄(Creational Patterns)
  • ꡬ쑰 νŒ¨ν„΄(Structural Patterns)
  • 행동 νŒ¨ν„΄(Behavioral Paterrns)
  • 생성 νŒ¨ν„΄
  • νŒ©ν† λ¦¬ νŒ¨ν„΄
  • 싱글톀 νŒ¨ν„΄
  • νŒ©ν† λ¦¬ λ©”μ„œλ“œ νŒ¨ν„΄
  • λΉŒλ” νŒ¨ν„΄(Builder Pattern)
  • ꡬ쑰 νŒ¨ν„΄
  • Facade νŒ¨ν„΄
  • Proxy νŒ¨ν„΄
  • 행동 νŒ¨ν„΄
  • μ „λž΅ νŒ¨ν„΄(Strategy Pattern)
  • μ˜΅μ €λ²„ νŒ¨ν„΄
  1. DEV_NOTE
  2. System Design

Design Pattern

GoF의 λ””μžμΈ νŒ¨ν„΄

  • Gang of Four

    • Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides λΌλŠ” λ„€λͺ…μ˜ μ €μžλ₯Ό λœ»ν•¨

  • μ†Œν”„νŠΈμ›¨μ–΄ λ””μžμΈμ—μ„œ 자주 λ°œμƒν•˜λŠ” λ¬Έμ œλ“€μ— λŒ€ν•œ 해결책을 μ œκ³΅ν•˜λŠ” 23κ°€μ§€ λ””μžμΈ νŒ¨ν„΄μ˜ μ‘°ν•©

객체지ν–₯ 원칙

  • λ°”λ€ŒλŠ” 뢀뢄은 μΊ‘μŠν™”ν•œλ‹€

  • μƒμ†λ³΄λ‹€λŠ” ꡬ성을 ν™œμš©ν•œλ‹€

  • κ΅¬ν˜„λ³΄λ‹€λŠ” μΈν„°νŽ˜μ΄μŠ€μ— λ§žμΆ°μ„œ ν”„λ‘œκ·Έλž˜λ°ν•œλ‹€

  • μƒν˜Έμž‘μš©ν•˜λŠ” 객체 μ‚¬μ΄μ—μ„œλŠ” κ°€λŠ₯ν•˜λ©΄ λŠμŠ¨ν•œ 결합을 μ‚¬μš©ν•΄μ•Ό ν•œλ‹€

  • ν΄λž˜μŠ€λŠ” ν™•μž₯μ—λŠ” μ—΄λ € μžˆμ–΄μ•Ό ν•˜μ§€λ§Œ λ³€κ²½μ—λŠ” λ‹«ν˜€μžˆμ–΄μ•Ό ν•œλ‹€(OCP)

  • μΆ”μƒν™”λœ 것에 μ˜μ‘΄ν•˜κ²Œ λ§Œλ“€κ³  ꡬ상 ν΄λž˜μŠ€μ— μ˜μ‘΄ν•˜μ§€ μ•Šκ²Œ λ§Œλ“ λ‹€.

생성 νŒ¨ν„΄(Creational Patterns)

  • 객체의 생성과 μ΄ˆκΈ°ν™”μ— κ΄€λ ¨λœ νŒ¨ν„΄

  • 객체λ₯Ό μƒμ„±ν•˜λŠ” 방식을 좔상화

  • μ£Όμš” νŒ¨ν„΄: Singleton, Factory Method, Abstract Factory, Builder, Prototype

ꡬ쑰 νŒ¨ν„΄(Structural Patterns)

  • ν΄λž˜μŠ€λ‚˜ 객체λ₯Ό ν•©μ„±ν•˜μ—¬ 더 큰 ꡬ쑰둜 λ§Œλ“œλŠ” νŒ¨ν„΄

  • ν΄λž˜μŠ€μ™€ 객체λ₯Ό μ‘°ν•©ν•˜μ—¬ 더큰 ꡬ쑰둜 λ§Œλ“¦

  • μ£Όμš” νŒ¨ν„΄: Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy

행동 νŒ¨ν„΄(Behavioral Paterrns)

  • 객체 κ°„μ˜ 톡신과 μ±…μž„μ„ λΆ„μ‚°ν•˜λŠ” νŒ¨ν„΄

  • μ•Œκ³ λ¦¬μ¦˜κ³Ό 객체 κ°„μ˜ μ±…μž„μ„ 뢄리

  • μ£Όμš” νŒ¨ν„΄: Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor

생성 νŒ¨ν„΄

νŒ©ν† λ¦¬ νŒ¨ν„΄

  • 객체 생성을 μΊ‘μŠν™”ν•˜λŠ” λ””μžμΈ νŒ¨ν„΄

  • 객체λ₯Ό μƒμ„±ν•˜λŠ” μ½”λ“œλ₯Ό ν΄λΌμ΄μ–ΈνŠΈ μ½”λ“œλ‘œλΆ€ν„° λΆ„λ¦¬ν•˜μ—¬ μœ μ—°μ„±μ„ 제곡

  • 객체λ₯Ό μƒμ„±ν•˜λŠ” 과정을 μ„œλΈŒν΄λž˜μŠ€μ—μ„œ μ²˜λ¦¬ν•˜λ„λ‘ ν•˜λŠ” 것이 νŠΉμ§•

  • 객체λ₯Ό μƒμ„±ν•˜λŠ” 별도 도ꡬλ₯Ό 제곡

// λ„ν˜• μΈν„°νŽ˜μ΄μŠ€
interface Shape {
  void draw()
}

// ꡬ체적인 λ„ν˜• 클래슀 - 원
class Circle implements Shpae {
  @Override
   public void draw() {
  }
}

// ꡬ체적인 λ„ν˜• 클래슀 - μ •μ‚¬κ°ν˜•
class Square implements Shpae {
  @Override
   public void draw() {
  }
}

// νŒ©ν† λ¦¬ μΈν„°νŽ˜μ΄μŠ€
interface ShapeFactory {
  Shape createShape();
}

// 원을 μƒμ„±ν•˜λŠ” νŒ©ν† λ¦¬
class CircleFactory implements ShapeFactory {
  @Override
  public Shape createShape() {
    return new Circle();
  }
}

// μ •μ‚¬κ°ν˜•μ„ μƒμ„±ν•˜λŠ” νŒ©ν† λ¦¬
class SquareFactory implements ShapeFactory {
  @Override
  public Shape createShape() {
    return new Square();
  }
}

// ν΄λΌμ΄μ–ΈνŠΈ μ½”λ“œ
public class FactoryPatternExample {
  public state void main(String[] args) {
    // 원을 μƒμ„±ν•˜λŠ” νŒ©ν† λ¦¬λ₯Ό μ΄μš©ν•΄ λ„ν˜• 생성
    ShapeFactory circleFactory = new CircleFactory();
    Shape circle = circleFactory.createShape();
    circle.draw();
    
    // μ •μ‚¬κ°ν˜•μ„ μƒμ„±ν•˜λŠ” νŒ©ν† λ¦¬λ₯Ό μ΄μš©ν•΄ λ„ν˜• 생성
    ShapeFactory squareFactory = new SquareFactory();
    Shape square = squareFactory.createShape();
    square.draw();
  }
}

싱글톀 νŒ¨ν„΄

  • ν•˜λ‚˜μ˜ μΈμŠ€ν„΄μŠ€λ§Œμ„ 갖도둝 보μž₯

public class Singleton {
  private static Singleton intstance;
  // μ™ΈλΆ€μ—μ„œ 생성할 수 없도둝 μƒμ„±μž private
  private Singleton() {}
  
  // 였직 μƒμ„±μžλŠ” getInstance()λ₯Ό 톡해 κ°€μ Έκ°ˆ 수 μžˆλ„λ‘ λ©”μ„œλ“œ 제곡
  public static Singleton getInstance() {
    if (instace == null) {
      instance = new Singleton();
    }
    return instacne;
  }
}

νŒ©ν† λ¦¬ λ©”μ„œλ“œ νŒ¨ν„΄

  • 객체 생성을 μΊ‘μŠν™”

  • 객체 생성을 μ„œλΈŒ ν΄λž˜μŠ€μ—μ„œ κ²°μ •ν•˜λ„λ‘ ν•˜λŠ” 것

interface Product {
  void create();
}

class ConcreteProduct implements Product {
  @Override
  public void create() {
  }
}
  
abstract class Creator {
  public abstract Product factoryMethod();
}
  
class ConcreteCreator extends Creator {
  @Override 
  public Product factoryMethod() {
    return new ConcreteProduct();
  }
} 

λΉŒλ” νŒ¨ν„΄(Builder Pattern)

  • λ³΅μž‘ν•œ 객체λ₯Ό μƒμ„±ν•˜λŠ”λ° μ‚¬μš©λ˜λŠ” νŒ¨ν„΄

  • 객체 μƒμ„±μ˜ 단계λ₯Ό λΆ„λ¦¬ν•˜μ—¬ λ³΅μž‘μ„±μ„ ν•΄κ²°

  • 객체의 생성 과정을 λ‹€μ–‘ν•œ λ°©μ‹μœΌλ‘œ μ‘°ν•©

class Product {
  private String part1;
  private String part2;
  
  public void setPart1(String part1) {
    this.part1 = part1;
  }
  
  public void setPart2(String part1) {
    this.part2 = part2;
  }
  
  @Override
  public String toString() {
    return "Product [part1=" + part1 + ", part2=" +part2 + "]";
  }
}

// λΉŒλ” μΈν„°νŽ˜μ΄μŠ€
interface Builder {
  void buildPart1(String part1);
  void buildPart2(String part2);
  Product getResult();
}

// ꡬ체적인 λΉŒλ” 클래슀
class ConcreteBuilder implements Builder {
  private Product product = new Product();
  
  @Override
  public void buildPart1(String part1) {
    product.setPart1(part1);
  }
  
  @Override
  public void buildPart2(String part2) {
    product.setPart2(part2);
  }  
  
  @Override
  public Product getResult() {
    return project
  }
}

// μˆœμ„œλ₯Ό λ³€κ²½ν•˜κ±°λ‚˜ μ „λž΅ μƒνƒœλ₯Ό λ³€κ²½ ex

// λ””λ ‰ν„° 클래슀 (생성 μˆœμ„œλ₯Ό μ •μ˜)
class Director {
  public Product construct(Builder builder) {
    builder.buildPart1("Part1");
    builder.buildPart2("Part2");
    return builder.getResult();
  }
}

// ν΄λΌμ΄μ–ΈνŠΈ μ½”λ“œ'
public class BuilderPatternExample {
  public static void main(String[] args) {
    Builder builder = new ConcreteBuilder();
    Director director = new Director();
    
    Product product = director.construct(builder);
  }
}

ꡬ쑰 νŒ¨ν„΄

Facade νŒ¨ν„΄

  • μ—¬λŸ¬ 개의 μ„œλΈŒμ‹œμŠ€ν…œμ΄λ‚˜ λ³΅μž‘ν•œ μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ‹¨μˆœν™”

  • ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ 더 μ‰½κ²Œ μ‚¬μš©ν•  수 μžˆλŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ œκ³΅ν•˜λŠ” νŒ¨ν„΄

  • λ³΅μž‘ν•œ μ‹œμŠ€ν…œμ„ λ‹¨μˆœν™” μΈν„°νŽ˜μ΄μŠ€λ‘œ λž˜ν•‘

  • μ—¬λŸ¬ μ„œλΈŒμ‹œμŠ€ν…œμ„ μ‘°ν•©ν•΄μ„œ λ­”κ°€ λ™μž‘μ„ λ§Œλ“€κΈ°μ—” λ³΅μž‘ν•΄μ§„λ‹€. 이럴 λ•Œ Facade νŒ¨ν„΄μ„ μ‚¬μš©

  • λ³΅μž‘ν•œ μ—¬λŸ¬ μ„œλΈŒμ‹œμŠ€ν…œμ„ μ΄μš©ν•΄μ„œ μ˜€νΌλ ˆμ΄μ…˜μ„ λ§Œλ“€ λ•Œ νŒŒμ‚¬λ“œ νŒ¨ν„΄μ„ μ“΄λ‹€.

// Subsystem classes
class Subsystem1 {
  public void operation1() {}
  
  public void operation2() {}
}

class Subsystem2 {
  public void operation1() {}
  
  public void operation2() {}
}

// Facade class
class Facade {
  private Subsystem1 subsystem1;
  private Subsystem2 subsystem2;
  private Subsystem3 subsystem3;
  
  public Facade() {
    this.subsystem1 = new SubSystem1();
    this.subsystem2 = new SubSystem2();
    this.subsystem3 = new SubSystem3();
  }
  
  // Methods that simplify complex operations
  public void operationA() {
    System.out.println('Facde Operation A");
    subsystem1.operation1();
    subsystem2.operation1();
    subsystem3.operation1();
  }
}

// Exmplae
public class FacadePatternExample {
  public static void main(String[] args) {
    Facade facade = new Facade();
    
    // Use the Facade to simplify operations
    facade.operationA();
    facade.operationB();
  }
}

Proxy νŒ¨ν„΄

  • 객체의 μ ‘κ·Όμ΄λ‚˜ μ‘°μž‘μ„ μ€‘κ°„μ—μ„œ μ œμ–΄ν•˜κ±°λ‚˜ 보쑰할 λ•Œ μ‚¬μš©

  • 객체의 λŒ€λ¦¬μžλ‚˜ λŒ€λ³€μž 역할을 ν•˜λŠ” 객체λ₯Ό μ‚¬μš©

  • μ‹€μ œ 객체에 λŒ€ν•œ 접근을 μ€‘κ°„μ—μ„œ μ œμ–΄ν•˜κ±°λ‚˜ μ€‘κ°„μ—μ„œ 뢀가적인 μž‘μ—…μ„ μˆ˜ν–‰ν•˜λŠ” νŒ¨ν„΄

  • 쀑간에 λŒ€λ¦¬μžκ°€ λ‚€ ν˜•νƒœ

  • μ€‘κ°„μ—μ„œ ν•˜λŠ” 일은 μ—†μ§€λ§Œ μ–΄λ–€ λ™μž‘μ— λŒ€ν•΄μ„œ μœ„μž„ 받은 객체에 전달

  • λ‘œκΉ… 및 속도 μΈ‘μ •, μ ‘κ·Ό μ œμ–΄ λ“± 뢀가적인 별도 λ™μž‘μ„ ν• λ•Œ μ‚¬μš©

interface Image {
  void display();
}

class RealImage implements Image {
  private String filename;
  
  public RealImage(String filename) {
    this.filename = filename;
    loadImageFromDisk();
  }
  
  private void loadImageFromDisk() {}
  
  @Override
  public void display() {}
}

// μ΄λ―Έμ§€μ˜ ν”„λ‘μ‹œ 클래슀
// μ‹€μ§ˆμ μΈ λ™μž‘μ€ κ΅¬ν˜„λ˜μ§€ μ•ŠμŒ
class ProxyImage implements Image {
  private RealImage realImage;
  private String filename;
  
  public ProxyImage(String filename) {
    this.filename = filename;
  }
  
  @Override
  public void display() {
    if (realImage == null) {
      realImage = new RealImage(filename);
    }
    realImage.display();
  }
}

// ν΄λΌμ΄μ–ΈνŠΈ μ½”λ“œ
public class ProxyPatternExample {
  public static void main(String[] args) {
    // ν”„λ‘μ‹œλ₯Ό μ‚¬μš©ν•˜μ—¬ 이미지 λ‘œλ”© 및 ν‘œμ‹œ
    Image image = new ProxyImage("example.jpg");
    
    // 이미지가 처음으둜 ν‘œμ‹œλ  λ•Œ μ‹€μ œ 이미지가 λ‘œλ”©λ˜λŠ” 것을 확인할 수 있음
    image.display();
    
    // 이미지가 이미 λ‘œλ”©λœ κ²½μš°μ—λŠ” μ‹€μ œ 이미지λ₯Ό λ‹€μ‹œ λ‘œλ”©ν•˜μ§€ μ•ŠμŒ
    image.dispaly();

  }
}

행동 νŒ¨ν„΄

μ „λž΅ νŒ¨ν„΄(Strategy Pattern)

  • μ•Œκ³ λ¦¬μ¦˜μ΄λ‚˜ ν–‰μœ„λ₯Ό μ •μ˜ν•˜κ³ , 각각을 μΊ‘μŠν™”

  • μ•Œκ³ λ¦¬μ¦˜μ˜ 변경에 λ”°λ₯Έ 영ν–₯을 μ΅œμ†Œν™”

  • λ™μ μœΌλ‘œ μ•Œκ³ λ¦¬μ¦˜μ„ 선택

  • μ•Œκ³ λ¦¬μ¦˜μ΄λ‚˜ ν–‰μœ„(μ „λž΅)을 μ£Όμž…ν•˜μ—¬ κ΅μ²΄ν•˜κΈ° μ‰½κ²Œ λ§Œλ“œλŠ” 것

// μ „λž΅ μΈν„°νŽ˜μ΄μŠ€
interface SortingStrategy {
  void sort(int[] array);
}

// ꡬ체적인 μ „λž΅ 클래슀 - 버블 μ •λ ¬
class BubbleSortStrategy implements SortingStrategy {
  @Override
  public void sort(int[] array) {
  //μ‹€μ œ 버블 μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜ κ΅¬ν˜„
  }
}

// ꡬ체적인 μ „λž΅ 클래슀 - 퀡 μ •λ ¬
class QuickSortStrategy implements SortingStrategy {
  @Override
  public void sort(int[] array) {
  //μ‹€μ œ 퀡 μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜ κ΅¬ν˜„
  }
}

// μ»¨ν…μŠ€νŠΈ 클래슀
class SortContext {
  private SortingStrategy sortingStrategy;
  
  public void setSortingStrategy(SortingStrategy sortingStrategy) {
    this.sortingStrategy = sortingStrategy;
  }
  
  public void performSort(int[] array) {
    sortingStrategy.sort(array);
  }
}

// ν΄λΌμ΄μ–ΈνŠΈ μ½”λ“œ
public class StrategyPatternExample {
  public static void main(String[] args) {
    int[] arrayToSort = {5, 2, 8, 1, 7};
    
    SortContext sortContext = new SortContext();
    
    // 첫 번째 μ •λ ¬: 버블
    SortContext.setSortingStrategy(new BubbleSortStrategy());
    sortContext.performSort(arrayToSort);
    
    
    // 두 번째 μ •λ ¬: 퀡
    SortContext.setSortingStrategy(new QuickSortStrategy());
    sortContext.performSort(arrayToSort);
  }
}

μ˜΅μ €λ²„ νŒ¨ν„΄

  • μ–΄λ–€ 객체의 μƒνƒœκ°€ 변경될 λ•Œ 쒅속 객체듀이 μžλ™μœΌλ‘œ μ•Œλ¦Όμ„ λ°›λŠ”λ‹€.

  • κ°μ²΄λ“€μ—κ²Œ μ•Œλ¦¬κΈ°

// Observer interface
interface Observer {
  void update(String message);
}

// ConcreteObserver class
class ConcreteObserver implements Observer {
  private String name;
  
  public ConcreteObserver(String name) {
    this.name = name;
  }
  
  @Override
  public void update(String message) {
    System.out.println(name + " received message: " + message);
  }
}

// Subject class
class Subject {
  private List<Observer> observers = new ArrayList<>();
  
  // Add an observer to the list
  public void addObserver(Observer observer) {
    observers.add(observer);
  }
  
  // Remove an observer from the list
  public void removeObserver(Observer observer) {
    observers.remove(observer);
  }
  
  // Notify all observers with a message
  public void notifyObservers(String message) {
    for (Observer observer : observers) {
      observer.update(message);
    }
  }
}

// Example usage
public class ObserverPatternExample {
  public static void main(String[] args){
    // Create subject
    Subject subject = new Subject();
    
    // Create observers
    Observer observer1 = new ConcreteObserver("Observer 1");
    Observer observer2 = new ConcreteObserver("Observer 2");
    
    // Register observers with the subject
    subject.notifyObservers("Hello Observers!");
    
    // Unregister observer1
    subject.removeObserver(observer1);
    
    // Notify remaining observer with another message
    subject.notifiyObservers("Observer 1 has been removed.");
  }
}
PreviousClean CodeNextCQRS Pattern

Last updated 9 months ago