The video owner has disabled playback on external websites.

This video is no longer available on YouTube.

This video cannot be played right now.

Watch on YouTube

AI 학습 도구 잠금 해제

가입하여 모든 동영상에서 더 빠르게 학습할 수 있는 강력한 도구를 이용하세요.

장면 설명 표현 찾기 플래시카드 복습 섀도잉 연습 되말하기
무료 회원가입
A2 초등급 영어 29:36 4,441 단어 Science & Tech

This Design Pattern Scares Me To Death

Late Night with Seth Meyers · 72,793 조회수 · 추가됨 1개월 전

AI 요약

This video provides a deep dive into the **Specification Pattern**, a design technique from Domain-Driven Design used to encapsulate complex business rules into reusable, composable blocks. While the instructor warns that this approach can lead to overengineering, they demonstrate how to move beyond basic, tangled `if` statements. You will learn to use Python’s functional features—such as decorators, dunder methods, and generics—to create a flexible "predicate" framework. By the end, you will understand how to separate the definition of business logic from its execution, allowing you to treat complex rules as manageable, modular data components.

학습 통계

A2

CEFR 레벨

4,441

총 단어 수

688

고유 단어

3/10

난이도

어휘 다양성 15%

자막 (688 세그먼트)

00:00

This is a video about the specification

00:02

design pattern with a big warning. It's

00:05

extreme overengineering. I wasn't even

00:08

sure whether I should actually post this

00:10

video. The reason I'm doing it is

00:12

because, well, there are a couple of

00:13

really interesting design ideas that I

00:16

just don't want to keep from you. But

00:18

frankly, when I look at the final

00:20

version of the code, it scares the

00:22

out of me. It's love craftsion. Is that

00:25

a word? That should be a word. When you

00:28

first look at it, you go like, but then

00:30

it's also kind of interesting and then

00:32

it sucks you in and then bam, you're

00:34

done. We're going deep today. Higher

00:37

order functions, decorators, thunder

00:39

method, overrides, generics, the whole

00:41

shebang. It's going to be insane, but

00:44

really good. This video is sponsored by

00:46

SER API. More about them later. Now,

00:49

what is the specification? But let me

00:51

first show you a code example. I have a

00:53

user class with a bunch of different

00:56

settings. And there's a function that

00:57

checks whether this user can access some

00:59

API. As you can see, the logic is kind

01:02

of convoluted. There is an age, whether

01:04

the user is active, is it banned, what

01:07

country the user is in. Now, you can

01:09

maybe refactor this a bit to turn a lot

01:12

of these things into guard clauses,

01:14

right? But it's still kind of

01:16

complicated business logic. And you can

01:18

imagine that this is maybe something

01:19

that regularly changes, maybe because

01:22

the company changes uh the policy or

01:24

something like that. And perhaps you

01:26

have very similar kind of business logic

01:29

in other places as well like can you

01:32

access some kind of report which you

01:34

know looks kind of like it but is

01:36

slightly different or maybe you have

01:39

another function that checks whether you

01:41

can access the CLI which again is

01:43

slightly different from the first

01:45

version. Now maybe this is intended

01:48

maybe not who knows. The problem with

01:50

this type of business logic is that well

01:52

here this example is of course very

01:54

basic but this would normally be spread

01:56

out over your entire codebase and mixed

01:59

with other types of business logic and

02:01

whenever you want those rules to change

02:04

it means you have to update this in many

02:05

different places and you don't have a

02:07

clear overview of what those rules

02:09

actually are so today I'm going to show

02:12

you the specification pattern which

02:14

fixes this by turning these complicated

02:16

if conditions into reusable building

02:20

blocks and ultimately this will let you

02:22

turn all of this code into data and I'll

02:24

show you an example of how that works at

02:26

the end of this video. Now this

02:28

specification pattern doesn't come from

02:30

the gang of four book it's from domain

02:32

driven design and at the core the idea

02:34

is to encapsulate business rules into

02:36

small specifications and then you can

02:39

combine these specifications using

02:41

boolean logic and then you can reuse

02:43

that everywhere. Now if you have a

02:45

classical object-oriented language, you

02:48

would then basically implement this with

02:50

lots of small classes and each class

02:52

represents one of these business rules.

02:54

You can find a bit more information

02:56

about this pattern on Wikipedia. Uh so

02:59

you can see there is like a UML diagram

03:01

where we have various types of

03:02

specifications.

03:04

Uh there are some code example which is

03:07

extremely abstract. So, and when I read

03:10

this the first time, I had a hard time

03:11

understanding why you would actually

03:13

need this because what's the advantage,

03:16

right? You can just write the same logic

03:18

in an if condition. So, why would you

03:19

have all of these classes to wrap around

03:22

that? So, that's what I'm trying to

03:24

address in today's video. There's even a

03:26

Python example in Wikipedia that adds

03:29

all of these classes without really

03:31

showing why you would want to do that.

03:34

The only actual example that's given on

03:36

the Wikipedia page is in my opinion

03:38

something pretty basic that you can also

03:40

just capture in a regular if condition.

03:43

So in this video I hope I can take this

03:45

a few steps further. Now the goal that

03:47

we have is that we want to have rules

03:50

that are reusable and composable. And

03:54

obviously like I already said the

03:55

example here is pretty basic. uh but I

03:58

do think in Python we can do much better

04:01

than this objectoriented example from

04:04

Wikipedia. In Python we can use

04:06

functions in much better way. So we can

04:08

build a small functional framework for

04:10

these rules and we can rely on other

04:13

features like uh decorators and generics

04:16

and cool stuff like that that we have

04:19

access to in Python. So the first step

04:21

that I want to take is to move these

04:24

conditions out of this if statement into

04:26

a sort of uh let's call that a

04:28

predicate. Basically a predicate is a

04:30

function that returns true or false and

04:33

then we can later on combine these

04:36

functions so that we get more

04:37

complicated logic. So for example here

04:40

you see a bit of code that could be

04:41

turned into a predicate. So that would

04:43

be then a simple function called let's

04:45

say is admin.

04:48

That's a user.

04:50

It returns a boolean value and this

04:52

returns whether the user is an admin. So

04:57

that's a function returning true or

04:59

false. That could be a predicate. That

05:01

could be a rule. And we can do this for

05:02

other types of rules as well. For

05:04

example, is active

05:11

like so. or this check whether the

05:13

account is over 30 days.

05:23

So these are some basic example of rules

05:25

that we can pull out of these functions.

전체 자막은 비디오 플레이어에서 이용 가능

핵심 어휘 (49)

you A1 pronoun

당신은 누구십니까? (Who are you?)

change A1 verb

변화하다는 무언가를 다르게 만들거나 스스로 다르게 변하는 것을 의미합니다.

return A1 verb

원래 있던 곳으로 돌아오거나 가다. 물건을 원래의 주인이나 장소로 돌려주다.

연습 문제로 학습하기

이 동영상에서 어휘, 문법, 이해력 연습 문제를 만드세요

어휘 및 문법 이해력 퀴즈 IELTS 시험 쓰기 연습
회원가입해서 연습하기
아직 댓글이 없습니다. 첫 번째로 생각을 공유하세요!

가입하고 모든 기능 잠금 해제

진행 상황 추적, 단어 저장, 연습 문제 풀기

무료로 언어 학습