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
KI-gestützte Lerntools freischalten
Registriere dich, um leistungsstarke Tools zu nutzen, die dir helfen, schneller aus jedem Video zu lernen.
This Design Pattern Scares Me To Death
KI-Zusammenfassung
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.
Lernstatistiken
GER-Niveau
Gesamtwörter
Einzigartige Wörter
Schwierigkeit
Untertitel (688 Segmente)
This is a video about the specification
design pattern with a big warning. It's
extreme overengineering. I wasn't even
sure whether I should actually post this
video. The reason I'm doing it is
because, well, there are a couple of
really interesting design ideas that I
just don't want to keep from you. But
frankly, when I look at the final
version of the code, it scares the
out of me. It's love craftsion. Is that
a word? That should be a word. When you
first look at it, you go like, but then
it's also kind of interesting and then
it sucks you in and then bam, you're
done. We're going deep today. Higher
order functions, decorators, thunder
method, overrides, generics, the whole
shebang. It's going to be insane, but
really good. This video is sponsored by
SER API. More about them later. Now,
what is the specification? But let me
first show you a code example. I have a
user class with a bunch of different
settings. And there's a function that
checks whether this user can access some
API. As you can see, the logic is kind
of convoluted. There is an age, whether
the user is active, is it banned, what
country the user is in. Now, you can
maybe refactor this a bit to turn a lot
of these things into guard clauses,
right? But it's still kind of
complicated business logic. And you can
imagine that this is maybe something
that regularly changes, maybe because
the company changes uh the policy or
something like that. And perhaps you
have very similar kind of business logic
in other places as well like can you
access some kind of report which you
know looks kind of like it but is
slightly different or maybe you have
another function that checks whether you
can access the CLI which again is
slightly different from the first
version. Now maybe this is intended
maybe not who knows. The problem with
this type of business logic is that well
here this example is of course very
basic but this would normally be spread
out over your entire codebase and mixed
with other types of business logic and
whenever you want those rules to change
it means you have to update this in many
different places and you don't have a
clear overview of what those rules
actually are so today I'm going to show
you the specification pattern which
fixes this by turning these complicated
if conditions into reusable building
blocks and ultimately this will let you
turn all of this code into data and I'll
show you an example of how that works at
the end of this video. Now this
specification pattern doesn't come from
the gang of four book it's from domain
driven design and at the core the idea
is to encapsulate business rules into
small specifications and then you can
combine these specifications using
boolean logic and then you can reuse
that everywhere. Now if you have a
classical object-oriented language, you
would then basically implement this with
lots of small classes and each class
represents one of these business rules.
You can find a bit more information
about this pattern on Wikipedia. Uh so
you can see there is like a UML diagram
where we have various types of
specifications.
Uh there are some code example which is
extremely abstract. So, and when I read
this the first time, I had a hard time
understanding why you would actually
need this because what's the advantage,
right? You can just write the same logic
in an if condition. So, why would you
have all of these classes to wrap around
that? So, that's what I'm trying to
address in today's video. There's even a
Python example in Wikipedia that adds
all of these classes without really
showing why you would want to do that.
The only actual example that's given on
the Wikipedia page is in my opinion
something pretty basic that you can also
just capture in a regular if condition.
So in this video I hope I can take this
a few steps further. Now the goal that
we have is that we want to have rules
that are reusable and composable. And
obviously like I already said the
example here is pretty basic. uh but I
do think in Python we can do much better
than this objectoriented example from
Wikipedia. In Python we can use
functions in much better way. So we can
build a small functional framework for
these rules and we can rely on other
features like uh decorators and generics
and cool stuff like that that we have
access to in Python. So the first step
that I want to take is to move these
conditions out of this if statement into
a sort of uh let's call that a
predicate. Basically a predicate is a
function that returns true or false and
then we can later on combine these
functions so that we get more
complicated logic. So for example here
you see a bit of code that could be
turned into a predicate. So that would
be then a simple function called let's
say is admin.
That's a user.
It returns a boolean value and this
returns whether the user is an admin. So
that's a function returning true or
false. That could be a predicate. That
could be a rule. And we can do this for
other types of rules as well. For
example, is active
like so. or this check whether the
account is over 30 days.
So these are some basic example of rules
that we can pull out of these functions.
Vollständige Untertitel im Videoplayer verfügbar
Wichtige Vokabeln (49)
Du bist sehr nett. (You are very kind.)
Verändern bedeutet, etwas anders zu machen oder selbst anders zu werden.
An einen Ort zurückkehren. Etwas an seinen ursprünglichen Besitzer oder Ort zurückgeben.
Mit Übungen trainieren
Erstelle Vokabel-, Grammatik- und Verständnisübungen aus diesem Video
Kommentare (0)
Zum Kommentieren AnmeldenRegistriere dich, um alle Features freizuschalten
Verfolge deinen Fortschritt, speichere Vokabeln und übe mit Übungen
Interaktiver Modus
Quiz
Richtige Antwort:
Ähnliche Videos
Late Night with Seth Meyers
Quiz
Richtige Antwort:
Quizfragen erscheinen beim Anschauen des Videos
Merkhilfe
Aus diesem Video
Kostenlos Sprachen lernen