61: Design Patterns: Prototype.
Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Kategorier:
The prototype creational pattern is great for creating objects that you may not know about ahead of time. If your program allows users to build complex objects from simpler components and then needs to build more of these complex objects or if your program loads plugins at runtime, then it can benefit from this design pattern. In order to understand and get the most benefit from this pattern, you need to understand the difference between a shallow copy and a deep copy. Shallow vs. deep copies are normally discussed when working with classes that have pointers or references especially to other class types which could also have pointers and references. A shallow copy just copies the first class data members as-is so that any pointers in the copy will point to the same data as in the original. A deep copy will change pointers so they point to their own copy of whatever the original points to. Deep copies will also continue this pattern looking for any possibility of shared data. To make use of the prototype design pattern, just create a base class with a virtual method called clone which returns a pointer to another base class. The clone method will implement a deep copy and return a pointer to the base class. Your original code will just work with the base class pointer without worrying about what specific type it actually points to. After all, this type could be some new type defined by a plugin or some add-on which your code has no idea even exists. Listen to the full episode for more about this pattern or read the full transcript below. Transcript The basic description says that this pattern allows you to create new objects by copying prototypes or examples. Before we get too far into this explanation, I want to explain an important concept that’s directly applicable to this pattern as well as any time you need to copy items. This concept is the difference between a shallow copy and a deep copy. And sometimes, there’s no difference at all. Let’s take a simple example and say you just have an integer variable called height with the value 1. You can create another integer variable called anotherHeight and assign it whatever value is in the height variable. When you’re done, both variables will contain the value 1. There’s no need to consider a shallow vs. deep copy because it just doesn’t apply. You either assign the value from another integer or you don’t. The really important part about this assignment is that while both height and anotherHeight have the value 1, you can change anotherHeight to 3 without affecting the height variable. Even if they have the same value, they have their own independent values. Let’s take a more complicated example and look at a class with a couple integer member variables height and width. You have a class with either your own custom assignment operator and copy constructor or you’re using the default. Let’s call this class a box and you have an instance called redBox that’s 3 wide and 5 tall. The units are up to you. These could be feet, inches, meters, or whatever. You have another box called the blueBox that you copy from the redBox. When you’re done, both boxes will have their own width and height that’s the same. They’re also independent of one another. But what about a pointer or a reference? If you have an integer variable called height with the value 1 and then you declare a pointer to an integer variable called heightPointer and assign heightPointer the address of your height integer, then heightPointer points to height. Now you already know that there’s really only a single integer value in memory here with the value 1. The other pointer variable just points to the same memory. Changing height to the value 3 will also change the value that the heightPointer variable points to. This is because it points to the same thing. But what if we have another pointer variable called anotherHeightPointer and