Strategy Pattern – Design Patterns (ep 1)

00:35:11
https://www.youtube.com/watch?v=v9ejT8FO-7I

Resumen

TLDRCette vidéo est une introduction au modèle de stratégie dans le cadre des modèles de conception. Elle explique pourquoi la composition est préférable à l'héritage, illustrant cela avec un exemple de canards. Le modèle de stratégie définit une famille d'algorithmes qui peuvent être échangés, ce qui permet aux algorithmes de varier indépendamment des clients qui les utilisent. Cela réduit les complexités liées aux changements dans les exigences tout en évitant la duplication de code. La vidéo encourage également à considérer une lecture du livre recommandé pour une meilleure compréhension des modèles de conception.

Para llevar

  • 📘 Introduction aux modèles de conception
  • 🔄 Préférer la composition à l'héritage
  • ⚙️ Encapsulation d'algorithmes
  • 💡 Variabilité des algorithmes
  • 🐤 Exemple des classes de canards
  • 📈 Meilleure évolutivité du code
  • ⚠️ Éviter la duplication de code
  • 📖 Recommandation de livre pour approfondir
  • ✅ Méthode d'injection de dépendance
  • 🔍 Analyse UML du modèle de stratégie

Cronología

  • 00:00:00 - 00:05:00

    Cette série sur les motifs de conception débute avec une introduction au livre de référence et une vue d'ensemble des 13 motifs qui seront traités. L'importance de comprendre que ce livre est plus pédagogique que technique est soulignée, avec un accent sur l'apprentissage des motifs de conception plutôt que sur des définitions sèches.

  • 00:05:00 - 00:10:00

    Le premier motif présenté est le motif strategy, qui est décrit comme un moyen d'utiliser la composition plutôt que l'héritage. La définition officielle stipule qu'il définit une famille d'algorithmes, chacun étant encapsulé et interchangeable, permettant aux algorithmes de varier indépendamment des clients qui les utilisent.

  • 00:10:00 - 00:15:00

    Le motif strategy permet de décoller les algorithmes de leurs clients, illustré par des exemples de classes de canards. L'idée est que les différentes sortes de canards peuvent afficher des comportements variés sans que les clients aient besoin de connaître leurs implémentations internes. C'est un moyen de rendre le code plus flexible et moins sujet aux erreurs lors de changements de comportement.

  • 00:15:00 - 00:20:00

    L'héritage est montré comme une solution potentiellement problématique lorsque l'on doit gérer des comportements partagés, entraînant une duplication de code. On voit comment des classes comme canard en caoutchouc nécessiteraient des modifications indésirables à cause d'un comportement hérité non pertinent comme 'voler'.

  • 00:20:00 - 00:25:00

    Le passage à la composition via le motif strategy est expliqué en démontrant la création d'interfaces pour les comportements des algorithmes spécifiques comme 'quacker' et 'fly', permettant à chaque canard d'adopter des comportements variés sans redéfinir la logique dans chaque classe de canard.

  • 00:25:00 - 00:30:00

    Une fois les interfaces en place, des comportements spécifiques sont créés, illustrant comment les différents types de canards peuvent se comporter différemment tout en partageant d'autres logiques au sein des classes. Par exemple, un canard peut avoir un comportement de 'quack' simple ou aucun comportement de 'quack' du tout.

  • 00:30:00 - 00:35:11

    Au fur et à mesure que la conception progresse, l'importance de l'injection de dépendances est examinée, soulignant que les comportements ne devraient pas être codés en dur dans les classes, afin de maintenir la capacité de modification sans impacts indésirables sur l'ensemble du code. Le diagramme UML est introduit pour résumer la structure du motif strategy et sa flexibilité, renforçant le besoin de cette approche en programmation orientée objet.

Ver más

Mapa mental

Vídeo de preguntas y respuestas

  • Qu'est-ce qu'un modèle de stratégie ?

    Un modèle de stratégie est un modèle de conception qui définit une famille d'algorithmes, les encapsule et les rend interchangeables.

  • Pourquoi privilégier la composition à l'héritage ?

    La composition permet de changer de comportement sans altérer la classe cliente, contrairement à l'héritage.

  • Comment le modèle de stratégie aide-t-il lors des changements de spécifications ?

    Il permet d'adapter les algorithmes sans devoir modifier les classes clientes, ce qui réduit le risque de bogues.

  • Quel est l'avantage de décorréler comportements et classes ?

    Cela facilite l'évolutivité et la maintenabilité du code, permettant de gérer la complexité.

  • Quel est un exemple de mise en œuvre du modèle de stratégie ?

    Utiliser des classes pour les comportements de vol et de canard, permettant différentes implémentations sans modification des classes de canard.

Ver más resúmenes de vídeos

Obtén acceso instantáneo a resúmenes gratuitos de vídeos de YouTube gracias a la IA.
Subtítulos
en
Desplazamiento automático:
  • 00:00:01
    welcome to the series on design patterns
  • 00:00:04
    in this series we're going to talk about
  • 00:00:06
    the design patterns in this book head
  • 00:00:09
    the first design patterns one by one so
  • 00:00:14
    at least we're going to look at thirteen
  • 00:00:16
    patterns before we get started if you're
  • 00:00:18
    getting started with the design patterns
  • 00:00:20
    I highly recommend this book if you
  • 00:00:22
    already know a lot about design patterns
  • 00:00:23
    and you're looking for maybe a reference
  • 00:00:26
    book this is not really it this is more
  • 00:00:28
    of a very talking Illustrated super
  • 00:00:31
    pedagogical book for learning design
  • 00:00:34
    patterns right it's there's tons of
  • 00:00:36
    super silly jokes in this book so if
  • 00:00:39
    you're just looking for the definitions
  • 00:00:41
    and the UML diagrams like that don't get
  • 00:00:44
    this book but if you want to learn
  • 00:00:45
    design patterns and the rationale behind
  • 00:00:47
    them get this book check the description
  • 00:00:51
    but so let's get into it
  • 00:00:53
    pattern number one strategy pattern
  • 00:00:58
    strategy pattern is a super sensible way
  • 00:01:00
    to start if you're just learning design
  • 00:01:02
    patterns it's actually probably the
  • 00:01:04
    simplest pattern if I were to think of
  • 00:01:06
    it in only a few words I would say that
  • 00:01:08
    it's about using composition rather than
  • 00:01:11
    inheritance it's about understanding
  • 00:01:12
    that inheritance is not intended for
  • 00:01:16
    code reuse but let's check out the
  • 00:01:18
    official definition from this book so
  • 00:01:21
    they're saying the strategy pattern
  • 00:01:23
    defines a family of algorithms
  • 00:01:26
    encapsulate s' each one and makes them
  • 00:01:29
    interchangeable strategy let's the
  • 00:01:32
    algorithm vary independently from
  • 00:01:36
    clients that use it at all so let's
  • 00:01:39
    think about this strategy pattern
  • 00:01:41
    defines a family of algorithms okay when
  • 00:01:44
    you're using strategy pattern you have a
  • 00:01:46
    family of algorithms yeah set of
  • 00:01:49
    algorithms that you want to use it
  • 00:01:50
    encapsulates each one of them and makes
  • 00:01:52
    them interchangeable okay so you have
  • 00:01:54
    this bunch of algorithms and each of
  • 00:01:57
    them are interchangeable right so you
  • 00:01:58
    have algorithm a you have algorithm B
  • 00:02:00
    you have algorithm C and there they're
  • 00:02:02
    claiming that the strategy pattern you
  • 00:02:04
    can plug and play you can sometimes use
  • 00:02:06
    algorithm a
  • 00:02:07
    sometimes use algorithm B sometimes use
  • 00:02:09
    algorithm C and so forth and then the
  • 00:02:12
    second line I guess it's more like the
  • 00:02:14
    rationale right then they're saying
  • 00:02:16
    strategy let's the algorithm vary
  • 00:02:18
    independently from the clients that use
  • 00:02:20
    it so think about the word decoupling
  • 00:02:22
    we've decoupled the algorithm from the
  • 00:02:26
    one using the algorithm so we said that
  • 00:02:28
    we can vary between algorithm a and
  • 00:02:30
    algorithm B and algorithm C for example
  • 00:02:32
    so whoever is using algorithm a or
  • 00:02:35
    algorithm B your algorithm C that thing
  • 00:02:37
    will usually refer to these things as
  • 00:02:39
    clients in this book that client does
  • 00:02:42
    not have to vary if one of the
  • 00:02:44
    algorithms varies in other words when we
  • 00:02:46
    say vary we mean if you want to change
  • 00:02:49
    one of the algorithms the contents of
  • 00:02:51
    the algorithms what the algorithm
  • 00:02:52
    actually does then you don't necessarily
  • 00:02:54
    have to change the client at the same
  • 00:02:57
    time so whoever is using that algorithm
  • 00:03:00
    is not forced to change when you are
  • 00:03:02
    changing at one of the algorithms and
  • 00:03:04
    that's super good this will make more
  • 00:03:07
    sense in the end but if you think about
  • 00:03:08
    an implementation of a collection for
  • 00:03:10
    example an implementation of a list if
  • 00:03:12
    the list had a sorting algorithm built
  • 00:03:15
    into it you can never change the sorting
  • 00:03:18
    algorithm but if you using strategy
  • 00:03:20
    pattern inject a sorting strategy then
  • 00:03:23
    the sorting strategy can vary
  • 00:03:25
    independently from the list so you have
  • 00:03:28
    the implementation of the list and
  • 00:03:29
    that's stable but then you inject
  • 00:03:32
    different sorting algorithms but that's
  • 00:03:35
    far beyond let's now get into this so
  • 00:03:37
    generally in this series I'll make heavy
  • 00:03:40
    use of the examples from this book but I
  • 00:03:42
    won't just give them one to one I'm
  • 00:03:44
    slightly adapting them because I think
  • 00:03:46
    some of them are just overly complicated
  • 00:03:48
    and I'll try to go a bit faster but
  • 00:03:51
    here's the gist of their example so in
  • 00:03:54
    the example there's a duck class the
  • 00:03:56
    duck class is a superclass so the
  • 00:03:59
    intention is that other classes should
  • 00:04:02
    inherit from the duck class so they have
  • 00:04:04
    something like a mallard duck and some
  • 00:04:06
    other duck and that's just confusing
  • 00:04:07
    because I don't even know what a mallard
  • 00:04:09
    duck
  • 00:04:09
    is and I don't think it's relevant
  • 00:04:11
    example so let's just say that we have a
  • 00:04:14
    wild duck and a city duck so wild duck
  • 00:04:17
    would be a duck out in the forest or
  • 00:04:19
    wherever ducks hang out and a city duck
  • 00:04:22
    would be a duck in the city
  • 00:04:24
    now that hangs out in the palms in the
  • 00:04:26
    city makes no sense but it doesn't
  • 00:04:28
    really matter the point is that we have
  • 00:04:29
    two types of ducks and notice how I make
  • 00:04:32
    use of this kind of arrow and this kind
  • 00:04:35
    of arrow so in in UML this is is a-- and
  • 00:04:38
    and this is has a--
  • 00:04:41
    so composition is this one inheritance
  • 00:04:45
    is this one so in other words a wild
  • 00:04:48
    duck is a duck ah city duck is a duck so
  • 00:04:53
    this would mean that a duck has a wild
  • 00:04:55
    duck right this would mean that a wild
  • 00:05:00
    duck has a duck but now we're saying a
  • 00:05:03
    wild duck is a duck because of this
  • 00:05:06
    movie so the point in the example is
  • 00:05:08
    that the sub classing ducks the
  • 00:05:11
    subclasses are responsible for
  • 00:05:13
    implementing their own version of the
  • 00:05:16
    display method
  • 00:05:17
    so while duck has its own display method
  • 00:05:20
    city duck has its own display method and
  • 00:05:24
    thus wild ducks can be displayed
  • 00:05:26
    differently than city ducks and so forth
  • 00:05:29
    if we would add another class this would
  • 00:05:34
    have its own another duck type this
  • 00:05:36
    would have its own display method so
  • 00:05:38
    that could be displayed in a manner
  • 00:05:40
    appropriate for that duck and then of
  • 00:05:43
    course the kwok method the behavior of
  • 00:05:46
    quark is shared amongst all of these
  • 00:05:49
    subclasses and this is how they're using
  • 00:05:51
    inheritance for code reviews now this
  • 00:05:55
    seems fine and dandy right what's the
  • 00:05:56
    problem the problem is of course that
  • 00:05:58
    has oftentimes when we talk about design
  • 00:05:59
    patterns it's change its that chain when
  • 00:06:02
    requirements change when our system will
  • 00:06:04
    have to change over time our current
  • 00:06:06
    design may not necessarily be
  • 00:06:08
    appropriate for the income
  • 00:06:10
    requirements the example that they take
  • 00:06:11
    is that let's say that we have another
  • 00:06:13
    method which is fly right so ducks fly
  • 00:06:16
    which makes sense because ducks do fly
  • 00:06:18
    thus we've suddenly given city duck the
  • 00:06:21
    ability to fly and we've given wild duck
  • 00:06:23
    the ability to fly this example is of
  • 00:06:25
    course completely fixtures then kind of
  • 00:06:27
    silly but then they're saying that okay
  • 00:06:28
    then some new requirements come in and
  • 00:06:30
    they somebody some programmer forgets
  • 00:06:33
    that they're doing this and we have
  • 00:06:35
    another subclass which is a rubber duck
  • 00:06:37
    sort rubber duck is a fake duck right
  • 00:06:39
    you know these yellow plastic ducks
  • 00:06:41
    rubber plants whatever it's a fake duck
  • 00:06:43
    it's one of these yellow ducks and
  • 00:06:45
    rubber dog has its own display method
  • 00:06:48
    but the argument is that or the problem
  • 00:06:51
    is that they're saying it well rubber
  • 00:06:54
    ducks shouldn't be flying okay so so
  • 00:06:58
    rubber ducks shouldn't be flying so that
  • 00:07:00
    means we can't just blindly inherit that
  • 00:07:03
    behavior so what if we then now I'm sort
  • 00:07:05
    of trading off from where they're going
  • 00:07:07
    but I think we're going to serve the
  • 00:07:08
    same place so what if then we're saying
  • 00:07:10
    that let's implement fly as well here
  • 00:07:12
    let's implement fly here so we have some
  • 00:07:16
    rubber duck has its own behavior and
  • 00:07:18
    that behavior is essentially nothing
  • 00:07:19
    right it's non flying so you could argue
  • 00:07:23
    that not being able to fly is a kind of
  • 00:07:26
    flying behavior we'll get more into that
  • 00:07:28
    and this too actually works fine but you
  • 00:07:31
    can maybe start to get this feeling that
  • 00:07:33
    we're sort of heading down a slippery
  • 00:07:35
    slope so let's say that we have another
  • 00:07:38
    duck I'll just make up silly name so you
  • 00:07:40
    know never mind the name is just thing
  • 00:07:43
    we had lots of ducks so let's say that
  • 00:07:44
    we have a mountain duck right names make
  • 00:07:47
    no sense whatever and the mountain duck
  • 00:07:50
    of course can be displayed but the point
  • 00:07:52
    here is that that this duck also wants
  • 00:07:55
    to have its own flying behavior so we
  • 00:07:58
    override fly here but let's say that
  • 00:08:01
    this flying behavior isn't necessarily
  • 00:08:03
    not being able to fly let's say that it
  • 00:08:05
    is a flying behavior but it's just some
  • 00:08:07
    kind of different flying behavior and
  • 00:08:09
    then let's say
  • 00:08:10
    say that we have another duck so it's a
  • 00:08:12
    cloud duck it's it it's another duck
  • 00:08:15
    that has its own flying behavior but
  • 00:08:18
    what's interesting is that this duck has
  • 00:08:19
    this guy's flying behavior these two
  • 00:08:23
    Ducks have the same flying behavior so
  • 00:08:25
    we here also override and implement the
  • 00:08:28
    fly behavior but because this fly
  • 00:08:31
    behavior is the base class fly behavior
  • 00:08:33
    right that's different from these two
  • 00:08:35
    these two have their own right so the
  • 00:08:39
    code here the copy in this fly game
  • 00:08:41
    actually is the same as this one
  • 00:08:44
    100% duplicated we just go copy paste so
  • 00:08:48
    these have the same and then you could
  • 00:08:50
    start to say okay well I need another
  • 00:08:52
    class I need another flying like flying
  • 00:08:57
    with style a and both of these inherit
  • 00:09:00
    from flying with stylette but hopefully
  • 00:09:03
    you're feeling that now really treading
  • 00:09:06
    down a dangerous path right because yes
  • 00:09:08
    okay that works for fly I'm running out
  • 00:09:11
    of space here but what if we also have
  • 00:09:13
    eat for example right so we have an eat
  • 00:09:17
    method and this duck has a base classes
  • 00:09:21
    eat method but this duck has its own eat
  • 00:09:25
    method and then there's another duck
  • 00:09:28
    let's say the rubber duck has the same
  • 00:09:29
    eat method as the mountain duck you can
  • 00:09:32
    start to feel however sort of it's
  • 00:09:34
    getting kind of crazy so like let's
  • 00:09:36
    let's try and draw this it works as long
  • 00:09:38
    as the behavior is shared downwards so
  • 00:09:41
    this is an inheritance hierarchy right
  • 00:09:43
    as soon as you want to show me havior
  • 00:09:45
    horizontally here there is no way unless
  • 00:09:48
    you get into multiple inheritance but
  • 00:09:50
    like I think Sammy met says it nicely
  • 00:09:52
    when she says that the solution to
  • 00:09:54
    problems with inheritance is not more
  • 00:09:56
    inheritance but that's a topic for
  • 00:09:58
    another discussion but I think this and
  • 00:10:00
    now by analogy kind of makes sense you
  • 00:10:03
    choose of course there are many ways of
  • 00:10:05
    building these hierarchies there are
  • 00:10:08
    many ways of building up this
  • 00:10:10
    inheritance chain but essentially the
  • 00:10:13
    point is that no matter how you do it
  • 00:10:15
    there are some scenarios where you
  • 00:10:18
    can't build it hierarchically because
  • 00:10:20
    you will end up in a situation where
  • 00:10:22
    some behavior here also once we wants to
  • 00:10:26
    be used here
  • 00:10:26
    so you either have to duplicate code or
  • 00:10:29
    find another solution
  • 00:10:30
    by the way Sandi Metz has a really great
  • 00:10:33
    talk on amongst other things the null
  • 00:10:35
    object pattern where she talks a bit
  • 00:10:37
    about this I'll link that in the
  • 00:10:38
    description it's super valuable and that
  • 00:10:40
    gives more insight into why composition
  • 00:10:42
    should often be favored over inheritance
  • 00:10:44
    but let's move on I think you're seeing
  • 00:10:46
    the problem so let me get rid of this
  • 00:10:47
    stuff and let's just directly jump into
  • 00:10:50
    the solution never mind all of those
  • 00:10:52
    intermediate steps so what I'll do is
  • 00:10:54
    that I'll remove everything and just
  • 00:10:56
    keep the wild duck and a city duck but
  • 00:10:59
    please generalize in your head and
  • 00:11:00
    remember that there could be more ducks
  • 00:11:02
    so the point with strategy let's think
  • 00:11:04
    about the definition again the strategy
  • 00:11:06
    pattern defines a family of algorithms
  • 00:11:07
    encapsulate each one and makes them
  • 00:11:09
    interchangeable strategy let's the
  • 00:11:11
    algorithm vary independently from the
  • 00:11:12
    clients that use it that's exactly our
  • 00:11:14
    problem here we have an algorithm fork
  • 00:11:17
    working we have an algorithm for flying
  • 00:11:19
    and what we're realizing is that we
  • 00:11:21
    can't create a hierarchical solution in
  • 00:11:23
    order to share code between these
  • 00:11:26
    different uses of the different
  • 00:11:28
    algorithms so we have to extract the
  • 00:11:29
    algorithms and say that these are
  • 00:11:31
    clients right a duck is a client a city
  • 00:11:34
    wild duck is a client a sticky duck is a
  • 00:11:36
    client
  • 00:11:37
    was that air duck cloud dark cloud duck
  • 00:11:40
    is a client and they make use of
  • 00:11:43
    different algorithms for flying and fork
  • 00:11:46
    walking and these must be able to vary
  • 00:11:50
    independently from from other aspects of
  • 00:11:52
    the clients so what do we do well we say
  • 00:11:55
    that ok or create strategies for
  • 00:11:58
    quacking and strategies for flying so
  • 00:12:01
    we'll create an interface i kwok
  • 00:12:03
    behavior and what it does is that it
  • 00:12:05
    says you need to have a kwok method and
  • 00:12:08
    there are many ways of solving this that
  • 00:12:11
    would still adhere to the strategy
  • 00:12:12
    pattern but this is one way of
  • 00:12:14
    approaching it and then we do the same
  • 00:12:16
    for flying
  • 00:12:17
    so I fly behavior the interface are
  • 00:12:20
    flying i kwok behavior the interface for
  • 00:12:22
    quacking sorry we need to have
  • 00:12:24
    my method yourself and you'll probably
  • 00:12:27
    see later that we could probably
  • 00:12:28
    generalize so I'm not going to do that
  • 00:12:30
    in this video but you could probably
  • 00:12:31
    generalize as well as that instead of
  • 00:12:33
    making these two separate interfaces you
  • 00:12:35
    could probably make them to say one same
  • 00:12:37
    one so you could have maybe a duck
  • 00:12:39
    behavior and this would be something
  • 00:12:41
    more generic such as executes but just
  • 00:12:44
    bear that in mind as we go forward so
  • 00:12:45
    notice that we're making use of has a
  • 00:12:47
    rather than isn't so that means that
  • 00:12:49
    every duck must have a quack behavior
  • 00:12:53
    and every duck must have a fly behavior
  • 00:12:57
    what are them flag behaviors ok let's
  • 00:12:59
    start to think about them as algorithms
  • 00:13:01
    previously we said that wild ducks and
  • 00:13:04
    sticky ducks they have the same quoc
  • 00:13:07
    behavior and they have the same fly
  • 00:13:09
    behavior so these are the same here in
  • 00:13:12
    terms of flying and in terms of quacking
  • 00:13:15
    and previously we achieve that through
  • 00:13:17
    inheritance but now we're saying okay if
  • 00:13:21
    this is an interface an interface for
  • 00:13:23
    quacking and every duck has a quat
  • 00:13:27
    behavior has a concrete quoc behavior at
  • 00:13:30
    remember this is an interface so it's
  • 00:13:33
    not instantiate able so something needs
  • 00:13:36
    to implement again this is inherits or
  • 00:13:39
    implements in terms of an interface some
  • 00:13:41
    kind of quoc behavior and if these
  • 00:13:44
    previously were in the base class let's
  • 00:13:46
    call them something like simple or
  • 00:13:47
    default all right so that's a simple
  • 00:13:49
    simple walk I should probably put simple
  • 00:13:52
    quack behavior to increase semantics and
  • 00:13:53
    make it easier for other developers and
  • 00:13:55
    to indicate to them that we're making
  • 00:13:57
    use of something like strategy pattern
  • 00:13:59
    we want to be super clear look at the
  • 00:14:01
    name is something like simple quacks
  • 00:14:02
    strategy and this would be quark
  • 00:14:04
    strategy for example these are naming
  • 00:14:07
    discussions but never underestimate the
  • 00:14:09
    power of naming your things in a way
  • 00:14:12
    that makes it understandable for your
  • 00:14:14
    code developers never underestimate so
  • 00:14:17
    let's say that we have a simple quote
  • 00:14:18
    behavior that that implements the
  • 00:14:20
    interface I I quote behavior and then
  • 00:14:22
    let's say that this quantum ESSID
  • 00:14:24
    previously had an implementation we had
  • 00:14:26
    implementation of quark in here let's
  • 00:14:28
    now say that instead one of the
  • 00:14:31
    method delegates to the quark behavior
  • 00:14:34
    that this duck the instance of the duck
  • 00:14:36
    happens to have so if this particular
  • 00:14:39
    duck happens to have a simple kwok
  • 00:14:41
    behavior then when we call Kwok we will
  • 00:14:44
    run whatever is in here sorry of course
  • 00:14:46
    this should be walk so we run the
  • 00:14:48
    contents of this method if we've
  • 00:14:51
    composed an instance if we create an
  • 00:14:53
    instance of a duck that has the simple
  • 00:14:55
    kwok behavior this is possible because
  • 00:14:58
    this Kwok man this duck has an IKE walk
  • 00:15:02
    behavior or I mean we could trivially we
  • 00:15:05
    could of course do this it has a simple
  • 00:15:07
    Kwok behavior but that's this is
  • 00:15:09
    significantly less flexible this creates
  • 00:15:12
    class explosions so what we're doing is
  • 00:15:15
    that we're saying that all we need to
  • 00:15:17
    know is that it has something which is
  • 00:15:18
    quotable why because remember we have
  • 00:15:22
    the rubber duck the rubber duck had no
  • 00:15:24
    quacking behavior so then we simply
  • 00:15:27
    implement another concrete class a no
  • 00:15:29
    Kwok behavior that also of course
  • 00:15:32
    implements the interface I quark
  • 00:15:34
    behavior so we have now no Kwok
  • 00:15:37
    behaviors and we have simple Kwok
  • 00:15:39
    behaviors and suddenly we can take these
  • 00:15:41
    two different algorithms these two
  • 00:15:43
    different strategies these two in this
  • 00:15:45
    case different behaviors and put them in
  • 00:15:48
    places and use them to compose new
  • 00:15:51
    things that behave differently depending
  • 00:15:53
    on which one they have so a duck can now
  • 00:15:56
    we can now say that the duck class it
  • 00:15:58
    doesn't need to care about quacking its
  • 00:16:00
    subclasses that doesn't need to don't
  • 00:16:02
    need to care about quacking the duck
  • 00:16:04
    class only needs to ensure that whenever
  • 00:16:07
    it is an instance it has a Kwok behavior
  • 00:16:10
    if it happens to have an O Kwok behavior
  • 00:16:13
    it won't go on if it happens to have a
  • 00:16:15
    simple walk behavior it will quacks
  • 00:16:17
    simply whatever that simple quacking
  • 00:16:20
    means and then we do exactly the same
  • 00:16:22
    thing for fly behavior so we say let's
  • 00:16:26
    let's use the same terminology let's say
  • 00:16:27
    that we have simple fly behavior here as
  • 00:16:29
    well so we have simple flying as a
  • 00:16:32
    method fly and implements the interface
  • 00:16:35
    flag behavior so those two other ducks
  • 00:16:38
    that we talked about before the
  • 00:16:39
    Mountain duck and the was that the cloud
  • 00:16:43
    the duck silly name sorry the mounted
  • 00:16:45
    like the clown duck these two if I
  • 00:16:48
    remember correctly wasn't it so that we
  • 00:16:50
    said they share the same flying behavior
  • 00:16:53
    but the flying behavior that they share
  • 00:16:56
    that flying behavior that both of them
  • 00:16:58
    share is not the same as the flying
  • 00:17:00
    behavior that we previously had in the
  • 00:17:01
    base class which is now the simple
  • 00:17:04
    flying behavior here because we've moved
  • 00:17:06
    we've done exactly the same thing with
  • 00:17:08
    fly that we did with kwok we said that
  • 00:17:10
    okay actually ducks any duck must have a
  • 00:17:14
    concrete fly behavior it has anything
  • 00:17:17
    that is that implements I fly behavior
  • 00:17:20
    right it must have a concrete fly
  • 00:17:22
    behavior but instead of saying that we
  • 00:17:24
    it has a concrete flying behavior any
  • 00:17:26
    particular one we're saying anything
  • 00:17:29
    that has that implements the interface
  • 00:17:31
    fly behavior will suffice
  • 00:17:33
    just as we move the logic here away from
  • 00:17:37
    the quack behavior away from the kwok
  • 00:17:39
    method into concrete quark behaviors
  • 00:17:42
    here and here right in the same way and
  • 00:17:45
    we did that have we taken a fly behavior
  • 00:17:48
    we've taken a fly behavior from here and
  • 00:17:50
    moved it into here which now means that
  • 00:17:54
    in our previous example where we had the
  • 00:17:57
    mountain duck and the cloud duck that
  • 00:17:59
    both shared a fly behavior that was
  • 00:18:01
    different from this particular fly
  • 00:18:04
    behavior we can now trivially solve that
  • 00:18:06
    problem by introducing another fly
  • 00:18:09
    behavior that is I don't know what their
  • 00:18:12
    fly behavior would be called but let's
  • 00:18:13
    jet fly behavior right so so jet fly
  • 00:18:18
    behavior ah again semantics link that's
  • 00:18:21
    at least named them according to the
  • 00:18:22
    same convention so jet flying I have
  • 00:18:25
    some engine supported flying whatever
  • 00:18:27
    and it has the fly method so again the
  • 00:18:30
    names are silly but I think you can see
  • 00:18:32
    sort of how we're stringing together the
  • 00:18:33
    pieces so let's let's draw the same
  • 00:18:35
    thing again
  • 00:18:35
    so here the code that's in here is not
  • 00:18:39
    what was previously in here but what was
  • 00:18:41
    previously in when we had let's let's
  • 00:18:45
    reintroduce let's say that we had the
  • 00:18:46
    cloud duck we have the cloud duck before
  • 00:18:50
    so
  • 00:18:51
    there we had a fly method there we had a
  • 00:18:54
    fly method and it had some logic and
  • 00:18:56
    we've taken that logic and we've moved
  • 00:18:58
    it here into the jet flying behavior and
  • 00:19:02
    into the fly method of a jet flying
  • 00:19:05
    class and sorry I forgot to draw this
  • 00:19:07
    arrow we should of course had a jet
  • 00:19:08
    flying implements fly behavior so simple
  • 00:19:12
    flying implements fly behavior and jet
  • 00:19:15
    flying implements fly behavior and a
  • 00:19:18
    duck has a fly behavior which means that
  • 00:19:21
    a duck can have jet flying behavior or
  • 00:19:23
    it can have simple flying behavior if
  • 00:19:26
    you start to think about this I've
  • 00:19:27
    talked about this is in a previous video
  • 00:19:29
    I really think that this is one of those
  • 00:19:31
    four critical moments where you start to
  • 00:19:33
    realize that we need inheritance much
  • 00:19:35
    much less than some people make us
  • 00:19:38
    believe it's I think very common to sort
  • 00:19:40
    of go through schooling or or go through
  • 00:19:43
    a common object-oriented examples and
  • 00:19:45
    then think that inheritance is very very
  • 00:19:47
    powerful and that it applies to very
  • 00:19:49
    many scenarios but actually in this case
  • 00:19:51
    where we have a very flexible system
  • 00:19:53
    looking at these parts and we suddenly
  • 00:19:55
    realize that we don't actually need
  • 00:19:57
    these middle parts assuming of course
  • 00:20:00
    that displaying would be the same in
  • 00:20:03
    this in this in this example that they
  • 00:20:05
    have I actually think the display the
  • 00:20:07
    point is the display method is different
  • 00:20:09
    so what we could do is of course we can
  • 00:20:12
    do the same thing for display we could
  • 00:20:14
    inject the display behavior or we could
  • 00:20:16
    we could extract the display behavior to
  • 00:20:19
    an interface and then saying that a duck
  • 00:20:22
    has a display behavior let's actually do
  • 00:20:24
    it just for the sake of X before the air
  • 00:20:26
    for the sake of the example but let me
  • 00:20:27
    just first remove this so we want to get
  • 00:20:29
    rid of this display behavior and this
  • 00:20:31
    display behavior so we do exactly the
  • 00:20:33
    same thing I don't even know what this
  • 00:20:34
    is what they do in the book but I mean
  • 00:20:35
    it makes perfect sense and it's that
  • 00:20:36
    strategy pattern so just hang high
  • 00:20:38
    display actually I just realized that I
  • 00:20:42
    was super inconsistent with the naming I
  • 00:20:44
    mean the interface is our neighbor
  • 00:20:46
    mmm-hmm behavior but on this side we've
  • 00:20:50
    got simple quark and a simple quacking
  • 00:20:53
    and here we've got simple flying and not
  • 00:20:57
    simple fly so that was totally
  • 00:20:59
    inconsistent
  • 00:20:59
    apologies in order to save your time I
  • 00:21:02
    won't change that
  • 00:21:03
    but I would never check in I would never
  • 00:21:05
    commit source code like this I would
  • 00:21:06
    absolutely change it so that the naming
  • 00:21:08
    is consistent to another developer this
  • 00:21:10
    is super confusing right it would
  • 00:21:12
    indicate if I would read code that was
  • 00:21:15
    written like this I would it would
  • 00:21:16
    indicate to me that this is a completely
  • 00:21:19
    different concept that this concept
  • 00:21:21
    because the grammar is different but the
  • 00:21:23
    words are suffix differently so I would
  • 00:21:25
    think that they are they're different
  • 00:21:26
    concepts but yeah in order to save our
  • 00:21:28
    time let's just now you know let's move
  • 00:21:30
    on so let's say that we have an I
  • 00:21:32
    display I display behavior so I display
  • 00:21:36
    behavior same thing we have a display
  • 00:21:38
    method display and now we can then say
  • 00:21:41
    that okay if the city ducks display
  • 00:21:43
    strategy is different from the display
  • 00:21:46
    strategy of the wild duck then we at
  • 00:21:48
    least need two concrete classes so now
  • 00:21:51
    since we don't know what what these
  • 00:21:53
    would be right I mean if if we I would
  • 00:21:55
    if we would now end up in a scenario
  • 00:21:56
    where we would say that okay I want the
  • 00:21:58
    most sensible thing I can think of to
  • 00:22:01
    name the display behavior would be city
  • 00:22:03
    duck display and while the duck display
  • 00:22:06
    then I mean then there's probably zero
  • 00:22:09
    need for strategy pattern because as the
  • 00:22:12
    name implies there is zero reuse in that
  • 00:22:15
    code there is there's no intention of
  • 00:22:18
    reusing that is display behavior for
  • 00:22:20
    city ducks because we've renamed its
  • 00:22:22
    safety duck display behavior but for the
  • 00:22:25
    sake of the example and just to sort of
  • 00:22:26
    show you this in absurdum let's say that
  • 00:22:28
    we are intending some reuse of the
  • 00:22:30
    display behavior so let's say that this
  • 00:22:31
    would be I mean I am very hard time
  • 00:22:34
    coming up with examples for well what
  • 00:22:36
    the different display behaviors would be
  • 00:22:37
    but let's just do something silly so
  • 00:22:39
    let's say that this would be display
  • 00:22:42
    graphically that's one display behavior
  • 00:22:45
    and the other display behavior as you
  • 00:22:47
    might guess is display just say as a
  • 00:22:50
    string or as text as tanks so we have
  • 00:22:53
    this place text behavior and we have
  • 00:22:55
    displayed graphically behavior behind
  • 00:22:57
    again naming I'm so sloppy so maybe this
  • 00:23:00
    should be display as graphics and
  • 00:23:02
    displays text and both these of course
  • 00:23:04
    implement the eye display
  • 00:23:07
    so a display display as graphics is an I
  • 00:23:11
    display behavior and display as text is
  • 00:23:14
    an I display behavior or rather they
  • 00:23:17
    both implement I display behavior what
  • 00:23:19
    does this mean
  • 00:23:20
    okay if you start to now look at what we
  • 00:23:22
    have that means that our specific
  • 00:23:24
    overriding are are are the thing that
  • 00:23:27
    was previously city ducks were
  • 00:23:29
    responsible for displaying they were
  • 00:23:31
    responsible for that's the only logic
  • 00:23:33
    they're adding they are by definition
  • 00:23:35
    because of inheritance they are ducks so
  • 00:23:38
    they get all all the stuff that ducks
  • 00:23:40
    have but they were saying that actually
  • 00:23:42
    city ducks have special displaying
  • 00:23:44
    behavior and that displaying behavior we
  • 00:23:47
    put here but let's say that that
  • 00:23:49
    displaying behavior most displays
  • 00:23:51
    graphics and I'm trying to see the
  • 00:23:52
    digger example makes no sense but I mean
  • 00:23:54
    bear with me so assume that what does
  • 00:23:56
    what was special about that once that we
  • 00:23:58
    wanted to display its graphics if we now
  • 00:24:01
    have the I display behavior and we say
  • 00:24:04
    that the duck has an I display behavior
  • 00:24:07
    so that's that's this is getting messy
  • 00:24:09
    let's just reiterate the duck has a
  • 00:24:11
    quack behavior a doc hasn't display
  • 00:24:14
    behavior a duck has a flying behavior
  • 00:24:17
    but that's on abstract level that's on a
  • 00:24:19
    class level and then when you create
  • 00:24:20
    instances you need to make sure that
  • 00:24:22
    that duck has something that implements
  • 00:24:24
    these three different interfaces so that
  • 00:24:27
    could be a duck that has a simple quark
  • 00:24:29
    behavior and a jet flying behavior or it
  • 00:24:32
    could be a duck that has an O quark
  • 00:24:34
    behavior and a jet flying behavior so if
  • 00:24:36
    it had a simple flying behavior and it
  • 00:24:39
    had no quack behavior I was going to say
  • 00:24:41
    that that would have been the the rubber
  • 00:24:43
    duck but maybe it wasn't bad maybe it
  • 00:24:45
    was that the rubber duck couldn't fly
  • 00:24:46
    but could quacks oh I've messed that up
  • 00:24:48
    but I think you see what I'm saying by
  • 00:24:50
    creating these classes you can create
  • 00:24:52
    another one no flying behavior fly does
  • 00:24:55
    nothing this one also implements the fly
  • 00:24:58
    behavior which means that we can now
  • 00:24:59
    have no flying behavior which means that
  • 00:25:01
    we gotta duck there's no fly we a turn
  • 00:25:03
    no quark behavior that would definitely
  • 00:25:04
    be a rubber duck but back to the display
  • 00:25:07
    behaviors so if the doc now also has
  • 00:25:10
    display behaviors we have the display
  • 00:25:12
    method here
  • 00:25:13
    suddenly now the display method makes
  • 00:25:15
    use of a display behavior so when we
  • 00:25:19
    call the display method we invoke the
  • 00:25:21
    display method in whichever display
  • 00:25:24
    method which are a concrete display
  • 00:25:26
    meant that we happen to have and we can
  • 00:25:28
    do that because we know that we have
  • 00:25:30
    something that is and I display behavior
  • 00:25:32
    let me just as parenthesis also say that
  • 00:25:34
    it seems like with this example that I'm
  • 00:25:37
    somehow arguing that interfaces would be
  • 00:25:39
    more appropriate than the subclass this
  • 00:25:42
    whole thing with the behaviors could of
  • 00:25:44
    course have be done using sub classing
  • 00:25:46
    we could have said that I quad behavior
  • 00:25:48
    is a class and so you can give ducks the
  • 00:25:51
    kwok behavior but you can also give it
  • 00:25:53
    something that inherits from the quark
  • 00:25:54
    behavior entirely possible and but that
  • 00:25:56
    mainly depends on your scenario and
  • 00:25:57
    again as soon as you get into
  • 00:25:59
    inheritance you you can have this
  • 00:26:01
    problem that we started with so it's
  • 00:26:03
    kind of interesting but I mean the
  • 00:26:04
    problem that we started with could be
  • 00:26:06
    exhibited within a single behavior
  • 00:26:09
    hierarchy so you start to introduce
  • 00:26:11
    quark behaviors and then you experience
  • 00:26:13
    the same problem that we had in the
  • 00:26:15
    beginning the same problem of wanting to
  • 00:26:16
    reuse code horizontally within the
  • 00:26:18
    behaviors so then maybe you would have
  • 00:26:20
    strategies that make use of strategies
  • 00:26:22
    so you know you can dig far into this
  • 00:26:24
    but everything of course entirely
  • 00:26:25
    depends on the requirements in your
  • 00:26:28
    scenario that's more let's finish up
  • 00:26:30
    this thing with the display behaviors
  • 00:26:31
    where I wanted to go regarding
  • 00:26:33
    introducing display behavior as well as
  • 00:26:35
    a strategy right again remember these
  • 00:26:37
    are strategies we call them behaviors
  • 00:26:40
    but they are strategies in the strategy
  • 00:26:41
    pattern sense for the there are abstract
  • 00:26:44
    strategies whereas these are concrete
  • 00:26:48
    these are concrete strategies now that
  • 00:26:51
    we have these we realize that even
  • 00:26:53
    displaying in these ducts is completely
  • 00:26:56
    unnecessary there's no need to override
  • 00:26:59
    display in these subclasses because we
  • 00:27:01
    can simply give a duck a particular
  • 00:27:04
    display behavior and thus compose a duck
  • 00:27:07
    that behaves as a city duck so actually
  • 00:27:09
    we remove these now and suddenly there's
  • 00:27:12
    no need for any sunglasses we have cloud
  • 00:27:16
    ducks and mouse
  • 00:27:17
    ducks and the city ducks and wild ducks
  • 00:27:20
    and rubber duck and all these ducks
  • 00:27:22
    right now suddenly these are no longer
  • 00:27:25
    named classes in the system they are a
  • 00:27:27
    particular configuration of instances of
  • 00:27:30
    behaviors so given a combination of
  • 00:27:33
    different strategies given a combination
  • 00:27:35
    of different algorithms we happen to
  • 00:27:37
    call some of these combinations
  • 00:27:39
    different things so if it flies in a
  • 00:27:41
    particular when it quacks in a
  • 00:27:42
    particular way and it's displayed in a
  • 00:27:43
    particular way that we call that a wild
  • 00:27:45
    duck if it flies in the same way and the
  • 00:27:48
    quarks in a different way we call it
  • 00:27:50
    some other documents so forth and so
  • 00:27:51
    forth what I haven't talked about I sort
  • 00:27:53
    of glanced over this is dependency
  • 00:27:55
    injection right that is only possible if
  • 00:27:58
    these behaviors are somehow injected
  • 00:28:01
    into an instance of a duck and not
  • 00:28:04
    hard-coded in the class if if the class
  • 00:28:07
    actually hard holds these things then we
  • 00:28:11
    can't do that anymore so let's just very
  • 00:28:13
    very quickly look at what that would
  • 00:28:15
    look like let me remove all of this
  • 00:28:17
    stuff and we'll look at some cold very
  • 00:28:19
    very very quickly
  • 00:28:20
    okay sort of pseudo coding yeah a class
  • 00:28:23
    it's called a duck as we sent this duck
  • 00:28:25
    has fly behavior quat behavior and
  • 00:28:28
    display behavior so let's let's make
  • 00:28:30
    these members by the way I put eyes in
  • 00:28:33
    the beginning just to denote that their
  • 00:28:34
    interface is by convention it has no
  • 00:28:36
    other meaning so we put these as
  • 00:28:38
    properties in the class and I could of
  • 00:28:40
    course say that this member equals a new
  • 00:28:44
    some particular fly behavior but if I
  • 00:28:47
    did that it wouldn't be as flexible we
  • 00:28:50
    need to somehow inject the behavior
  • 00:28:52
    right this would hard-code the
  • 00:28:55
    dependency and put me back in a state
  • 00:28:57
    where i need multiple classes to
  • 00:28:59
    represent all of these different types
  • 00:29:01
    of ducks and it's just think about it
  • 00:29:04
    it's significantly less impossible so
  • 00:29:07
    instead let me just say that i have that
  • 00:29:09
    member and the key here is if we use
  • 00:29:11
    constructor injection we look at the
  • 00:29:13
    constructor so we say we
  • 00:29:14
    constructor public duck and it takes
  • 00:29:17
    some arguments the question is what
  • 00:29:18
    arguments well we need a flying behavior
  • 00:29:21
    we need a quant behavior and we need a
  • 00:29:22
    display behavior so let's just take
  • 00:29:24
    those so well take something off time
  • 00:29:27
    fly behavior and let me just call it FB
  • 00:29:31
    to save space and we'll take a quack
  • 00:29:35
    behavior and again just QB to save space
  • 00:29:38
    and a display behavior calling it DB and
  • 00:29:44
    that's it that's the constructor the
  • 00:29:46
    signature of the constructor so let's
  • 00:29:49
    open this up and we'll close it here and
  • 00:29:51
    what are the contents of the constructor
  • 00:29:53
    right in the constructor we set these
  • 00:29:55
    all those extremely silly I forgot the
  • 00:29:58
    variable names here I'm sorry so just to
  • 00:30:00
    save space again that's the same thing
  • 00:30:01
    that's say that the flying behavior is
  • 00:30:03
    called FB the coop behavior is called QB
  • 00:30:05
    and the display behavior is called DB
  • 00:30:08
    again please bear in mind never
  • 00:30:11
    underestimate the power of semantics and
  • 00:30:15
    underestimate the power of good naming
  • 00:30:17
    so I'm just doing this here because I
  • 00:30:19
    managed it in space in your program make
  • 00:30:21
    use of proper variable names
  • 00:30:23
    okay so then end the constructor we just
  • 00:30:25
    take these arguments that we've been
  • 00:30:26
    passed in and set these other variables
  • 00:30:29
    we take the variables we get passed in
  • 00:30:31
    through the constructor and set the
  • 00:30:33
    values of them to our instance variables
  • 00:30:36
    so we just say this not f b this.f b is
  • 00:30:40
    to be super clear I should do this so
  • 00:30:42
    here we have F b QB for kwok behavior DB
  • 00:30:45
    for display behavior and then here we
  • 00:30:48
    have F B for Kwok for fly behavior QB
  • 00:30:51
    for quad behavior and DB for display
  • 00:30:54
    behavior and then we set these variables
  • 00:30:57
    so this don't FB for flag behavior is
  • 00:31:00
    equal to the fly behavior that
  • 00:31:02
    passing this dog Kwok behavior the
  • 00:31:05
    instance variable quark behavior is the
  • 00:31:07
    kwok behavior that we passed in as an
  • 00:31:09
    argument to the constructor and finally
  • 00:31:11
    the display behavior on an instance
  • 00:31:14
    level is a display behavior that we
  • 00:31:16
    passed in that's it right sorry of
  • 00:31:19
    course that's not it fly behaviors for
  • 00:31:21
    example of course have the fly method so
  • 00:31:24
    when we when we call fly on a class duck
  • 00:31:27
    we need to invoke the fly behavior here
  • 00:31:29
    I don't really have space for that here
  • 00:31:31
    but let's just say that
  • 00:31:32
    let me put bolt here and then you just
  • 00:31:35
    continue that over here wha
  • 00:31:38
    again just continue that over here sorry
  • 00:31:41
    public void fly takes no arguments and
  • 00:31:45
    has some implementation what is that
  • 00:31:47
    implementation what that implementation
  • 00:31:49
    is just to invoke so what we do is we
  • 00:31:52
    say this dot
  • 00:31:54
    FB this fly behavior the instance level
  • 00:31:57
    fly behavior that will have there will
  • 00:32:00
    be a concrete instance I was passed in
  • 00:32:02
    in the constructor this flag behavior
  • 00:32:05
    adult fly because that was the the name
  • 00:32:08
    of the method when we were designing a
  • 00:32:09
    class diagram and then we do exactly the
  • 00:32:12
    same thing so I won't do it here because
  • 00:32:13
    I think you can Cesar the pattern we do
  • 00:32:15
    exactly the same thing for a quat
  • 00:32:16
    behavior and for display behavior so you
  • 00:32:19
    pass in a behavior and then you execute
  • 00:32:22
    that behavior within the class so now
  • 00:32:26
    ducks very independently or how any
  • 00:32:31
    particular concrete quark behaviors of
  • 00:32:34
    fly behaviors actually vary and that's
  • 00:32:37
    the key point before we wrap up let's
  • 00:32:39
    quickly look at the generalized UML
  • 00:32:42
    diagram for strategy pattern let me just
  • 00:32:44
    clean this up so we would simply be
  • 00:32:46
    something like this you have a client in
  • 00:32:48
    our case a duck and that client has some
  • 00:32:53
    eye behavior and that eye behavior can
  • 00:32:57
    be implemented by it's an interface or
  • 00:32:59
    an abstract class or class but it's an
  • 00:33:01
    interface that
  • 00:33:02
    it can be implemented by many concrete
  • 00:33:05
    behaviors so complete behavior a common
  • 00:33:08
    baby and so forth and of course this I
  • 00:33:11
    was kind of sloppy with before but of
  • 00:33:12
    course the client needs to have a
  • 00:33:13
    reference to I behavior so it has an
  • 00:33:17
    instance of an AI behavior I didn't draw
  • 00:33:19
    this in the previous diagrams but I
  • 00:33:21
    think you can figure out how that's
  • 00:33:22
    drawn so that we can say execute for
  • 00:33:25
    execute behavior and then the AI
  • 00:33:27
    behavior has an execute method actually
  • 00:33:30
    there my name is run to emphasize that
  • 00:33:33
    this method is entirely different from
  • 00:33:35
    this so the execute would execute the
  • 00:33:37
    run method of the behavior I execute it
  • 00:33:41
    takes the behavior that we have here and
  • 00:33:43
    runs the run method here and these
  • 00:33:46
    concrete behaviors have specialized run
  • 00:33:49
    methods and that's it that's strategy
  • 00:33:51
    pattern the only thing that made it
  • 00:33:53
    complex before is that we had multiple
  • 00:33:56
    kinds of strategies we have fly
  • 00:33:57
    strategies and quark strategies and
  • 00:33:59
    display strategies but if you only have
  • 00:34:01
    one it's it's it's this simple
  • 00:34:03
    so now if we read this again I'm sure it
  • 00:34:04
    makes a lot of more sense so the
  • 00:34:06
    strategy pattern defines a family of
  • 00:34:08
    algorithms right encapsulate each one
  • 00:34:12
    write it capsule it's the whole
  • 00:34:14
    algorithm here the whole algorithm here
  • 00:34:16
    and makes them interchangeable right
  • 00:34:18
    because they are both eye behaviors I
  • 00:34:20
    should of course have said strategy or
  • 00:34:23
    to make it more sensible in terms of
  • 00:34:24
    strategy pattern but makes them
  • 00:34:26
    interchangeable because since they act
  • 00:34:28
    as the same type whenever you have an ID
  • 00:34:30
    havior you can take any of the concrete
  • 00:34:31
    algorithms so it makes them
  • 00:34:33
    interchangeable and thus strategy lets
  • 00:34:36
    the algorithm vary independently from
  • 00:34:38
    the clients that use it
  • 00:34:39
    so these algorithm this can vary just as
  • 00:34:42
    much as it wants
  • 00:34:42
    this can vary just as much as it wants
  • 00:34:44
    without having to change the code here
  • 00:34:47
    that's the power of strategy pattern I
  • 00:34:51
    highly recommend this book if you want
  • 00:34:53
    to get a good introduction to design
  • 00:34:54
    patterns that's it remember to subscribe
  • 00:34:56
    so that you won't miss the next video on
  • 00:34:59
    the next design pattern from this book
  • 00:35:02
    remember we're going through 13 of them
  • 00:35:05
    thanks for watching I hope that's useful
  • 00:35:08
    and I'll see you in the next one
Etiquetas
  • modèle de stratégie
  • composition
  • héritage
  • design patterns
  • algorithmes
  • canards
  • exemples pratiques
  • programmation orientée objet
  • flexibilité
  • réutilisation du code