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/...
  • Наука та технологія

КОМЕНТАРІ • 30

  • @jeevanreddy7989
    @jeevanreddy7989 Рік тому +2

    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!

  • @WilhelmDrake
    @WilhelmDrake Рік тому +2

    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!

  • @raghul1208
    @raghul1208 2 роки тому +1

    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!

  • @Itsme1n1ly
    @Itsme1n1ly 11 місяців тому +1

    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  11 місяців тому

      With extensible factory you can add different types at run-time that you want to create.

  • @whykozhoma
    @whykozhoma 9 місяців тому +1

    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  9 місяців тому

      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 3 дні тому +1

    Brilliant, thanks.

  • @antonfernando8409
    @antonfernando8409 5 місяців тому +1

    Awesome Mike thanks. What are those 2 books you mentioned, if I heard right they seem some 20 years old.

    • @MikeShah
      @MikeShah  5 місяців тому

      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.

  • @jeordanisfiguereo7592
    @jeordanisfiguereo7592 2 роки тому +1

    Very interesting. Thanks!

    • @MikeShah
      @MikeShah  2 роки тому

      You're most welcome!

  • @fishplantsandhamsters8391
    @fishplantsandhamsters8391 11 місяців тому +1

    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  11 місяців тому

      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 11 місяців тому +1

      Thanks a lot for the reply! I will look into abstract factory pattern!

    • @fishplantsandhamsters8391
      @fishplantsandhamsters8391 11 місяців тому +1

      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  11 місяців тому +1

      @@fishplantsandhamsters8391 Can still have static members, just need an instantiation of the templated class before using it.

  • @jeevanreddy7989
    @jeevanreddy7989 Рік тому +1

    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  Рік тому +1

      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)

  • @nicolaschan3335
    @nicolaschan3335 6 місяців тому +1

    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  6 місяців тому +1

      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 6 місяців тому +1

      @@MikeShah Perfect explanation, thank you!

  • @mohammedaqdam1708
    @mohammedaqdam1708 Рік тому +1

    Interesting... Thanks

  • @MohamedHamzaoui
    @MohamedHamzaoui 3 місяці тому

    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  3 місяці тому

      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.