끄적이는 개발노트
260406_강의정리 (클래스 관계) 본문
728x90
■ 클래스 관계 종류
- 의존 (Dependency)
- 한 클래스가 다른 클래스를 일시적으로 사용
- 주로 메소드 매개 변수, 지역 변수, 리턴 타입으로 사용
# 의존
class Name:
def __init__(self, value):
self.value = value
class User:
def print_name(self, name: Name):
print(name.value)
- 연관 (Association)
- 한 클래스가 다른 클래스 객체를 지속적으로 참조
- 멤버 변수로 가짐
- 객체의 생명주기는 서로 독립
- 한 클래스 외부에서 다른 클래스 생성
# 연관
class Name:
def __init__(self, value):
self.value = value
class User:
def __init__(self, name: Name):
self.name = name
name = Name("홍길동")
user = User(name)
- 집합 (Aggregation)
- 연관 관계의 한 종류 (전체-부분)
- 여러 객체를 포함
- 부분 객체가 독립적으로 존재 가능
- 한 클래스 외부에서 다른 크래스 생성
# 집합
class Name:
def __init__(self, value):
self.value = value
class User:
def __init__(self, names: list[Name]):
self.names = names
name1 = Name("홍길동")
name2 = Name("김철수")
user = User([name1, name2])
- 포함 (Composition)
- 강한 집합 관계
- 부분 객체의 생명주기가 전체에 종속
- 한 클래스 내부에서 다른 클래스 생성
# 포함
class Name:
def __init__(self, value):
self.value = value
class User:
def __init__(self, name_value):
self.name = Name(name_value)
user = User("홍길동")
- 상속 (Inheritance)
- 기존 클래스의 기능을 물려받아 확장
- is-a 관계
- 공통 코드 재사용
- 개방-폐쇄의 원칙 (OCP, Open-Closed Principle) => 확장 O , 수정 X
# 상속
class Name:
def __init__(self, value):
self.value = value
def get_name(self):
return self.value
class User(Name):
pass
user = User("홍길동")
print(user.get_name())
- 구현 (Implementation)
- 추상클래스를 기준으로 클래스 구현
- 인터페이스의 메소드를 구현(override)
- 반드시 오버라이딩 필요
- ABC (추상클래스, Abstract Base Class), @abstractmethod (추상함수)
# 구현
from abc import ABC, abstractmethod
class Name(ABC):
@abstractmethod
def get_name(self):
pass
class User(Name):
def __init__(self, value):
self.value = value
def get_name(self):
return self.value
■ 디자인 패턴 (Design Pattern) 종류
- 생성 패턴 (Creational)
- 객체 생성 방식 추상화 (어떻게 만들까?)
- Factory Method
- Abstract Factory
- Singleton
- Builder
- Prototype
- 객체 생성 방식 추상화 (어떻게 만들까?)
- 구조 패턴 (Structural)
- 클래스/객체 구조 설계 (어떻게 구성할까?)
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Proxy
- 클래스/객체 구조 설계 (어떻게 구성할까?)
- 행위 패턴 (Behavioral)
- 객체 간 상호작용 / 책임 분리 (어떻게 동작할까?)
- Strategy
- Observer
- Command
- State
- Template Method
- Iterator
- Mediator
- 객체 간 상호작용 / 책임 분리 (어떻게 동작할까?)
■ 데코레이터 (Decorator)
- 장식자
- 함수 기능을 확장하는 wrapper 함수
# 함수 데코레이터
def my_decorator(func):
def wrapper(*args, **kwargs):
print("before")
result = func(*args, **kwargs)
print("after")
return result
return wrapper
@my_decorator
def add(a, b):
return a + b
print(add(3, 4))
# before
# after
# 7
# 클래스 데코레이터
class Verbose:
def __init__(self, f):
print("초기화")
self.func = f
def __call__(self, *args, **kwargs):
print("데코레이터 시작", self.func.__name__)
result = self.func(*args, **kwargs)
print("데코레이터 종료", self.func.__name__)
return result
@Verbose
def my_function(x, y):
print("원래 함수의 기능")
return x + y
print(my_function(3, 4))
# 초기화
# 데코레이터 시작 my_function
# 원래 함수의 기능
# 데코레이터 종료 my_function
# 7
728x90
'Python' 카테고리의 다른 글
| 260415_강의 정리 (Flask) (1) | 2026.04.15 |
|---|---|
| 260410_강의정리 (Python을 이용한 MySQL DB 연동) (0) | 2026.04.10 |
| 260402_강의정리 (상속) (0) | 2026.04.02 |
| 260401_강의정리 (벡터) (0) | 2026.04.01 |
| 260401_건양대 특강 정리 (0) | 2026.04.01 |
