Design Patterns are templates implemented in many aspects of
coding to create a more efficient way to reuse code. Instead of just copying
and pasting code many times, with the correct design patterns, you could use
one design pattern for multiple situations in your code. Design Patterns also
show the relationships between classes or objects. It is always general and not
for a specific language such as C++ so others can easily understand Design
Patterns and implement it themselves. Reusing Design Patterns helps prevent issues that can cause major problems in the future.
Advantages of using Design Patterns:
- Speed up development process
- Help with debugging
- Increase readability of your code
- Increase the usability of your code
Some Design Patterns are:
Singleton:
The singleton pattern restricts instantiation of a class to one object which helps with when a single object is needed to coordinate actions to the system. A singleton only lets an object to have one instance of the class. Singletons have many uses since it only allows for a single instance. With a single instance, a singleton can be used for the renderer, mouse or even single sets of global parameters.
Facade Pattern:
A facade is an object that provides a simplified interface to code. It is a manager class that manages others. The facade provides a single interface to a large collection of classes related classes. This makes a really solid pipeline for your code and makes how the classes interact with each other much more easily. The facade pattern reduces a number of interactions between classes in 2 different subsystems and instead allows the managers from each class to interact with each other, where they can then relay the data to the appropriate class.
State Pattern:
State patterns deal with the objects state and use many switches or enums. The more states there are, the more confusing the state pattern can get. It gets even more troublesome when multiple functions must be shared between multiple states. A good way to help design this is to have a manager that controls the state logic and state switching. With shared functionality, it is always a good idea to use inheritance to solve your problems. When you want to add a new class to the system, it is easy to implement and doesn't require a lot of modify with other parts of the code. State patterns are great for controlling different states when using Artificial Intelligence (A.I) since you can switch between different states for different behaviours of characters.
An example of this is a typical character in a modern game has multiple actions or behaviours such as idle, walking and attacking. The game needs to know which state is should be doing at a time. The manager takes care of which is the correct state must be used at a certain situation, and once the correct state has been selected the character will change its behaviour and perform the correct action.
Factory Pattern:
Factory patterns allows for objects to be created without specifying what the exact class of the object. Factory pattern defines a separate method for creating objects, where subclasses can override to specify the derived type of the object that is to be created. This method allows for the creations of instances of all types. This can be as simple as passing in a specific ID and have your factory/GameEntity class to create an object for you. These can be simple primitives such as spheres or squares to more complex objects such as meshes and models. New functionality can be easily implemented without changing much of the code.
No comments:
Post a Comment