[Cosmic Python] Introduction

Introduction

Why do our designs go wrong?

Encapsulation and Abstractions

이 글에서는 먼저 첫번째 의미를 다룬다.

우리는 코드에서 수행해야할 작업을 식별하고, 해당 작업을 잘 정의된 개체 또는 함수에 제공하여 동작을 캡슐화 한다.이때 그 객체 또는 함수를 추상화라고 부른다.

import json
from urllib.request import urlopen
from urllib.parse import urlencode

params = dict(q='Sausages', format='json')
handle = urlopen('http://api.duckduckgo.com' + '?' + urlencode(params))
raw_text = handle.read().decode('utf8')
parsed = json.loads(raw_text)

results = parsed['RelatedTopics']
for r in results:
    if 'Text' in r:
        print(r['FirstURL'] + ' - ' + r['Text'])
import requests

params = dict(q='Sausages', format='json')
parsed = requests.get('http://api.duckduckgo.com/', params=params).json()

results = parsed['RelatedTopics']
for r in results:
    if 'Text' in r:
        print(r['FirstURL'] + ' - ' + r['Text'])

위 코드의 동작은 같다. 그러나 두번째 코드가 더 읽기 쉽고 이해하기 쉽다. 왜냐하면, 더 높은 수준에서 추상화로 동작 하였기 때문이다.

Layering

The Dependency Inversion Principle

A Place for All Our Business Logic: The Domain Model