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
ابزارهای یادگیری هوش مصنوعی را باز کنید
ثبتنام کنید تا به ابزارهای قدرتمندی دسترسی پیدا کنید که به شما کمک میکنند سریعتر از هر ویدیو یاد بگیرید.
I Hate Long If-Elif Chains: This Design Pattern Solved It Once and For All
زیرنویسها (373 بخشها)
If your code looks like this, you're in
trouble. You wanted something clean and
extensible, a system where you could
just, you know, plug in a new exporter
or an algorithm, but it turned into a
hot mess of if else statements. Today,
I'll show you a simple design pattern
that solves exactly this problem, the
registry. It's easy to implement, and
once you know how it works, you want to
use this everywhere. Let's dive in. If
you want to learn more about how to
design a piece of software from scratch,
I have a free guide for you. You can get
it at iron.co/designguide.
This contains the seven steps I take
when I design new software. Hopefully,
it helps you avoid some of the mistakes
I made in the past. Iron.code/design
guide. Link is also in the video
description. Now, what is the registry
pattern? This pattern is a way to avoid
hard- coding logic like those if else
statements by storing that behavior in a
sort of central registry. In Python,
that can be a dictionary for example.
And then instead of calling functions
directly, you register them under a key
like a string or an enum and later call
them dynamically. It's like a sort of
named plug-in map, one that's easy to
extend without changing the core logic
of your application. If you look at this
example, we have an export data function
that picks the exporter based on the
format, which is a string. Now, you
could use an enum for that. I kept it to
a string also because that maps better
to the example that I'll show you after
this one. But the problem here is that
this is like this chain of if else
statements. Uh depending on the format,
you call a different function. And if
you want to add more exporters, you're
going to add more lines of code here.
This function is going to get really
long. It's not really a nice design,
right? And by the way, these exporter
functions in this particular example,
they don't really do anything. They just
print a message cuz it's not really
about exporting. It's more about how you
uh patch everything up. And then the
main function, I call export data with
various formats. Now, you could say,
hey, why don't just call export PDF and
export CSV? But that kind of defeats the
purpose because maybe you want to be
able to select the exporter in a
dropdown for example and then you want
to call that function directly. Now if
we run this example then this is what we
get. You see we get some export results.
So how do we change this so that here we
no longer have to deal with this if else
statement? Well, we're going to use the
registry. Like I mentioned in the
beginning, a registry is a sort of
central place where you keep track of
all of these different things. So let's
create something called exporters and
that's a dictionary of strings to export
functions. That's a type that I defined
here which is a collable that takes data
and doesn't return any results. It's
زیرنویس کامل در پخشکننده ویدیو موجود است
با تمرینها یاد بگیرید
تمرینهای واژگان، گرامر و درک مطلب از این ویدیو بسازید
نظرات (0)
برای نظر دادن وارد شویدثبتنام کن و همه امکانات رو باز کن
پیشرفتت رو دنبال کن، واژگان رو ذخیره کن و تمرین کن
حالت تعاملی
آزمون
پاسخ صحیح:
ویدیوهای مرتبط
Let’s Travel to the Scariest Place in The Universe
RASPBERRY PI PICO FROM ALI EXPRESS: REAL OR FAKE??
Reverse Engineering Ali Express Spy Camera
Python Properties vs Methods: The Contract You Didn’t Know You Were Making
You’ve Been Underusing Dataclasses (These Tricks Are Wild)
Late Night with Seth Meyers
آزمون
پاسخ صحیح:
آزمونها هنگام تماشای ویدیو ظاهر میشوند
راهنمای حفظ
از این ویدیو
شروع رایگان یادگیری زبان