The Factory Pattern in Python // Separate Creation From Use

preview_player
Показать описание

In this video, I explain what the Factory pattern (or: Abstract Factory) is, and how to use it in Python. One of the greatest things about this pattern is that it allows you to separate creation from use. It's one of my most-used patterns.

🔖 Chapters:
0:00 Intro
0:37 Explaining the example
3:25 Analysis of the code
4:46 What is the Factory pattern?
5:31 Creating the abstract ExporterFactory
7:06 Creating the concrete factories
8:47 Revising the main() function
10:51 Running the new version of the code
11:43 Analysis of the new version
12:11 Bonus: inject the factory as a dependency
13:28 When to (not) use the factory pattern

#arjancodes #softwaredesign #python
Рекомендации по теме
Комментарии
Автор

For me as a data scientist your videos really stepped up my software development game. The Factor Pattern together with having abstract classes as interfaces is something I immediately applied in my current project. This is not something you typically learn in data science courses, even though I think this is crucial when willing to write code in a professional context. Thanks Arjan for sharing those valuable insights!

johnstanley
Автор

I'm binging your videos currently and (among other things) there's something I love: the examples are meaningful. They are practical, solve actual problems. Not your another off the mill tutorial with generic class C inheriting class B etc. Seeing that your example applications mimic actually doing something, makes it 100x easier to wrap the application logic and focus on the topic at hand. I'm amazed.

fazzah
Автор

I just found out about you today while learning certain design patterns. Now, I started reading about factory patterns just a few hours ago and boom, you're here with the video. Thanks for the video. Maybe adapter pattern in the next video?

uzumaki
Автор

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.

VCR
Автор

I am starting a new fulltime developer position in august and im trying to brush up my basics. your channel is amazing

GuRuGeorge
Автор

I'm addicted to your channel man!
Congrats. Cheers from Brazil.

JonMartins
Автор

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.

ytlagu
Автор

Thank you your videos, I understood the design patterns. I implemented it in my current project initially I thought it was lot of work but I persisted and wrote classes for them. I had a requirement to change the data source from DB to file. Some I had implemented it as classes, it was easier me for replacing the spice without impacting the code ! It was a life and a time saver. Thank you !
Please keep sharing such quality content. I’m sure it must be helpful for a lot of folks!

munotpratik
Автор

I so enjoy your presentation and delivery. Great job in explaining "why" things are used and not just the typical "how" (which is what geeks who already grokk something tend to do).

I also like how you discuss what things a pattern doesn't do well and then you provide an alternative for such cases. ⭐⭐⭐

cabanford
Автор

I have been watching all your videos over the last week. I am glad I have reached a point where as soon as you started explaining the factory pattern for decoupling the main(), I thought that this would eventually lead back to dependency injection.

lazyoracle
Автор

My most frequently used pattern before your videos. Thank you!

UrbanAI
Автор

Master quality video on Factory pattern :)

pawekostecki
Автор

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!

judegomolina
Автор

Your code examples and the way how you explain them is top.

robertbrummayer
Автор

Thank you for what you are doing. It is really helpful. Keep going.

persfarhat
Автор

this is the best python channel hands down, anyone can suggest other channels like this?

mhdimhdi
Автор

Awesome videos man, really helps me understand design patterns that up till now sounded a bit vague and abstract to me. Bedankt!

JelleWolbers
Автор

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.

AsenovMiroslav
Автор

Amazing video! One SUPER small item I saw in read_explorer() is that export_quality is using in to check against the available factories. Using: if factories.get(export_quality) will not iterate through the dictionary but directly see if that key is available. Considering how few factories are available, this would never get noticed. I recently found your channel and binged a few videos already :) Subscribed!! Can't wait for more.

robertmccarthy
Автор

I really enjoyed the video. Your pacing and amount of content is very good as a reference!

In a future video, could you clarify the differences between a Factory Pattern and a Builder Pattern? I feel like the builder is a way to collect factories, but I would enjoy seeing a worked example.

mdunkman