In the second of our dark arts sessions we were taught about the uses of singletons and entities in our coding. As I've already covered the singleton pattern in Java, it was simple recap when Richard talked about how to use it, however it was very useful to see the examples he gave of where in a game it could be used such as for UI managers. The singleton pattern is where you set something up that is only created once and called anytime it's ever needed. This stops multiple copies of the same object from being instantiated, lowering the amount of errors you could get and also keeping memory management tidier. The downsides are that it isn't thread safe and everything can access it, which is just as useful as it is a liability.
 |
How to instantiate and keep an singleton |
Virtual methods were the second subject Richard covered, which are methods a class can essentially inherit from, unless the method is overwritten. Essentially it allows you to write one class with a lot of methods in for a variety of objects that will use them, but be able to change it for each object if needs be. The base class is essentially the game entity with all other classes using it if they require the methods.
 |
Example of two classes using a base class but one overriding the method |
No comments:
Post a Comment