From the course: C++ Design Patterns: Structural
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Reducing memory usage: Part 1 - C++ Tutorial
From the course: C++ Design Patterns: Structural
Reducing memory usage: Part 1
- [Instructor] Facade uses sharing to avoid unnecessary object creation and minimize memory usage. Let's demonstrate how to apply this design pattern to improve our demo code. First, we'll need to identify the intrinsic state of our sprite class. The intrinsic state is the state that can be shared between sprite instances. Let's have a look at the data members in the sprite class. The width and height of the sprite as well as its coordinates X and Y are specific to each instance. Thus, sharing them with other sprite objects doesn't make sense. We can consider these properties part of sprite's extrinsic state, which should be set by the client code and shouldn't be shared. Now, the texture is a different story. Sprites with the same texture are common in video games. Just think of particle effects used to simulate fire, rain, or smoke. These effects are created by spawning a large number of sprites with the same…