C++ Tutorial: the factory design pattern

  Рет қаралды 9,351

Professor Hank Stalica

Professor Hank Stalica

2 жыл бұрын

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.com/s/aznvi30wjob...
// Check out my software design patterns playlist:
• Software Design Patterns
// Check out my complete C++ videos playlist:
kzfaq.info?list...
// Consider supporting this channel in multiple ways:
Super Thanks
ko-fi.com/professorhank
paypal.me/hankstalica1
/ @professorhankstalica
Bitcoin: 177wfkQwzXiC8o2whQMVpSyuWUs95krKYB
Dogecoin: DRK2HDp3ZkkbFpvGZnVgMVosnBhPv8r3uP
#C++ #programming #tutorial

Пікірлер: 27
@sallaklamhayyen9876
@sallaklamhayyen9876 Ай бұрын
clear + simple = thank you so much
@ProfessorHankStalica
@ProfessorHankStalica 28 күн бұрын
Glad it helped!
@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! 🍻😆
@danielbruneau2936
@danielbruneau2936 Жыл бұрын
Thanks! Great explanation!
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Hey, Daniel. Thanks for the beer, and glad you found it helpful!
@alexplaytop
@alexplaytop 4 ай бұрын
Simply about hard stuff as for me, thank you very much!
@ProfessorHankStalica
@ProfessorHankStalica 4 ай бұрын
You can do it!
@chandusrinivas7813
@chandusrinivas7813 4 ай бұрын
Super explanation sir, Thank you!
@ProfessorHankStalica
@ProfessorHankStalica 4 ай бұрын
Glad you liked it
@propov1802
@propov1802 2 жыл бұрын
Excellent explanation.
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Glad you think so!
@Danta1ion
@Danta1ion Жыл бұрын
Can I simply think that the create() method is similar to the getClass() in Java, which in C++ returns a 'this' pointer?
@raht9578
@raht9578 7 ай бұрын
thank you so much
@ProfessorHankStalica
@ProfessorHankStalica 7 ай бұрын
You are welcome so much.
@VoidloniXaarii
@VoidloniXaarii 8 ай бұрын
Thank you
@ProfessorHankStalica
@ProfessorHankStalica 8 ай бұрын
You're welcome
@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.
@windowsbuilderthegreat3121
@windowsbuilderthegreat3121 Жыл бұрын
Can this design pattern work for objects created on the stack rather than on the heap
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
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.
@user-wc5gs4tt2m
@user-wc5gs4tt2m 5 ай бұрын
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 5 ай бұрын
None of the classes need a virtual constructor.
@user-mn7ks5tu6l
@user-mn7ks5tu6l 5 ай бұрын
​@@ProfessorHankStalica sorry I was asking for virtual destructor
@ProfessorHankStalica
@ProfessorHankStalica 5 ай бұрын
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.
@alexfu1734
@alexfu1734 6 ай бұрын
Why not using template instead of enum? Is factory required to support RTTI?
@ProfessorHankStalica
@ProfessorHankStalica 5 ай бұрын
Just because. There's many ways you could implement a factory pattern. This is one.
C++ Tutorial: the singleton design pattern
23:04
Professor Hank Stalica
Рет қаралды 1,4 М.
마시멜로우로 체감되는 요즘 물가
00:20
진영민yeongmin
Рет қаралды 34 МЛН
39kgのガリガリが踊る絵文字ダンス/39kg boney emoji dance#dance #ダンス #にんげんっていいな
00:16
💀Skeleton Ninja🥷【にんげんっていいなチャンネル】
Рет қаралды 8 МЛН
C++ Builder design pattern: A pragmatic approach
14:49
platis.solutions
Рет қаралды 3,7 М.
Everything is possible in math
11:08
Higher Mathematics
Рет қаралды 1,9 М.
OpenAI's New SearchGPT Shakes Up the Industry, Google Stock CRASHES!
10:10
Factory Method Pattern - Design Patterns (ep 4)
27:21
Christopher Okhravi
Рет қаралды 540 М.
Can we dooby Tesco's GU10 bulbs?  - with schematic
10:41
bigclivedotcom
Рет қаралды 62 М.
unique_ptr: C++'s simplest smart pointer
11:54
mCoding
Рет қаралды 41 М.