Skip to content

ProgrammingPatterns

Henry m edited this page Jul 17, 2024 · 6 revisions

Programming Patterns

Introduction

References

  • Nys14 - Game programming patterns

Vocabulary

  • speed - how fast you are traveling

  • velocity - speed in a given direction

  • keep variable scope as narrow as possible, to have fewer places to keep in the head, while working with the code(Nys14, 114)

Singleton

When to use: Seemingly never?

Replacement for the Singleton:

  • Requiring a single instance - (Nys14, 112)
  • To provide convenient access to an instance
    • Pass it in (Nys14, 114)
    • Get it from the base class (Nys14, 114)
      • Maybe also see: ServiceLocator pattern(Nys14, 116)
    • Get it from something already global(Nys14, 116)
    • Get it from a service locator(Nys14, 117)

When the class interacts with an external system that maintains its own global state(Nys14, 99).


When deriving a class, it is a good idea for it to model the "is a" relationship. This means that the derived class should also be of the same type as the parent class. For example, deriving a Player2 class from Player would fit the model, as Player2 "is a" Player . But let's say, for example, we have a Jetpack class and we derive Player from this class to give it access to all the functionality that a Jetpack class has. This would not model the "is a" relationship, as a Player class is not a Jetpack class. It makes a lot more sense to say a Player class has a Jetpack class, and therefore, a Player class should have a member variable of type Jetpack with no inheritance; this is known as containment p61

  • If your core loop involves calling a virtual function many times per second, the performance penalties can add up.