Комментарии:
This is also a good example of strategy pattern.
ОтветитьWould love a video on comparing the Factory and Builder design patterns and when to use one versus the other.
ОтветитьThis won’t help any Python developer as GoF patterns were in so many cases irrelevant for dynamic languages I feel it’s not only not idiomatic but also slows juniors down when learning to code in Python.
ОтветитьAs Arjan mentioned if you have wide variety of combinations then I use a factory controller class which can generate the proper factories. It is not so complicated.
ОтветитьMan you could have used a simpler example to explain this. For example car models or something. I got less in all of the audio visual stuff.
ОтветитьI would like to propose the use of a factory dictionary approach, where the dictionary values store class names instead of actual instances. This can be more efficient, especially when creating instances is resource-intensive. The idea is to define a dictionary of factories like this:
factories = {
'one': FactoryOne,
'two': FactoryTwo
}
Later, when you need to create an instance based on a specific key, you can use the stored class names to instantiate the desired objects. This is done as follows:
return factories[key]()
With this approach, you optimize memory usage by storing class names, and actual instances are only created when needed.
missed opportunity for an ExporterFactory Factory 😀
ОтветитьThanks Arjan. this is so authentic.
ОтветитьOh wow, the noise this introduces..
The decoupling using map is great, but going through the unnecessary step of Factories is too much.
A really neat solution would be mapping high: AVPreset(audio=partial(WavExporter, settings...), video=partial(FancyVideoExporter, settings...))
That way you have the full map in one place, clear, fully customisable, extendable and visible without jumping around classes.
I seriously can't think why you would want a class for each preset, when the preset is a piece of data.
This is like modelling the users in a database as separate classes.
Im a bit confused about terms and concepts so any help is appreciated.
1) Is example from video an implementation of "Abstract factory" pattern?
2) Does such thing as "Factory pattern" exist? Or maybe its not a "pattern", but something else? Because i mostly find "Factory method" and "Abstract Factory" among patterns.
Great video, btw! As always on this channel.
What should I do if I need N objects that have the same methods, but each type of object has a different input args + partially some common properties?
ОтветитьIn this example an extra layer of inheritance was used. It increases the complexity in provided example without a reason :)
Otherwise... nice.
Wait a second.
Use object without being aware of specifics... Sounds like normal python to me, this is not c/c++ where you need to know exactly how much space to allocate beforehand...
Design guide is not working
ОтветитьQuestion: If the concrete classes require different parameters in the constructor, how can you implement this in the factories get_video/audio_exporter() methods?
ОтветитьIn your read_exporter function, you check if export_quality is in a set of possible quality options. Is there any reason why you use a set instead of a list?
ОтветитьMaster quality video on Factory pattern :)
ОтветитьCould we consider read_exporter() to be a FactoryFactory?
ОтветитьAll I am thinking while watching this is how lazy I am, I would of skipped the abc, and typing and used static methods.. also maybe just used eval and f stings to dynamically call the method...
ОтветитьIf the factory class has a registry that takes up low, high or master, does that change it from being a factory to something else?
ОтветитьI am a bit confused.
Usually factories are used to generate instance of similar object no ?
But here we have to create a different factory per class ! (well one factory for two classes, but let's start simple with just a family of video codec)
What is the use of it ? Each time I create a new VideoExporter I have to create it's dedicated factory.
The only improvement I see, is that the switch has been located in a different function, not in the main anymore.
And I still would have to rewrite this switch after adding a new codec. The difference is that we have a switch of factories instead of codec... great.
Shouldn't we use metaclass and each class using the relevant metaclass being automatically referenced with its associated keyword in to the factory when it is created ?
And thus according to this exemple, we would have only two factories, one for video, one for audio, able to return the instance matching the good codec from the relevant keyword, with auto handled switch !
Thank you so much! This helps a lot :D
ОтветитьUnsure if this is mentioned somewhere, but this is the GOF Abstract Factory pattern. Not to be confused with the GOF Factory Method pattern. The Abstract Factory pattern's goal is, as Arjan says, for grouping different class instantiations together. The Factory Method pattern is about extending the functionality of a class.
Ответитьif your object was to demonstrate factory patterns, you lost your viewer in layers of needless and confusing video paradigm.
ОтветитьI never get your guide even after I subscribe. And I never get any replies from you either :(
ОтветитьLOL that Tim Cook really made me laugh.
ОтветитьWhy would you write an ABC for that?
very unnecessary, imho. Could have just used a simple function tht returns a object.
Just providing a real world example here. I just solved an issue that I had where I needed to provide an implementation of an output port based on a given value with the factory pattern. In this case it was a Payment Processing Service and the code quality and maintainability was quite good with the implementation of the factory pattern, which comes super handy since Payment Processing Services are always likely to require updates or new integrations in the future.
Thanks for the video, super useful as always!
this is so redundant... you are calling the objects through the factory class and you still have to know which objects to call when implementing it, so why not call them directly?
ОтветитьDo you want to instantiate all those exporter objects in the low/high/master dictionary? Wouldn't it be better to instantiate only the one that is called?
ОтветитьI give it a like for that smiling apple CEO, cheers, hire me pls
ОтветитьGreat Python content. Thank you very much for content like this. Do you mind a silly question: why the ExporterFactory.get_video_exporter & ExporterFactory.get_audio_exporter don't have the decoractor @abstractmethod like the class VideoExporter?
ОтветитьLooks very similar to the strategy pattern avoiding a bunch of if...elif...etc..
ОтветитьGreat video! You explain the concepts incredibly well.
ОтветитьI really like the idea where you first present the problem and show the solution. Most videos I watched rant about the theory but forgets to tell you the why part.
ОтветитьGreat video. Really helped me understand this pattern
ОтветитьTop notch content, keep it up.
ОтветитьThanks for the video.
Seems you shouldn't create all of available factory instances in the `factories` dict. Just remove brackets from dict (don't _call_ classes) and add call on return to create chosen class only
dude types so fast I checked my playback speed
ОтветитьThat one was great!! Thanks for the help. you provide great content!!
The only video I kinda dont get is the Observer one.. but I guess thats just me
So instead of exporter classes, you have a bunch of exporter factory classes. This can be all avoided. The factory pattern is useful but I don't think that this is a good example of how to use it. You can make a single factory function and you are done. The function gets some parameters and returns the desired class instance -- it is that simple. This is a critical comment but thanks you for the video.
ОтветитьQuestion: Why to use classes at all here? Why not just functions like "export_video(quality, path)",export_audio(quality,path)" and then use then just use them directly in the 'main'? In the above approach, you make an abstract factory, and a class for each of the combinations of qualities factories and then a separate class for each quality settings and an abstract class for them.
Why is it a bad idea to just use a function instead all of this? If you need to group the video export and audio export together, you can just put them in another function. What am I missing here?
Really enjoy the video that talking about the design pattern in python, especially with a understandable example. Look forward to see more video with more difficult and useful pattern in python.
ОтветитьSo in order to avoid 10 lines of code, you wrote 30 lines of code and made the code harder to follow. Great.
Ответить