No video

Design Patterns - The Extensible Factory Pattern in C++ | Register Objects at Runtime

  Рет қаралды 3,304

Mike Shah

Mike Shah

Күн бұрын

Пікірлер: 30
@WilhelmDrake
@WilhelmDrake Жыл бұрын
I really appreciate this series. I hope you continue. Thank-you, you are a gentleman and a scholar.
@MikeShah
@MikeShah Жыл бұрын
Thank you for the kind words :) More coming, observer is next!
@jeevanreddy7989
@jeevanreddy7989 Жыл бұрын
Thank you Mike. I wasn't aware of this extensible factory pattern, prior to your lecture. It was amazing to learn this new pattern.
@MikeShah
@MikeShah Жыл бұрын
Cheers, thank you for the kind words!
@raghul1208
@raghul1208 2 жыл бұрын
thanks for this. Design patterns is something that is not emphasized as much in most curriculum. This playlist helped me a lot. thanks
@MikeShah
@MikeShah 2 жыл бұрын
You're most welcome!
@fishplantsandhamsters8391
@fishplantsandhamsters8391 Жыл бұрын
Great lesson! I was thinking about making a factory pattern extensible after learning about it and this is exactly what i need to know! My next question though is can this class be turned into something generic? like templatize it?
@MikeShah
@MikeShah Жыл бұрын
You could make it a template, though the extensible factory itself is able to make any type of object. Probably want to look at the 'abstract factory pattern' which allows you to make any type of concrete factory through a template.
@fishplantsandhamsters8391
@fishplantsandhamsters8391 Жыл бұрын
Thanks a lot for the reply! I will look into abstract factory pattern!
@fishplantsandhamsters8391
@fishplantsandhamsters8391 Жыл бұрын
One more thing, once I turn this factory class into an abstract class, It cannot be static anymore correct? Only classes that inherits it will be allowed to be static.
@MikeShah
@MikeShah Жыл бұрын
@@fishplantsandhamsters8391 Can still have static members, just need an instantiation of the templated class before using it.
@whykozhoma
@whykozhoma 10 ай бұрын
Hi Mike! Really appreciate your lessons. Crazy that you can get such a quality product for free. Question: what is the right way to avoid memory leaks in the extensible factory example, cuz clearly there is no delete for each object you return with Create().
@MikeShah
@MikeShah 10 ай бұрын
Cheers! From the actual extensible factory you can erase when you unregister objects. Otherwise, for the actual object, if they are heap allocated you can simply delete.
@tanveerasif5978
@tanveerasif5978 Ай бұрын
Brilliant, thanks.
@MikeShah
@MikeShah Ай бұрын
Cheers!
@Itsme1n1ly
@Itsme1n1ly Жыл бұрын
Thanks Mike for sharing your knowledge. Its very useful. Kudos to you. Sorry to asking this. 1. What's the significant differences in using this extensible factory vs previous version of factory that has static getInstance()? 1. Register object 2. Statis is present in the derviced class Anything? In terms of terminology, such as Extensibility, avoid duplication, reusability, and ??? 2. Keeping static create in evry class makes the illusion of code redundancy, right? Whats your thoughts? Bear with my questions
@MikeShah
@MikeShah Жыл бұрын
With extensible factory you can add different types at run-time that you want to create.
@antonfernando8409
@antonfernando8409 6 ай бұрын
Awesome Mike thanks. What are those 2 books you mentioned, if I heard right they seem some 20 years old.
@MikeShah
@MikeShah 6 ай бұрын
Modern C++ Design (Andrei Alexandrescu) and C++ API Design are the two I recommend. The latter I would recommend (C++ API Design), and uses C++11. Andrei's book is older, but still excellent for how to think about programming and policy-based design.
@jeevanreddy7989
@jeevanreddy7989 Жыл бұрын
Mike...if the static methods are used to create a object, shouldn't the constructor of a class be private? In the examples used Ant, Plane classe have public constructors.
@MikeShah
@MikeShah Жыл бұрын
If static methods are being used, then you're right, probably best to make constructors private (i.e. effectively the factory design could be a singleton)
@jeordanisfiguereo7592
@jeordanisfiguereo7592 2 жыл бұрын
Very interesting. Thanks!
@MikeShah
@MikeShah 2 жыл бұрын
You're most welcome!
@nicolaschan3335
@nicolaschan3335 7 ай бұрын
Thanks for the video! I have a noob question - If I wanted to have some default object types (plane and boat for instance), instead of registering them in main.cpp like ant, do I just initialise them in the callback map in Factory.cpp after the callback map has been initialised?
@MikeShah
@MikeShah 7 ай бұрын
You could do that, but in some ways you coupling some behavior to your factory. It's likely better you do so from main (or write a function that perhaps loads from a config file the plane, boat, etc.) if you plan on modifying the number of types you'll add. I'd say if you know exactly the fixed-set of types you'll have, extensible factory may be over-engineering, but if you want to be open to extension, it's not a bad idea.
@nicolaschan3335
@nicolaschan3335 7 ай бұрын
@@MikeShah Perfect explanation, thank you!
@mohammedaqdam1708
@mohammedaqdam1708 2 жыл бұрын
Interesting... Thanks
@MikeShah
@MikeShah 2 жыл бұрын
You are most welcome!
@MohamedHamzaoui
@MohamedHamzaoui 4 ай бұрын
Since you have to register manually all objects, this can't be an OCP ready solution. Imagine that you have to use this factory in a library, it will be necessary to update the registration code whenever you add new type. The code must be closed for modification and open to extension.
@MikeShah
@MikeShah 4 ай бұрын
Could probably use a std::map to register new types at run-time, or a plugin system to take care of types loaded from a library.
Prank vs Prank #shorts
00:28
Mr DegrEE
Рет қаралды 8 МЛН
Мы сделали гигантские сухарики!  #большаяеда
00:44
Factory Method Pattern - Design Patterns (ep 4)
27:21
Christopher Okhravi
Рет қаралды 542 М.
SINGLETONS in C++
19:16
The Cherno
Рет қаралды 199 М.
The Factory Pattern in Python // Separate Creation From Use
14:58
The Factory Pattern - Mike Shah - CppCon 2021
1:01:06
CppCon
Рет қаралды 19 М.
C++ Tutorial: the factory design pattern
14:21
Professor Hank Stalica
Рет қаралды 10 М.
The Decorator - Programming Design Patterns - Ep 5 - C++ Coding
24:40
Code, Tech, and Tutorials
Рет қаралды 2,1 М.
Adapter Pattern - Design Patterns (ep 8)
26:36
Christopher Okhravi
Рет қаралды 241 М.
Prank vs Prank #shorts
00:28
Mr DegrEE
Рет қаралды 8 МЛН