The Factory Pattern in Python // Separate Creation From Use

The Factory Pattern in Python // Separate Creation From Use

ArjanCodes

2 года назад

166,022 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Muzaffer cıkay
Muzaffer cıkay - 12.11.2023 12:47

This is also a good example of strategy pattern.

Ответить
Dustin Lane
Dustin Lane - 11.11.2023 21:56

Would love a video on comparing the Factory and Builder design patterns and when to use one versus the other.

Ответить
Florian Salihovic
Florian Salihovic - 21.10.2023 22:08

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.

Ответить
Zsolt Sendula
Zsolt Sendula - 19.10.2023 22:02

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.

Ответить
Diggee17
Diggee17 - 29.08.2023 22:43

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.

Ответить
Miroslav Asenov
Miroslav Asenov - 28.08.2023 23:03

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.

Ответить
Denis Borchev
Denis Borchev - 29.07.2023 15:25

missed opportunity for an ExporterFactory Factory 😀

Ответить
Vijay Yarramsetty
Vijay Yarramsetty - 24.07.2023 06:14

Thanks Arjan. this is so authentic.

Ответить
blacklistnr1
blacklistnr1 - 14.06.2023 14:01

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.

Ответить
Andrei Kononov
Andrei Kononov - 31.05.2023 11:56

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.

Ответить
Arc Sin
Arc Sin - 26.05.2023 09:10

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?

Ответить
lukocius
lukocius - 19.02.2023 20:37

In this example an extra layer of inheritance was used. It increases the complexity in provided example without a reason :)
Otherwise... nice.

Ответить
lukocius
lukocius - 19.02.2023 19:47

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...

Ответить
Nishith Singhai
Nishith Singhai - 24.01.2023 17:07

Design guide is not working

Ответить
Dan Neagu
Dan Neagu - 18.01.2023 15:00

Question: If the concrete classes require different parameters in the constructor, how can you implement this in the factories get_video/audio_exporter() methods?

Ответить
Matteo Golin
Matteo Golin - 09.01.2023 06:36

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?

Ответить
Paweł Kostecki
Paweł Kostecki - 16.12.2022 11:11

Master quality video on Factory pattern :)

Ответить
Walter Vos
Walter Vos - 15.12.2022 11:44

Could we consider read_exporter() to be a FactoryFactory?

Ответить
axiomvp
axiomvp - 28.11.2022 20:11

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...

Ответить
COD-Newbie
COD-Newbie - 04.10.2022 09:51

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?

Ответить
LittleConan
LittleConan - 27.09.2022 20:25

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 !

Ответить
MomentousCrazynoob
MomentousCrazynoob - 20.09.2022 19:48

Thank you so much! This helps a lot :D

Ответить
Sam Chen
Sam Chen - 03.09.2022 13:32

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.

Ответить
Daniel Balfour
Daniel Balfour - 25.08.2022 20:10

if your object was to demonstrate factory patterns, you lost your viewer in layers of needless and confusing video paradigm.

Ответить
Mehmood Yousuf
Mehmood Yousuf - 25.08.2022 07:58

I never get your guide even after I subscribe. And I never get any replies from you either :(

Ответить
Neel Suthar
Neel Suthar - 19.08.2022 19:59

LOL that Tim Cook really made me laugh.

Ответить
Alex D
Alex D - 29.07.2022 21:40

Why would you write an ABC for that?
very unnecessary, imho. Could have just used a simple function tht returns a object.

Ответить
Juan Diego Molina
Juan Diego Molina - 14.07.2022 08:04

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!

Ответить
Jan Kučera
Jan Kučera - 06.07.2022 17:36

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?

Ответить
Michael
Michael - 24.06.2022 08:18

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?

Ответить
SockMyCook
SockMyCook - 03.05.2022 19:35

I give it a like for that smiling apple CEO, cheers, hire me pls

Ответить
Tung Pham
Tung Pham - 01.05.2022 09:38

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?

Ответить
Dyoser
Dyoser - 15.04.2022 01:46

Looks very similar to the strategy pattern avoiding a bunch of if...elif...etc..

Ответить
Jarquafelmu
Jarquafelmu - 17.03.2022 07:07

Great video! You explain the concepts incredibly well.

Ответить
Lagu Stephen
Lagu Stephen - 04.03.2022 22:32

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.

Ответить
stuart woodings
stuart woodings - 25.02.2022 00:49

Great video. Really helped me understand this pattern

Ответить
Vladimir the Aesthete
Vladimir the Aesthete - 15.02.2022 13:50

Top notch content, keep it up.

Ответить
Oleg A.
Oleg A. - 19.12.2021 20:25

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

Ответить
doulos
doulos - 15.12.2021 06:54

dude types so fast I checked my playback speed

Ответить
Bokistotel
Bokistotel - 07.12.2021 21:55

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

Ответить
David Landa
David Landa - 28.11.2021 18:05

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.

Ответить
TheWolverine1984
TheWolverine1984 - 28.11.2021 16:43

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?

Ответить
Chris Xiao
Chris Xiao - 25.11.2021 18:55

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.

Ответить
Paul Campbell
Paul Campbell - 24.11.2021 20:18

So in order to avoid 10 lines of code, you wrote 30 lines of code and made the code harder to follow. Great.

Ответить