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.
If you’re reading my posts and you’re checking my public repositories, you might notice that I’m implementing UI with Events that are passing input to the controllers. This is really convenient as you have a clear separation between logic and display layers.
To recap what this UI implementation looks like, let me first explain where it comes from.
From the beginning, I always tried to make my code modular, but I couldn’t grasp how to do it in a really clean way. It was literally taking me years, multiple projects and few programming languages to get to digging into design patterns finally. With them, I started to understand more of what I was doing and how easy it was to start taking care of your code and separate different layers from each other! 😍
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.