-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


vs interface

, 31 2017 . 09:01 +

- , . , - Java, , , .


" " , . - Java interface, , ", interface". Python interface , .


Smalltalk C++. interface, , :


:
  • , , , ;
  • "" , . ( ), .


, - : , .

, .



:


" " , ; , .

, . (, , , ). , . , , , .


: , , , . : , , . , , , . , .



, . " " . , , , . , .


. . , , :


class UserCollection:
    #   
    def linear_search_for(user: User) -> bool:
        for saved_user in self._all_users:
            if saved_user == user:
                return True
        return False

, , UserCollection. , . , "" , "" :


class UserCollection:
    #   
    def includes(user: User) -> bool:
        '''    '''

, . , , , , :


from utils import DatabaseConfig
# DatabaseConfig    ,
#    

def is_password_valid(password: str) -> bool:
    min_length = DatabaseConfig().password_min_length
    return len(password) > min_length

, , . , , , . , . min_length:


#   DatabaseConfig   
def is_password_valid(password: str, min_length: int) -> bool:
    return len(password) > min_length

DatabaseConfig . , .


. Python , . , :


# utils.py
class DatabaseConfig:
    '''        '''

config = DatabaseConfig()

#         
def is_password_valid(password: str, min_length: int) -> bool:
    return len(password) > min_length

# user.py
from utils import is_password_valid
#     
# DatabaseConfig    

class User:
    def __init__(self, name: str, password: str):
        self.name = name
        self.password = password

    def change_password(self, new_password: str) -> None:
        if not is_password_valid(new_password, min_length=6):
            raise Exception('Invalid password')
        self.password = new_password

User , , , . , from utils import is_password_valid, , . . DatabaseConfig .


, , . . "Smalltalk Best Practice Patterns" :


There are a few things I look for that are good predictors of whether a project is in good shape.



Replacing objects Good style leads to easily replaceable objects. In a really good system, every time the user says I want to do this radically different thing, the developer says, Oh, Ill have to make a new kind of X and plug it in.

, , . Python abc, , NotImplementedError.


, . . , , , -:


# weather.py
from typing import List, NamedTuple

class Weather(NamedTuple):
    max_temperature_: int
    avg_temperature_: int
    min_temperature_c: int

class WeatherService:
    def get_today_weather(self, city: str) -> Weather:
        raise NotImplementedError

    def get_week_weather(self, city: str) -> List[Weather]:
        raise NotImplementedError

, , , , :


# test.py
from client import WeatherWidget
from weather import Weather, WeatherService

class FakeWeatherService(WeatherService):
    def __init__(self):
        self._weather = Weather(max_temperature_ = 24,
                                avg_temperature_ = 20,
                                min_temperature_c = 16)

    def get_today_weather(self, city: str) -> Weather:
        return self._weather

    def get_week_weather(self, city: str) -> List[Weather]:
        raise [self._weather for _ in range(7)]

def test_present_today_weather_in_string_format():
    weather_service = FakeWeatherService()
    widget = WeatherWidget(weather_service)
    expected_string = ('Maximum Temperature: 24 C'
                       'Average Temperature: 20 C'
                       'Minimum Temperature: 16 C')
    assert widget.today_weather == expected_string

: , , , WeatherService.


" , " , . . Python interface, : , . , , .

Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/332578/

:  

: [1] []
 

:
: 

: ( )

:

  URL