인터페이스와 구현을 분리

hyuckhoon.ko·2023년 4월 7일
0

TIL

목록 보기
4/69

인터페이스는 기본적으로 퍼블릭 메소드다.
공용 인터페이스를 통한 메시징드로 A객체는 B객체와 통신할 수가 있다.

인터페이스는 좀처럼 변하지 않는다.
하지만 구현부는 사정이 다르다.
내부 알고리즘이 변경될 수도 있다.

핵심은 A객체는 B객체의 구현부에 관심이 없다. 알아서도 안 된다.


인터페이스와 구현이 명확히 분리된 클래스를 장고에서 설계해봤다.

from datetime import date, timedelta
import pytz

from django.db import models
from django.utils import timezone


class Match(models.Model):
        schedule = models.DateTimeField(
        verbose_name="경기일시", default=timezone.now
    )

    def _get_quarter_start_date(self) -> date:
        quarter_month_start = ((self.schedule.month - 1) // 3) * 3 + 1
        return date(self.schedule.year, quarter_month_start, 1)

    def _get_quarter_end_date(self) -> date:
        quarter_month_end = ((self.schedule.month - 1) // 3) * 3 + 3
        last_day_of_month = (
            date(self.schedule.year, quarter_month_end + 1, 1) - timedelta(days=1)
        ).day
        return date(self.schedule.year, quarter_month_end, last_day_of_month)

    def get_first_date_of_quarter(self) -> date:
        return self._get_quarter_start_date()

    def get_last_date_of_quarter(self) -> date:
        return self._get_quarter_end_date()

공용 인터페이스

  • get_first_date_of_quarter
  • get_last_date_of_quarter

구현부

  • _get_quarter_start_date
  • _get_quarter_end_date

0개의 댓글