The Factory Pattern - Mike Shah - CppCon 2021

Поділитися
Вставка
  • Опубліковано 19 лют 2022
  • cppcon.org/
    github.com/CppCon/CppCon2021
    ---
    C++ programs that are dynamic in nature have to create objects at some time during run-time. New objects can be created by explicitly calling ‘new’ and then the data type of that object. However, this requires that a programmer knows at ‘compile-time’ what object should be created. What we would like, is to have a layer of abstraction, or someway to create objects at run-time to reflect the dynamic nature of some C++ programs. Luckily, there is a common pattern that can help solve this problem--the factory design pattern.
    In this talk, we are going to discuss a creational design pattern known as a factory. The pattern can be as simple as a function, or take on other forms as a distributed factory, or an abstract factory. We’ll show some basic examples of a factory in modern C++ as well as real world use cases of where factories occur for further study. Finally, we’ll discuss the tradeoffs of the factory pattern, and discuss which scenarios you may not actually want to use a factory. Attendees will leave this talk with the knowledge to go forward and implement the factory pattern, as well as how to spot the factory pattern in projects they may already be working on!
    ---
    Mike Shah
    Michael D. Shah completed his Ph.D. at Tufts University in the Redline Research Group in 2017. His Ph.D. thesis advisor was Samuel Z. Guyer. Michael finished his Masters degree in Computer Science in 2013 at Tufts University and Bachelors in Computers Science Engineering at The Ohio State University in 2011. Currently Michael is a lecturer at Northeastern University.
    Michael discovered computer science at the age of 13 when googling ”how do I make games”. From that google search, Mike has worked as a freelance game developer, worked in industry for Intel, Sony Playstation, Oblong Industries, and researched at The Ohio Supercomputer Center to name a few. Mike cares about building tools to help programmers monitor and improve the performance of realtime applications- especially games.
    In Michael’s spare time he is a long distance runner, weight lifter, and amateur pizza maker.
    ---
    Videos Filmed & Edited by Bash Films: www.BashFilms.com
    UA-cam Channel Managed by Digital Medium Ltd events.digital-medium.co.uk
    *--*
  • Наука та технологія

КОМЕНТАРІ • 15

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

    Thanks for having me CppCon! I hope folks enjoy this pattern! :)

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

      Thanks for the great talks, Mike!
      You're an asset to the back to basics track.

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

      When I learnt about Factory Pattern the first thing came in my mind what Command and Conquer:) Awesome job Mate 👏

  • @newoap
    @newoap 2 роки тому +3

    Really enjoyed the talk Mike.
    You should do the State design pattern next as that is one I have trouble understanding😉

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

    Mike is a fantastic educator and I think if this talk was just in the back to basics track, and not the software design track, there would have been a lot less annoying nitpicky comments/“questions” from professionals with their egos hurt by being taught something they already know.

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

    A great approach for mimicking somehow a virtual constructor in the abstract class and allowing for the run time object creation. I think using modern features of c++ like std::function, lamda, code injection (or the old feature, macro), we can make it more general. So that we can auto generate the required static members and methods. We also can generat various constructor intrefaces for the object.

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

    I tried to extend your fancy registration style factory pattern. I wanted just two things: 1. Don’t force the concrete create method to obey a fixed constructor function signature (use variadic template). 2. Use std::function…
    I flamed out miserably, perhaps because I was running on 5 hours of sleep and 3 cups of coffee…

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

    My gripe is not with running interspersed updates and renders (which you admit is a problem) but with keeping all game objects as `Rc`.
    Wouldn't keeping objects of the same concrete type in individual dense non-dynamic collections (whether all together like in your prototype or split into Components within Systems) rather than a single collection of heterogenous dynamic objects always win?

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

    Why was the singleton create method required? Wouldn't it be likely that one would create more than one instance of ant?

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

    Is there any way in C++ to write this code using a generic like you might in C#? Like instead of Factory::Create(“ant”, 5, 5) could you do Factory::Create(5, 5) ?

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

      template
      void MakeItComplicated(T* any)
      {
      any->compilcated = Factory::CreateComplicated(5,5);
      }
      Can also be done using concepts, if you want to be fancy.
      Or just ..
      Ant ant;
      ant.complicated = Factory::CreateCompilcated(5,5);

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

    41:47

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

    I try with lambda is working well, better make it whole class as templates with function type or something like that.

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

    Inheritance bad REEEEE

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

    I honestly don't think this is a good example of a use case for the factory pattern. In games you're more likely to use(and you should probably use) an ecs.
    In what case it can be useful in in games is in combination with a state pattern. That's pretty much the only instance where we use factories in our real-time applications.