Design Patterns - The Extensible Factory Pattern in C++ | Register Objects at Runtime
Вставка
- Опубліковано 21 лип 2024
- ►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 present to you an even more powerful version of the factory, in which you can extend and register objects at run-time in your factories. This makes your factories even more powerful, as clients can derive objects from your base classes, and then register them so they are created from the same factory you provide.
If you'd like to review a previous iteration of the factory pattern, you can do so here: • Design Patterns - Fac...
00:00 Introduction and refresher on factory pattern
1:10 Utilizing run-time polymorphism for factory
2:10 Extensible Factory allows you to add objects at runtime
3:20 Refresher of IGameObject
4:05 The Create Function in derived types
5:10 Example of a user created type derived from IGameObject
6:15 The Extensible Factory Interface
7:35 Using a callback function as a parameter in a map
8:33 Map storing how to create types
10:30 Implementation of Extensible Factory
12:17 Registering new types
13:00 Using our factory
14:25 GDB walkthrough to show working extensible factory
16:40 Conclusion and wrap up
►UA-cam Channel: / mikeshah
►Please like and subscribe to help the channel!
►Join our free community: courses.mshah.io/communities/... - Наука та технологія
Thank you Mike. I wasn't aware of this extensible factory pattern, prior to your lecture. It was amazing to learn this new pattern.
Cheers, thank you for the kind words!
I really appreciate this series. I hope you continue.
Thank-you, you are a gentleman and a scholar.
Thank you for the kind words :) More coming, observer is next!
thanks for this. Design patterns is something that is not emphasized as much in most curriculum. This playlist helped me a lot. thanks
You're most welcome!
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
With extensible factory you can add different types at run-time that you want to create.
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().
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.
Brilliant, thanks.
Cheers!
Awesome Mike thanks. What are those 2 books you mentioned, if I heard right they seem some 20 years old.
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.
Very interesting. Thanks!
You're most welcome!
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?
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.
Thanks a lot for the reply! I will look into abstract factory pattern!
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.
@@fishplantsandhamsters8391 Can still have static members, just need an instantiation of the templated class before using it.
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.
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)
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?
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.
@@MikeShah Perfect explanation, thank you!
Interesting... Thanks
You are most welcome!
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.
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.