Filtering by Tag: protocols and delegates

Fighting With Delegation

Here's a quick illustration of how protocols and delegation can be used in a game to move combat mechanics into a separate class from characters.

The advantage of doing this is that a Person class needs only to know about its own attributes (and how to respond to external interactions) and can be totally oblivious to how the mechanics of the world work.

Below is a grossly simplified Person class and a brief interaction between two Person entities.

Note that we can freely improve and customise the specifics of interactions in the interaction manager without touching the Person class.

If we want additional functionality, we can put the method definitions into the protocol which will tell us exactly what changes need to be made to Person to work with the new functionality.