[Clean Code] 6-1 객체지향 절차지향

2021. 9. 6. 00:02·IT/Clean Code
반응형

객체와 자료구조

객체 지향적 과 절차 지향적의 차이

절차 지향적인 코드는 기존 자료구조를 변경하지 않으면서 새 함수를 추가하기 쉽다.
반면 객체 지향적인 코드는 변경점이 생기면 기존 함수를 변경해야 하는 부분이 생길 수 있다.

모든 것을 객체 지향적인것 보다는 때로는 단순한 것이 적합한 상황이 있을 수 있다.

절차 지향적

class Squre(object):
    x: float
    y: float
    side: float

class Rectangle(object):
    x: float
    y: float
    width: float
    height: float

class Circle(object):
    x: float
    y: float
    radius: float

def get_arae(figure: object) -> float:
    if isinstance(figure, Squre):
        return figure.side * figure.side

    elif isinstance(figure, Rectangle):
        return figure.width * figure.height

    elif isinstance(figure, Circle):
        return math.pi * figure.radius * figure.radius

    else:
        raise Exception('no such figure')

객체 지향적

class Point(object):
    x: float
    y: float

class Figure(object):
    point: Point

    def get_area(self) -> float:
        raise NotImplementedError("Not Implemented")

class Squre(Figure):
    side: float

    def get_area(self) -> float:
        return self.side * self.side

class Rectangle(Figure):
    width: float
    height: float

    def get_area(self) -> float:
        return self.width * self.height

class Circle(Figure):
    radius: float

    def get_area(self) -> float:
        return math.pi * self.radius * self.radius
import (
    "math"
)

type Point struct {
    x float32
    y float32
}

type Figure interface {
    GetArea() float32
}

type Sqaure struct {
    point *Point
    side  float32
}

type Rectangle struct {
    point  *Point
    width  float32
    height float32
}

type Circle struct {
    point  *Point
    radius float32
}

func (s *Sqaure) GetArea() float32 {
    return s.side * s.side
}

func (r *Rectangle) GetArea() float32 {
    return r.width * r.height
}

func (c *Circle) GetArea() float32 {
    return math.Pi * c.radius * c.radius
}
728x90
반응형
저작자표시 비영리 (새창열림)
'IT/Clean Code' 카테고리의 다른 글
  • [Clean Code] 7-1 예외처리
  • [Clean Code] 6-2 객체지향 절차지향 2
  • [Clean Code] 5-1 형식
  • [Clean Code] 4-1 주석
상쾌한기분
상쾌한기분
  • 상쾌한기분
    상쾌한기분
    상쾌한기분
  • 전체
    오늘
    어제
    • 분류 전체보기 (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 방문
  • 링크

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
상쾌한기분
[Clean Code] 6-1 객체지향 절차지향
상단으로

티스토리툴바