객체지향 설계 5대 원리 SOLID - IRP

2021. 1. 22. 12:13·IT
반응형

I - ISP - Interface Segregation Principle (인터페이스 분리 원칙)

특정 클라이언트를 위한 인터페이스 여러개가 범용 인터페이스 하나보다 낫다.
클래스에서 사용하지 않는 메서드는 분리해야 한다.

차량 소유주 정보는 변경점이 없어 그대로 구현

class CarOwnerInfo:

    def __init__(self, car_number, owner_name):
        self.__car_number = car_number
        self.__owner_name = owner_name

    @property
    def car_number(self):
        return self.__car_number

    @car_number.setter
    def car_number(self, value):
        self.__car_number = value

    @property
    def owner_name(self):
        return self.__owner_name

    @owner_name.setter
    def owner_name(self, value):
        self.__owner_name = value

    def show(self):
        print('Car number : {} / Owner name : {}'.format(self.car_number, self.owner_name))


class SuvOwnerInfo(CarOwnerInfo):
    pass


class TruckOwnerInfo(CarOwnerInfo):
    pass

 

트렁크 타입에 따른 인터페이스를 추가하여 분산 구현

class CarDetailInfo(metaclass = ABCMeta):

    def __init__(self, color, wheel):
        self.__color = color
        self.__wheel = wheel

    @abstractmethod
    def show(self):
        pass

    @property
    def color(self):
        return self.__color

    @property
    def wheel(self):
        return self.__wheel


class SuvDetailInfo(CarDetailInfo):

    def show(self):
        print('Color : {} / Wheel : {}'.format(self.color, self.wheel))


class TrunkType1(metaclass = ABCMeta):
    is_trunk_opened = False
    trunk_kind = None

    @abstractmethod
    def covered(self, value):
        pass

    @abstractmethod
    def opened(self):
        pass


class TruckDetailInfo(CarDetailInfo, TrunkType1):

    def covered(self, value):
        self.is_trunk_opened = False
        self.trunk_kind = value

    def opened(self):
        self.is_trunk_opened = True

    def show(self):
        print('Color : {} / Wheel : {} / Trunk : {} {}'.format(self.color, self.wheel, self.trunk_kind, self.is_trunk_opened))

 

참고

www.fun-coding.org/PL&OOP2-1.html

728x90
반응형
저작자표시 비영리 (새창열림)
'IT' 카테고리의 다른 글
  • 소프트웨어 개발원칙 KISS
  • 소프트웨어 개발 원칙 DRY
  • 객체지향 설계 5대 원리 SOLID - OCP
  • 객체지향 설계 5대 원리 SOLID - SRP
상쾌한기분
상쾌한기분
  • 상쾌한기분
    상쾌한기분
    상쾌한기분
  • 전체
    오늘
    어제
    • 분류 전체보기 (251)
      • Python (44)
        • Python (26)
        • Django (6)
        • Flask (4)
        • Open Source (6)
      • Kotlin & Java (5)
        • Spring (2)
        • 프로젝트 (1)
      • Go (11)
      • Database (24)
        • MySQL (21)
        • Redis (3)
      • Infrastructure (2)
        • CDC (4)
        • Kafka (5)
        • Prometheus (2)
        • Fluentd (11)
        • Docker (1)
        • Airflow (2)
        • VPN (2)
      • IT (26)
        • AI (9)
        • Langchain (8)
        • Web (18)
        • Git (8)
        • 리팩토링 (9)
        • Micro Service Architecture (8)
        • Clean Code (16)
        • Design Pattern (0)
        • 수학 (1)
        • 알고리즘 (14)
      • OS (14)
        • Centos (10)
        • Ubuntu (3)
        • Mac (1)
      • Search Engine (2)
        • ElasticSearch (1)
        • Lucene Solr (1)
      • PHP (2)
        • Laravel (1)
        • Codeigniter (1)
  • 블로그 메뉴

    • Github 방문
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    performance
    Langchain
    prompt
    티스토리챌린지
    백준
    Redis
    Golang
    파이썬
    docker
    python
    fluentd
    ollama
    Kafka
    오블완
    http
    MYSQL
    git
    go
    CDC
    LLM
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
상쾌한기분
객체지향 설계 5대 원리 SOLID - IRP
상단으로

티스토리툴바