The right way with DI

The implementation of DI is quite straightforward, and that's probably its main benefit. So, this section should be painless:

  1. Let's start by writing our Bike class; we could say that it's the actual client in this DI pattern example, mainly because it's the class that's dependent on receiving dependencies during the injection process:
using UnityEngine;public class Bike : MonoBehaviour{    private IEngine m_Engine;    private IDriver m_Driver;    // Setter injection    public void SetEngine(IEngine engine)    {        m_Engine = engine;    }        // Setter injection    public void SetDriver(IDriver driver)    {        m_Driver = driver;    }    public void StartEngine()    {        // Starting the engine        m_Engine.StartEngine();         // Giving control of the bike to a driver (AI ...

Get Hands-On Game Development Patterns with Unity 2019 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.