Controller class and logic

Viewed 49

So I have been programming some inventory code for a unity project.

I have the Slot class which contains the logic that happens with that slot ( locking it, filling with content , freeing it etc).

However , I also found that I needed a SlotsController class which will control what happens with the Slot when I am clicking it, selecting multiple Slots, stacking them etc.

Example 1 : Slot will call the method OnClicked(Slot slot) , found on the controller, and then the controller will call the method Fill() found on the Slot.

Example 2 : The Controller has a slot already selected , and when I click another slot, I call the Stack() method on the other slot and Free() on the selected slot.

Now the question is: Is this the correct way to do it? Should I have all the Slot logic contained inside that class and only call it’s methods from the Controller class? I feel like the SlotController class is something like a Manager class which is quite a disregarded thing to do.

( I hope it’s clear enough , English is not my native language )

1 Answers

In your example it becomes clear to me that the purpose of the controller is not only to manage the one and only slot. It is used to manage more then one slot and manage the interaction/state between them.

So on itself, you do not need a controller when you have slots which do not need to interact. The concept of controllers is common practise, many developers implement them at the start as they know their program will expand and become more complicated.

Another good use of the controller is when you give the slot to a player(s) in your game. The controller can keep track on who has the slot or where are the slots poisitioned in the game.

Related