Redo and Undo action is everywhere, but how it’s done? In most cases, developers use the Command Design Pattern which we implemented in Unity last time. So how can we extend it with undo and redo action? Let’s find out! 🔥
Because we did a lot in the last post about the Command Design Pattern, we will use it as our base. If you didn’t see it yet, it’s a great time to do so! I can wait. 😜
Ready? So let’s get into that!
There is a lot of places where undo and redo action can come handy. Maybe it’s not that popular in games, but almost every app has it!
Let’s get back to the design patterns for Unity!
Today let’s introduce Command Design Pattern! This design pattern is famous for its encapsulation of requests, which can be useful for many applications and often is used for handling user input, but it’s not the only use case.
Command Design Pattern
The idea behind this design pattern is to move requests to the objects which could be collected and be executed in the queue. You can think about them like Actions or Events but represented as objects.
You might already notice that I wrote a lot about design patterns and examples of use for them in Unity. I am genuinely interested in writing clean code and designing project architecture. This brings me huge enjoyment and satisfaction, especially when the team is following project guidelines and everything looks so perfect. 😍
Being a programmer means that you need to improve your skills constantly. So today let’s summarize things that I wrote on this blog so far in term of Design Patterns! 🔥
What is Design Pattern?
The first thing that you need to understand is what “design pattern” means. So design pattern stands for a programming concept which solves conceptual problems. It can be related to creating new objects, making the code more independent or modular, or even split your project into different layers of responsibility.
There are a lot of different implementations of MVC design pattern available for Unity, but I’ve noticed one problem with most of them. They require a lot of knowledge about programming in general, and some of them use some fancy techniques which unnecessarily complicates your code.
And who wants to have hard to read and maintain code? 😉
MVC stands for Model – View – Controller and is one of most used design pattern in IT, but not necessarily in Game Dev. This design pattern is used to separate your code based on use.
Here we have three parts of this pattern: Model – these are classes responsible for storing and handling data. View – these are responsible for handling displaying data and receiving input from the user. Controller – and these are responsible for the logic behind the app in general. They also handle respond to the input from view, and they are passing data into views.
Those who know me, know that I have a rough relationship with the Singleton Design Pattern. Mostly I don’t like using it very often or even at all. This approach has a lot of benefits, starting with cleaner code architecture, less coupling, and in general fewer problems. 🥰
When to use it?
So when you would implement singleton? When you would like to have only one instance of a specific object. Example of what could be a custom resource manager where you store or load files that you downloaded from the web or any other data storage.