Design Patterns - Factory Method Pattern Adding More Power to Count Allocated Objects in C++

Поділитися
Вставка
  • Опубліковано 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 a few ideas on how you may decide to add more power to the 'Factory Method' pattern.
    If you'd like to review singletons, you can do so here: • Design Patterns - Sing...
    00:00 Recap of previous lesson
    00:55 Wrapping the factory into a singleton
    1:40 Our motivation is for book keeping
    2:20 Refactoring our function into a class
    4:50 First test after refactoring
    5:46 Counting object types that were created
    7:50 PrintCounts function implementation
    9:00 Discussion on counting active objects
    10:27 Sketch of using adapter to count objects
    12:25 Other idea to use weak pointers to count active objects
    NOTE: You will have to think a little bit about solution you would like to implement, and as always, there are tradeoffs depending on how much power you want, how many layers of abstraction you may choose to use (and if this may cost you space or time), and how simple (i.e. readable) you want your code to be.
    ►UA-cam Channel: / mikeshah
    ►Please like and subscribe to help the channel!
    ►Join our free community: courses.mshah.io/communities/...
  • Наука та технологія

КОМЕНТАРІ • 15

  • @Maximus98245
    @Maximus98245 Місяць тому +1

    Hi Mike, great video! Could we have not used the reference count of the shared pointers to tell us how many active objects there are? Or maybe the reference count of the weak ptr associated with the shared ptr ? Thanks!
    auto ptr1 = std::make_shared(10);
    {
    auto ptr2 = ptr1;
    std::cout

    • @MikeShah
      @MikeShah  Місяць тому

      We can utilize use_count to tell how many times the pointer is referenced in a single-threaded environment (keep in mind, multithreading will otherwise not make this or 'expired' reliable)

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

    @9:35 can't we delete by overloading the delete operator? something like:
    void operator delete(void* memory){ free(memory);}

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

      Could certainly do something like that to track allocations.

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

    Is the wrapper the best approach for this problem? Could you store them in a container that gets created with the constructer and push back the shared_ptr when the CreateObject function is called? Then you could use the size function from that container?

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

      Could also use a container to count the number of objects created. I think I would improve the above by using an atomic to keep count at the least.

  • @fozilbekk
    @fozilbekk 2 роки тому +2

    hello bro, good job
    Could you recommend something solid for modern openGL 3.3+ with SDL2 (any website or books)
    I have googled it but could not get .
    I mean I am gettingSDL2 with this channel but I want openGl 3.3+ with it.

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

      OpenGL 3.3 - 4.6 videos are coming to this channel soon :) Otherwise, learnopengl.com is a good start!

  • @wojciechgrunwald80
    @wojciechgrunwald80 7 місяців тому

    can you explain how to further code the fragment with adapter?

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

      At some point I'll cover some other patterns like adapter in this series.

  • @heyheyheyhoev419
    @heyheyheyhoev419 9 місяців тому

    Hi, Mike!
    Awesome tut, I have one question: on 3:23 you created copy-constructor in private section of "factory" class, but what is the difference with explicit restriction on copy constructor on next line:
    FactoryGameObjects(const& FactoryGameObjects o) = delete;

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

      Effectively the same idea -- one is the pre c++11 way of avoiding copies, and 'delete' is the C++11 and beyond way of doing things.

    • @heyheyheyhoev419
      @heyheyheyhoev419 9 місяців тому

      I spotted your case in one of SDL examples(file or asset handler maybe) , so this question appeared in my mind.

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

      @@heyheyheyhoev419 Ah great! :)