Factory Design Pattern - Advanced Python Tutorial #7

Поделиться
HTML-код
  • Опубликовано: 19 июн 2024
  • In this video we talk about the factory design pattern in Python.
    ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
    📚 Programming Books & Merch 📚
    💻 The Algorithm Bible Book: www.neuralnine.com/books/
    🐍 The Python Bible Book: www.neuralnine.com/books/
    👕 Programming Merch: www.neuralnine.com/shop
    💻 Exclusive Content 💻
    👥 Patreon: / neuralnine
    🌐 Social Media & Contact 🌐
    📱 Website: www.neuralnine.com/
    📷 Instagram: / neuralnine
    🐦 Twitter: / neuralnine
    🤵 LinkedIn: / neuralnine
    📁 GitHub: github.com/NeuralNine
    🎵 Outro Music From: www.bensound.com/
  • НаукаНаука

Комментарии • 108

  • @kevinjones238
    @kevinjones238 2 года назад

    The clarity of your explanation/example of the factory pattern is awesome!!!

  • @abebuckingham8198
    @abebuckingham8198 2 года назад +25

    For factories I prefer to use a dictionary with the key strings and the value being the class in the build_person to avoid the cost of comparison as follows
    def build_person(person_type):
    PERSONTYPES = {"Student":Student, "Teacher":Teacher}
    return PERSONTYPES[person_type]().

    • @munteanionut3993
      @munteanionut3993 Год назад

      I think behind the scenes there is still a comparison being made - in basically the way indexing a dictionary (ie: PERSONTYPES['some_key_string']) works. But yeah, doing it like you looks prettier to me, too

    • @breakdancerQ
      @breakdancerQ Год назад

      Hell yeah

    • @PeaceToUsAll
      @PeaceToUsAll 10 месяцев назад +3

      @@munteanionut3993
      no comparison there, it is just the relation between a dictionary (PERSONTYPES) and its key (person_type)
      dictionaries have a complex but time efficient method of fetching values (using hashes), you can read more on that.

    • @srs32896
      @srs32896 7 месяцев назад

      Would this be preferable to using eval(f”{person_type}()”), and it so, why?

    • @martinbel
      @martinbel Месяц назад +1

      great solution! This is better

  • @raymondmichael4987
    @raymondmichael4987 3 года назад +20

    These pattern tutorials are gud, please we need more and eventually creating a project with one

  • @alexd7466
    @alexd7466 Год назад +13

    That is probably how you would do that in Java. But in Python we just use a function that returns the object.
    You can already see that because you made it a staticmethod, and a class with just one function (and even worse a staticmethod) , is a clear indicator that it should be a function, not a class.

    • @abebuckingham8198
      @abebuckingham8198 Месяц назад

      Dictionaries are a kind of function and work really well for dispatching.

  • @_indrid_cold_
    @_indrid_cold_ 2 года назад

    Awesome! Really good examples and explanations. Thank you!

  • @golammuhaimeen2825
    @golammuhaimeen2825 2 года назад

    that was a really nice and simple example man. thank you so much. cleared up my understanding of a factory design pattern

  • @manifaridi9200
    @manifaridi9200 3 года назад +10

    You proved programmers can also have biceps 💪

  • @pythonisthebest358
    @pythonisthebest358 3 года назад +1

    You’re unbelievably cool! Keep up the good work!

  • @ptyspawnbinbash
    @ptyspawnbinbash 3 года назад +2

    Thanks for all the valuable videos, supported you by buying all the seven books. :)

  • @bhaveshvaishnav7388
    @bhaveshvaishnav7388 2 года назад +5

    Explained with such a simple way without complicating anything. Awesome!!

  • @iiStxrii
    @iiStxrii 3 года назад +5

    Hey man, I know you don't have a lot of views yet, but when programming becomes a core subject in schools, you'll be the khan academy of the future. Keep on the grind G

  • @AnIceCrasher
    @AnIceCrasher 3 года назад

    Really well explained! Thank you.

  • @hasibislam4421
    @hasibislam4421 3 года назад +1

    I'm really liking this advanced python tutorials......Can you please make a video describing what meta classes and how they work?

  • @arthur6999
    @arthur6999 3 года назад +22

    day two: async/await tutorial

    • @tips1483
      @tips1483 3 года назад +7

      Yes please I understand the idea but when trying to use it I lose brain cells

    • @tips1483
      @tips1483 3 года назад +1

      @Rudy Rayden mmhm sure I see that you both decided to make your account 3 weeks ago stop.

  • @rsc8295
    @rsc8295 2 года назад

    You could actually add to that to create an automated conversation, like a code people could use to find details of a student or teacher. You would need to add the next prompts and conditionals and include perhaps some code to read in data from file, it would take a lot of time for all the possible responses but be really cool. In fact might do this as practice for my exam. Otherwise cool tutorial!

  • @deeprony7
    @deeprony7 2 года назад +2

    Having the if-else logic still reside in the ConcreteFactory violates the Open-Closed Principle. Would it be better to have an interface for FactoryPerson and implement Student and Teacher from it?
    If so, how would that implementation look like?

  • @julietatallaferro2072
    @julietatallaferro2072 2 года назад

    great teaching :) thank u again!!

  • @travelcinematically
    @travelcinematically 3 года назад

    Explained so beautifully 🤟🤟

  • @dmitriyobidin6049
    @dmitriyobidin6049 2 года назад

    Good tutorial, but missing one part that can be helpful - factory also can be instantiated from IFactory interface, cause in some cases we need different concrete factories, depending on user input.

  • @gauff3r
    @gauff3r 2 года назад

    helpfull, thanks, I'd rethink the use of conditions in factory

  • @jerielmorales1726
    @jerielmorales1726 3 года назад +2

    Great Tutorial. Part 2 of Vim course next =)

  • @saeedebahrami1992
    @saeedebahrami1992 Год назад

    thanks for simple and good example

  • @aniketpurohit8613
    @aniketpurohit8613 3 года назад

    This is a tremendous playlist. Looking for async/await video.

  • @girish6064
    @girish6064 Год назад

    Thanks for sharing!

  • @melokorn
    @melokorn Год назад

    Really useful. Thanks

  • @pratheek5096
    @pratheek5096 2 года назад

    Perfectly explaineddd

  • @garfieldgray6746
    @garfieldgray6746 9 месяцев назад

    Thanks for your brilliant video,
    I've got a question,
    In the last lines of your code, "person" is a class that we've determined dynamically, then must be able to instantiate an object with it right?!
    Like pp=person()
    But I just can't:/
    Do you know why?!
    Thanks 🙏

  • @val8801
    @val8801 3 года назад

    I see your video only for you awesome intro :)

  • @JeffreyMyersII
    @JeffreyMyersII 11 месяцев назад

    nicely explained, but i have a question. isn't creating factories a use case that the ___new___ class method was made for?

  • @danielg.campos9662
    @danielg.campos9662 2 года назад

    Thanks so much for the tutorial, btw, i have got this message in the pyCharm : "abc.abstractstaticmethod' is deprecated since Python 3.3. Use 'staticmethod' with 'abc.abstractmethod' instead"
    so i have to replace it with : "from abc import ABCMeta, abstractmethod" and then in the class : class IPerson(metaclass=ABCMeta): @staticmethod" and it did the work. Any comments on that? thanks again

  • @alperklc7782
    @alperklc7782 2 года назад

    What is the difference between ABC or ABCMETA class? Can we use one instead of the other?
    Thanks for the lesson.

  • @franciscolucarini8761
    @franciscolucarini8761 3 года назад

    Hey bro will you do differences between python2 and python3 regarding bytes format, thx in advance

  • @cahitskttaramal3152
    @cahitskttaramal3152 Год назад

    it was a great and simple tutorial could you also more design patterns if possible?

  • @Liproqq
    @Liproqq 3 года назад

    Super tutorial. Würdest du empfehlen auf's englische keyboard layout zu wechseln?

  • @chongsun7872
    @chongsun7872 6 месяцев назад

    One quick question is that, in the Person object, the person_method is an abstract Static method. In student and teacher object, you can rewrite it as a normal method? Is this allowed in Python?

  • @sarthaksingh7570
    @sarthaksingh7570 7 месяцев назад +1

    Which text editor are you using bro???

  • @jrgomez
    @jrgomez Год назад +1

    Great explanation as usual. I'm defintively a rookie on OOP, but I'm learning a lot. Thanks !

  • @alialavizadeh2775
    @alialavizadeh2775 3 года назад

    so useful tnx

  • @suche_cz
    @suche_cz 2 года назад

    Thank you for video:
    Is possible to mix interface class with factory class ? eg.:
    It works, just if it''s not some bad practise.
    class Person(metaclass=ABCMeta):
    @abstractmethod
    def person_method(self):
    """ Interface method """
    @staticmethod
    def create(person_type):
    person_class = {
    'Student': Student,
    'Teacher': Teacher,
    }
    return person_class[person_type]()

  • @naserghodsian5545
    @naserghodsian5545 2 года назад +1

    In an statically-typed language like Java you make an Interface to have control over the output of builder method in the factory class. Given the fact that in Python the builder method of the factory class can return anything, what is the point of having an abstract class here?!

  • @Pyrografpl
    @Pyrografpl Год назад

    Thank you

  • @dpm-07
    @dpm-07 Год назад +2

    can anyone tell me difference between Factory Design and Abstract Factory Design ?

  • @ahmedossama7738
    @ahmedossama7738 2 года назад

    Great!

  • @TheDigitalSight
    @TheDigitalSight 3 года назад

    Which fonts do you use?

  • @andrejk9523
    @andrejk9523 2 года назад +1

    A combination of __Init_subclass__ and __new__ implimented in the master class would be a better implementation

  • @ddjambel
    @ddjambel 2 года назад

    You need always self in a object, because self, it the object itself, to use all attributes or methods, over "self.".
    Cheers from Germany.

  • @os_.mos_.m9346
    @os_.mos_.m9346 3 года назад

    What is the name of the song you are using in the intro?

  • @ishfaqbabar2355
    @ishfaqbabar2355 2 года назад

    Which Code Editor are you using .

  • @burakhanaksoy
    @burakhanaksoy 3 года назад

    Thanks for the tutorial. A personal comment is instead of making if check inside a single @staticmethod, it'd be better to create multiple static methods. In this way if you want to create another person, say engineer, you wouldn't have to make an if check.

  • @A_Xavien
    @A_Xavien 2 года назад

    In your example it's more like factory method than just factory if `PersonFactory` would have other actual methods.
    Also why use `if else` (in `build_person` method), when you can just create some dict that store name-class relationships (which name corresponds to which class)?

  • @ruslanways
    @ruslanways 2 года назад

    what IDe did you used?

  • @andytheodorko9874
    @andytheodorko9874 3 года назад

    Wouldn't it be a better example if the logic of creation of a Person in a Factory be more complex? (add random, multiple parameters)

  • @julianreichelt1719
    @julianreichelt1719 11 месяцев назад

    nice one

  • @heroicrhythms8302
    @heroicrhythms8302 Год назад

    instead of hardcoding "student" and "Teacher" values, you can use an ENUM

  • @z4ramoth
    @z4ramoth 3 года назад

    more pattern vids please !

  • @Ricocase
    @Ricocase 3 года назад

    Rotate video to see? Video start at 2:10. Where's else for printing error? If input is == "student" print "I'm a student?" What's the main and init?

  • @pegasus-weilsicherheitdiew5278
    @pegasus-weilsicherheitdiew5278 2 года назад

    How can i ensure that an other coder uses the interface to create objects and not the subclass directly? Can i set a class to private for other modules?
    Like private or public methods?

    • @alexd7466
      @alexd7466 Год назад

      no

    • @falklumo
      @falklumo Год назад

      The factory could use a secret to initialize the subclass not available public. Unfortunately, Python as a language is not expressive enough to support all the famous design patterns directly.

  • @ytalodossantosamorim3052
    @ytalodossantosamorim3052 2 года назад

    Are you using the terminal to write a script? If yes, .......how

  • @someonemight
    @someonemight 2 года назад

    What's the benefit of doing this vs just having a utility function?

  • @__3093
    @__3093 3 года назад

    you're getting more viewers

  • @01akbarikrunal24
    @01akbarikrunal24 3 года назад

    ya i learn lost of thanks

  • @wariare12world95
    @wariare12world95 3 года назад +2

    building a person sounds scary!

  • @macieks3604
    @macieks3604 Год назад

    And why is it necessary to use abstract method here? It would also be possible to have one base class and 2 child classes with same method name overriding the method of the parent class, so called polymorphism. There is no need for an abstract class here. I've seen many examples of an abstract class used in that way but I still don't understand why it was used....

  • @klipkon1941
    @klipkon1941 11 месяцев назад

    Why are they static methods?

  • @rafaeel731
    @rafaeel731 Год назад

    Why to create a factory class when you can, for instance, create a function to do the same?

  • @ninjacodertech
    @ninjacodertech 3 года назад +1

    possible tutorial: how to make VPN in python
    pls try to make

  • @armans4306
    @armans4306 9 месяцев назад

    Bro can you give. some vim tutorial with python. I really like your setup.

  • @pawepuciennik6604
    @pawepuciennik6604 3 года назад +1

    I don't understand, what is it abstract class for? The whole factory would work without it.

    • @nopens
      @nopens 3 года назад

      This. I get it you might wanna double check that every subclass of IPerson has its own imlementation of person_method. Maybe its useful when you have too many subclasses to keep track of them and making the base person abstract would raise an error, maing sure you can safely call that method on every child?

  • @a1dgha1
    @a1dgha1 3 года назад

    How come you haven't upgraded your Python to 3.9 yet?

  • @user-sy1sg4ws3y
    @user-sy1sg4ws3y 3 года назад

    This is Simple Factory?

  • @munzutai
    @munzutai 2 года назад

    What I've never understood is what's wrong with doing it without a factory? What are the benefits of using a factory?

    • @falklumo
      @falklumo Год назад

      You want to hide the subtypes. There is normally no need to expose subtypes to client code. Therefore, a factory is required.

  • @nopens
    @nopens 3 года назад +1

    Doesnt work in python 3.8.3 if you pass "meta=" instead of "metaclass=".

  • @andytheodorko9874
    @andytheodorko9874 3 года назад

    I'd use 'born_person' 8:12

  • @sachinpadwalkar8446
    @sachinpadwalkar8446 3 года назад

    Please upload your vim plugin setup 🙏

    • @NeuralNine
      @NeuralNine  3 года назад

      It is already on github

    • @sachinpadwalkar8446
      @sachinpadwalkar8446 3 года назад

      @@NeuralNine can you tell me the names of all the plugin that you use ?

    • @NeuralNine
      @NeuralNine  3 года назад

      @@sachinpadwalkar8446 all the names are in the github file? Why do you want me to list them here in the comment section manually?

    • @sachinpadwalkar8446
      @sachinpadwalkar8446 3 года назад

      @@NeuralNine oh found it thanks

  • @hoangphan2429
    @hoangphan2429 3 месяца назад

    I love your videos, but I think the example code for the abstract class should be improved. The code doesn't represent the implementation of an abstract class. In PersonFactory class, it basically returns the class.

  • @destiny_02
    @destiny_02 3 года назад

    12th

  • @tips1483
    @tips1483 3 года назад +3

    1st

    • @NeuralNine
      @NeuralNine  3 года назад

      yes ^^

    • @tips1483
      @tips1483 3 года назад

      @@NeuralNine funny enough the video took so long to load I originally typed “2nd” but when I realized I was genuinely first I edited it lmao

  • @falklumo
    @falklumo Год назад

    Shouldn't you mention Erich Gamma et al. and their book "Design Patterns"? After all, the patterns are language-independent and this is just Python trying to catch up 25 years later ;)

  • @kaya12892
    @kaya12892 3 года назад +1

    python in minecraft
    +1

  • @undergroundbodega9766
    @undergroundbodega9766 10 месяцев назад

    Gives and example then said “ this is not factory…”. What the point of that. I wish videos were straight forward.

  • @ViralKiller
    @ViralKiller Год назад

    appreciate the tutorial but completely useless and overcomplicates things for no reason...definitely wouldnt be allowed at my company....way too many dependencies and separate classes for such a simple program

    • @falklumo
      @falklumo Год назад

      You haven't seen big projects. Python doesn't do big projects for a reason ...

  • @navalkishorebarthwal5591
    @navalkishorebarthwal5591 Год назад

    don't mark your interface with "I" instead use meaningful name

  • @SergioInToronto
    @SergioInToronto 8 месяцев назад

    Yikes. Bad patterns like crazy.
    Don't return -1, raise an exception! Class PersonFactory is useless - just write the function! Classes which don't save state are effectively just namespaces, but that's what files are for!

  • @vascof614
    @vascof614 3 года назад +2

    1st

    • @tips1483
      @tips1483 3 года назад +1

      Nope beat you