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/ Наука
The clarity of your explanation/example of the factory pattern is awesome!!!
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]().
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
Hell yeah
@@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.
Would this be preferable to using eval(f”{person_type}()”), and it so, why?
great solution! This is better
These pattern tutorials are gud, please we need more and eventually creating a project with one
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.
Dictionaries are a kind of function and work really well for dispatching.
Awesome! Really good examples and explanations. Thank you!
that was a really nice and simple example man. thank you so much. cleared up my understanding of a factory design pattern
You proved programmers can also have biceps 💪
You’re unbelievably cool! Keep up the good work!
Thanks for all the valuable videos, supported you by buying all the seven books. :)
Explained with such a simple way without complicating anything. Awesome!!
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
Really well explained! Thank you.
I'm really liking this advanced python tutorials......Can you please make a video describing what meta classes and how they work?
day two: async/await tutorial
Yes please I understand the idea but when trying to use it I lose brain cells
@Rudy Rayden mmhm sure I see that you both decided to make your account 3 weeks ago stop.
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!
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?
great teaching :) thank u again!!
Explained so beautifully 🤟🤟
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.
helpfull, thanks, I'd rethink the use of conditions in factory
Great Tutorial. Part 2 of Vim course next =)
thanks for simple and good example
This is a tremendous playlist. Looking for async/await video.
Thanks for sharing!
Really useful. Thanks
Perfectly explaineddd
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 🙏
I see your video only for you awesome intro :)
nicely explained, but i have a question. isn't creating factories a use case that the ___new___ class method was made for?
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
What is the difference between ABC or ABCMETA class? Can we use one instead of the other?
Thanks for the lesson.
Hey bro will you do differences between python2 and python3 regarding bytes format, thx in advance
it was a great and simple tutorial could you also more design patterns if possible?
Super tutorial. Würdest du empfehlen auf's englische keyboard layout zu wechseln?
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?
Which text editor are you using bro???
Great explanation as usual. I'm defintively a rookie on OOP, but I'm learning a lot. Thanks !
so useful tnx
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]()
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?!
Thank you
can anyone tell me difference between Factory Design and Abstract Factory Design ?
Great!
Which fonts do you use?
A combination of __Init_subclass__ and __new__ implimented in the master class would be a better implementation
You need always self in a object, because self, it the object itself, to use all attributes or methods, over "self.".
Cheers from Germany.
What is the name of the song you are using in the intro?
Which Code Editor are you using .
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.
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)?
what IDe did you used?
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)
nice one
instead of hardcoding "student" and "Teacher" values, you can use an ENUM
more pattern vids please !
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?
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?
no
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.
Are you using the terminal to write a script? If yes, .......how
What's the benefit of doing this vs just having a utility function?
you're getting more viewers
ya i learn lost of thanks
building a person sounds scary!
>:D
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....
Why are they static methods?
Why to create a factory class when you can, for instance, create a function to do the same?
possible tutorial: how to make VPN in python
pls try to make
Bro can you give. some vim tutorial with python. I really like your setup.
I don't understand, what is it abstract class for? The whole factory would work without it.
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?
How come you haven't upgraded your Python to 3.9 yet?
This is Simple Factory?
What I've never understood is what's wrong with doing it without a factory? What are the benefits of using a factory?
You want to hide the subtypes. There is normally no need to expose subtypes to client code. Therefore, a factory is required.
Doesnt work in python 3.8.3 if you pass "meta=" instead of "metaclass=".
Thanks man! That helped!
I'd use 'born_person' 8:12
Please upload your vim plugin setup 🙏
It is already on github
@@NeuralNine can you tell me the names of all the plugin that you use ?
@@sachinpadwalkar8446 all the names are in the github file? Why do you want me to list them here in the comment section manually?
@@NeuralNine oh found it thanks
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.
12th
1st
yes ^^
@@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
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 ;)
python in minecraft
+1
Gives and example then said “ this is not factory…”. What the point of that. I wish videos were straight forward.
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
You haven't seen big projects. Python doesn't do big projects for a reason ...
don't mark your interface with "I" instead use meaningful name
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!
1st
Nope beat you