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 학습 도구 잠금 해제

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

장면 설명 표현 찾기 플래시카드 복습 섀도잉 연습 되말하기
무료 회원가입
영어 15:24 Science & Tech

I Hate Long If-Elif Chains: This Design Pattern Solved It Once and For All

Late Night with Seth Meyers · 352,275 조회수 · 추가됨 2개월 전

자막 (373 세그먼트)

00:00

If your code looks like this, you're in

00:04

trouble. You wanted something clean and

00:07

extensible, a system where you could

00:08

just, you know, plug in a new exporter

00:11

or an algorithm, but it turned into a

00:14

hot mess of if else statements. Today,

00:18

I'll show you a simple design pattern

00:20

that solves exactly this problem, the

00:22

registry. It's easy to implement, and

00:25

once you know how it works, you want to

00:27

use this everywhere. Let's dive in. If

00:29

you want to learn more about how to

00:30

design a piece of software from scratch,

00:32

I have a free guide for you. You can get

00:34

it at iron.co/designguide.

00:36

This contains the seven steps I take

00:38

when I design new software. Hopefully,

00:41

it helps you avoid some of the mistakes

00:42

I made in the past. Iron.code/design

00:45

guide. Link is also in the video

00:47

description. Now, what is the registry

00:49

pattern? This pattern is a way to avoid

00:52

hard- coding logic like those if else

00:54

statements by storing that behavior in a

00:58

sort of central registry. In Python,

01:01

that can be a dictionary for example.

01:03

And then instead of calling functions

01:05

directly, you register them under a key

01:08

like a string or an enum and later call

01:11

them dynamically. It's like a sort of

01:14

named plug-in map, one that's easy to

01:16

extend without changing the core logic

01:19

of your application. If you look at this

01:21

example, we have an export data function

01:23

that picks the exporter based on the

01:26

format, which is a string. Now, you

01:28

could use an enum for that. I kept it to

01:30

a string also because that maps better

01:32

to the example that I'll show you after

01:33

this one. But the problem here is that

01:36

this is like this chain of if else

01:38

statements. Uh depending on the format,

01:40

you call a different function. And if

01:42

you want to add more exporters, you're

01:43

going to add more lines of code here.

01:45

This function is going to get really

01:46

long. It's not really a nice design,

01:50

right? And by the way, these exporter

01:52

functions in this particular example,

01:53

they don't really do anything. They just

01:55

print a message cuz it's not really

01:57

about exporting. It's more about how you

02:00

uh patch everything up. And then the

02:02

main function, I call export data with

02:04

various formats. Now, you could say,

02:06

hey, why don't just call export PDF and

02:09

export CSV? But that kind of defeats the

02:11

purpose because maybe you want to be

02:13

able to select the exporter in a

02:16

dropdown for example and then you want

02:18

to call that function directly. Now if

02:20

we run this example then this is what we

02:22

get. You see we get some export results.

02:25

So how do we change this so that here we

02:27

no longer have to deal with this if else

02:30

statement? Well, we're going to use the

02:32

registry. Like I mentioned in the

02:34

beginning, a registry is a sort of

02:36

central place where you keep track of

02:38

all of these different things. So let's

02:42

create something called exporters and

02:44

that's a dictionary of strings to export

02:47

functions. That's a type that I defined

02:49

here which is a collable that takes data

02:53

and doesn't return any results. It's

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

연습 문제로 학습하기

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

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

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

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

무료로 언어 학습