Design Patterns - Factory Method Pattern Explanation and Implementation in C++

  Рет қаралды 8,010

Mike Shah

Mike Shah

Күн бұрын

►Software Design and Design Patterns Playlist: • C++ Software Design an...
►Find full courses on: courses.mshah.io/
►Join as Member to Support the channel: / @mikeshah
►Git Repo: github.com/MikeShah/DesignPat...
►Lesson Description: In this lesson I teach you about a creational design pattern to have a single point in which you allocate objects at run-time. This is the factory method (or factory function), which takes advantage of inheritance based polymorphism to create the correct object at run-time and return it to the user.
00:00 Factory method and definition
1:25 Goal of the factory method pattern
2:37 Inheritance based polymorphism
3:40 Start of implementation with interface
4:50 Creating derived classes
5:58 Creation of our factory
9:15 Confirming the factory works in GDB
10:50 Refactoring our factory arguments with enum class
14:35 Returning a smart pointer from our factory
16:12 Confirming no memory leaks with valgrind
17:30 Recap of source code and pattern
18:00 Pros and cons of the pattern
►KZbin Channel: / mikeshah
►Please like and subscribe to help the channel!
►Join our free community: courses.mshah.io/communities/...

Пікірлер: 29
@anhurtjv
@anhurtjv 2 жыл бұрын
Your video is probably one of the best I have found in how to use design patterns in C++. It is really clear, and the fact that you show in real time what you are doing really helps follow the content.
@MikeShah
@MikeShah 2 жыл бұрын
Thank you for the kind words!
@k0185123
@k0185123 3 ай бұрын
amazing. I'm so lucky to have the chance to watch your videos!!!!!
@MikeShah
@MikeShah 3 ай бұрын
Thank you for the kind words!
@stevea.b.9282
@stevea.b.9282 3 ай бұрын
Thank you for explaining these patterns so clearly and for keeping it simple. Other sources I tried made them seem very complicated and I almost gave up.. thankfully I came across your channel! I can't thank you enough.
@MikeShah
@MikeShah 3 ай бұрын
Cheers! More coming to this series tomorrow 🙂
@user-ql7pw7ld1n
@user-ql7pw7ld1n Ай бұрын
Understood fully..loved this :)
@MikeShah
@MikeShah Ай бұрын
Cheers!
@bharatpatidar3693
@bharatpatidar3693 Жыл бұрын
Man i just got the understanding of Factory Design pattern after watching it so easily Thanks a lot 🙏
@MikeShah
@MikeShah Жыл бұрын
Cheers, glad to hear it!
@pratekag
@pratekag Жыл бұрын
info vtbl was really cool. thank you.
@MikeShah
@MikeShah Жыл бұрын
You are most welcome!
@riamuglihal1860
@riamuglihal1860 Жыл бұрын
Absolutely loved your teaching style! 🙌 Thanks for the cool tip: --tui
@essmunson
@essmunson Жыл бұрын
_thank you thank you thank you_ for the clear explanation
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@anasmansouri493
@anasmansouri493 Жыл бұрын
thanks a lot for this explanation
@MikeShah
@MikeShah Жыл бұрын
Cheers, you are most welcome
@larslindved9586
@larslindved9586 Жыл бұрын
Great video!
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@1981Praveer
@1981Praveer 4 ай бұрын
Curious, Is there any reason to use shared_ptr? in general, by default people use unique_ptr.
@MikeShah
@MikeShah 4 ай бұрын
Correct, unique_ptr should be the default that folks reach for. shared_ptr is appropriate if your object is referenced multiple times -- which is what I was thinking about in a gaming context :)
@user-cm2ko8ok9r
@user-cm2ko8ok9r 10 ай бұрын
Thanks for the clear explanation and great content. According to refactoring guru, the design pattern "factory method" would have a creator, concrete creators, product, and concrete products. In the example you provided, MakeGameObjectFactory seems to be an object creation function that abstracts away the object creation process and provides an easier way to create objects. I'm wondering if this is a simplified variation of the factory method, or if this still aligns well with the standard factory method? Thanks.
@MikeShah
@MikeShah 10 ай бұрын
Cheers -- yes this is an example of a factory method.
@mohamedericsson7203
@mohamedericsson7203 Жыл бұрын
Quality content
@MikeShah
@MikeShah Жыл бұрын
Cheers, thank you for the kind words!
@windowsbuilderthegreat3121
@windowsbuilderthegreat3121 Жыл бұрын
Does this pattern have to use heap allocation to work
@MikeShah
@MikeShah Жыл бұрын
You could make stack allocations or otherwise use a custom allocator to decide where to allocate your new objects. I suppose this depends on the requirements of your design and how much stack space you have. Heap memory (at least on desktops) we have much more memory :)
@maheshshinde6946
@maheshshinde6946 8 ай бұрын
Can we keep factory method as static class method and keep class constructor private? so only from factory method we can create instance of class.
@MikeShah
@MikeShah 8 ай бұрын
You can create a factory as a singleton if that is what you're asking -- sure!
Как быстро замутить ЭлектроСамокат
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 13 МЛН
Climbing to 18M Subscribers 🎉
00:32
Matt Larose
Рет қаралды 15 МЛН
TRY NOT TO LAUGH 😂
00:56
Feinxy
Рет қаралды 8 МЛН
What is the Factory Pattern? | Software Design Principles
26:48
Be A Better Dev
Рет қаралды 27 М.
Factory Pattern in C# with Dependency Injection
1:07:33
IAmTimCorey
Рет қаралды 102 М.
Design Patterns: Examples in C++ - Chris Ryan - ACCU 2023
1:39:33
ACCU Conference
Рет қаралды 6 М.
Design Patterns: Factories
25:26
Microsoft Visual Studio
Рет қаралды 54 М.
Here’s a More Pythonic Factory Pattern
23:18
ArjanCodes
Рет қаралды 99 М.
10 Design Patterns Explained in 10 Minutes
11:04
Fireship
Рет қаралды 2,2 МЛН
Выложил СВОЙ АЙФОН НА АВИТО #shorts
0:42
Дмитрий Левандовский
Рет қаралды 2,1 МЛН
С ноутбуком придется попрощаться
0:18
Up Your Brains
Рет қаралды 414 М.
How charged your battery?
0:14
V.A. show / Магика
Рет қаралды 4,5 МЛН
How To Unlock Your iphone With Your Voice
0:34
요루퐁 yorupong
Рет қаралды 20 МЛН
WWDC 2024 Recap: Is Apple Intelligence Legit?
18:23
Marques Brownlee
Рет қаралды 4,2 МЛН
ВЫ ЧЕ СДЕЛАЛИ С iOS 18?!
22:40
Overtake lab
Рет қаралды 63 М.