Section 2: Lecture 14: Overview Of Interfaces

My understanding of interface implementation is to implement an interface to better segregate the responsibilities of given classes and systems.

For example if I was creating an inventory system for a survival style game I could use an ILifetime interface and attach it to items I want to change over time. Things like food spoiling or a bag of sour mash turning into alcohol as time in the game progresses. All I would have to do after defining how the various classes that progress with time actually progress is call these functions form another class that handles time progression in the game world. This gives the benefit I mentioned earlier of segregating class and system responsibilities, so the classes that implement ILifetime interface are responsible for how their individual states change over time and system that calls these changes only has to know what items it should call this function on.

However it goes without saying my understanding may be flawed, incomplete, or the example given could be bad.

That’s a pretty good example! Remember that the interface defines just that, the methods that must exist on an ILifetime, for example:

public interface ILifetime
{
     void Tick(float deltaTime);
}

Then it’s up to whatever implements the interface to decide what happens in that method, could be a count down clock, healing the player, or whatever.

1 Like

Privacy & Terms