I want to discuss the use-cases of the chain of responsibility pattern in unity and game development. From my perspective design patterns are something that every development should look into from time to time. It’s like a spare time activity. This will not only make you spend some meaningful time for your career but also allow you remember the patterns. It’ s not needed to remember all the details. Remembering what the pattern tries to solve would be enough as you can look into it more detailed later when you need it.
So let’s get back to the chain of responsibility pattern. We, as DreamHarvesters, used this pattern a lot in the recent projects. That’s why i wanted to share some of the use-cases 🙂
First things first, let’s see what the pattern is
What’s Chain Of Responsibility?
Chain Of Responsibility is one of the behavioral patterns. The intention is while reducing the coupling between the request sender and its processors, allowing to process a request with a chain like object formation.
Was it a complicated definition?
If ok please have a look to the image below:
As you can see there’ s a client which creates the request
Processing elements in the image will get the request in the specified order and process it based on their capabilities.
If you need any further reference follow this link 🙂
Let’s discuss two different use-cases of the pattern:
In one of our projects, we used Photon Network. That’s why the application required to be connected to the internet, photon network, photon lobby, etc.
This connection checks need to be in order. Our connection handling logic needs to check if the device still connected to the internet, then photon network, then the lobby, then to the chat. Finding any of these connections were lost should cause app notifying the user and connect automatically.
This scenario is very convenient for the chain of responsibility pattern. Our client is the class that needs to check the connection. Processing elements are;
- internet connection handler
- photon connection handler
- Lobby connection handler
- Chat connection handler
Every handler checks the connection first and tries to reconnect if the connection is lost.
Our handler base class, BaseConnectionHandler, implements the interface IHandler which has a method Handle(). All the concrete handlers override BaseConnectionHandler as required.
Our client class, ConnectionHandling, sends a request to this chain periodically so we’ re sure the user has the best UX when a connection lost and reconnection is made automatically.
Authentication is something nearly all the applications need. The complicated parts of auth. Is the 3rd party providers like Facebook, Google, etc.
Generally, to have a frictionless signup, apps signs up users automatically as guest users. This allows users having an account without any efforts. In the application, at a later time, the app redirects users to signup with a persistent account using one of the choices like custom account(email-password), facebook, google, etc.
User might use guest account for a while before creating a persistent account. That’s something apps must handle so the user does not lose any information that remains in the guest account. This process requires merging the guest account, that created for the user, with the persistent account.
Here comes the chain of responsibility for our auth. Logic.
The client is a class like Authenticator.
Process Elements are concrete authenticators for different providers. (This use-case might require the composite pattern as well, but this is not the topic of this post :))
Examples for the chain might be:
Of course, we have to support the chain with factories so instance creation does not mess the logic.
Hope these use-cases help you in future for similar situations,