C++ Tutorial: the factory design pattern

  Рет қаралды 11,580

Professor Hank Stalica

Professor Hank Stalica

Күн бұрын

Here's a simple example and explanation of the Factory design pattern using C++.
In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created.
In this video, you'll learn how to implement one.
// Download the code here:
www.dropbox.co...
// Check out my software design patterns playlist:
• Software Design Patterns
// Check out my complete C++ videos playlist:
www.youtube.co....
// Consider supporting this channel in multiple ways:
Super Thanks
ko-fi.com/prof...
paypal.me/hank...
/ @professorhankstalica
Bitcoin: 177wfkQwzXiC8o2whQMVpSyuWUs95krKYB
Dogecoin: DRK2HDp3ZkkbFpvGZnVgMVosnBhPv8r3uP
#C++ #programming #tutorial

Пікірлер: 34
@danielbruneau2936
@danielbruneau2936 Жыл бұрын
Thanks! Great explanation!
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Hey, Daniel. Thanks for the beer, and glad you found it helpful!
@alexplaytop
@alexplaytop 10 ай бұрын
Simply about hard stuff as for me, thank you very much!
@ProfessorHankStalica
@ProfessorHankStalica 10 ай бұрын
You can do it!
@chandusrinivas7813
@chandusrinivas7813 11 ай бұрын
Super explanation sir, Thank you!
@ProfessorHankStalica
@ProfessorHankStalica 10 ай бұрын
Glad you liked it
@veronikabykova5494
@veronikabykova5494 Жыл бұрын
Amazing video, thank you so much! This helped me prep for my C++ code design interview:)
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Oh, that's fantastic! I hope you get the job. Good luck! If you get it, you owe me a beer! 🍻😆
@iaroslavz2644
@iaroslavz2644 15 күн бұрын
Thank you very much. It's a great explanation.
@ProfessorHankStalica
@ProfessorHankStalica 14 күн бұрын
Glad it was helpful!
@Muralikrishna-yz7jl
@Muralikrishna-yz7jl 3 ай бұрын
Nicely explained.. Thank you very much
@bartlx
@bartlx Ай бұрын
You showed a clean example of what a factory implementation 'looks like' in code, but I feel you came a bit short of 'why' you would want the overhead of a factory if you could instead create geometry objects directly based of a common shape interface. I guess you gave a hint at 13:32, making me think this is only useful when factory code and client code are written by different parties, and as a client you want a future proof way to create 3rd party defined shapes...
@anjan8614
@anjan8614 6 ай бұрын
Perfect sir. Thank you from INDIA. Subscribed to your channel.
@ProfessorHankStalica
@ProfessorHankStalica 4 ай бұрын
Namaste!!
@propov1802
@propov1802 2 жыл бұрын
Excellent explanation.
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Glad you think so!
@sallaklamhayyen9876
@sallaklamhayyen9876 7 ай бұрын
clear + simple = thank you so much
@ProfessorHankStalica
@ProfessorHankStalica 7 ай бұрын
Glad it helped!
@raht9578
@raht9578 Жыл бұрын
thank you so much
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
You are welcome so much.
@RamvilasGurjar-g2o
@RamvilasGurjar-g2o Жыл бұрын
Hi Stalica, does the base class need virtual constructor in this method if there are other data also in the derived classes (Triangle, circle and square) ?
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
None of the classes need a virtual constructor.
@रामविलासगुर्जर-च9ङ
@रामविलासगुर्जर-च9ङ Жыл бұрын
​@@ProfessorHankStalica sorry I was asking for virtual destructor
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Never a bad idea to make destructors virtual so that way you guarantee the right destructor is executed for the right object if there is any chance for a class to be inherited from.
@TN-cx4qi
@TN-cx4qi 9 күн бұрын
Shouldn’t the for loops be (Shape& s : shapes) not (Shape*& s : shapes)?
@VoidloniXaarii
@VoidloniXaarii Жыл бұрын
Thank you
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
You're welcome
@windowsbuilderthegreat3121
@windowsbuilderthegreat3121 2 жыл бұрын
Can this design pattern work for objects created on the stack rather than on the heap
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
You could allocate the Factory object on the stack and use statically allocated memory to allocate instances of each object as attributes of the Factory. Then you could return any of them on demand. Something like that might work, but would be unusual.
@alexfu1734
@alexfu1734 Жыл бұрын
Why not using template instead of enum? Is factory required to support RTTI?
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Just because. There's many ways you could implement a factory pattern. This is one.
@arthurtasca4527
@arthurtasca4527 Жыл бұрын
nice and clear video. which advantage do you see in using a Factory class instead of a create(type t) method directly (i.e. a factory method)?
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
If I understand the question, it's a matter of encapsulation and/or closely adhering to the object-oriented programming paradigm. Of course, rules can be broken, but it's going to depend on the requirements of your project and the overall resulting design you come up with. I prefer a consistent design, so if I'm going to do OOP, I'm going to do it all the way.
@Danta1ion
@Danta1ion 2 жыл бұрын
Can I simply think that the create() method is similar to the getClass() in Java, which in C++ returns a 'this' pointer?
C++ Tutorial: the singleton design pattern
23:04
Professor Hank Stalica
Рет қаралды 1,9 М.
Blind Boy Saved by Kind Girl ❤️
00:49
Alan Chikin Chow
Рет қаралды 50 МЛН
Smart Sigma Kid #funny #sigma
00:36
CRAZY GREAPA
Рет қаралды 51 МЛН
БАЙГАЙСТАН | 3 СЕРИЯ | ДУБАЙ |bayGUYS
44:17
bayGUYS
Рет қаралды 1,8 МЛН
vampire being clumsy💀
00:26
Endless Love
Рет қаралды 31 МЛН
Factory Pattern in C#: Creating Objects with Ease
12:54
campbelltech
Рет қаралды 20 М.
Return Value Optimization and Copy Elision in C++
9:54
mCoding
Рет қаралды 41 М.
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
Alex Hyett
Рет қаралды 333 М.
Singleton Design Pattern | C++ Example
13:24
Portfolio Courses
Рет қаралды 17 М.
C can do this too and it's faster than Python
2:09:48
Tsoding Daily
Рет қаралды 28 М.
10 Design Patterns Explained in 10 Minutes
11:04
Fireship
Рет қаралды 2,4 МЛН
Blind Boy Saved by Kind Girl ❤️
00:49
Alan Chikin Chow
Рет қаралды 50 МЛН