ME FIRST GAMES
games for me and you
Dependency Injection (DI) is a design pattern in which an object receives other objects that it depends on.
A class doesn't know where it's dependencies came from and doesn't even need to pass dependencies to any child objects it creates.
⭐️ There are four roles an object may play in Dependency Injection.
📌 Service - The dependency or object to be used.
📌 Client - The object that is using and depending on the service.
📌 Interface - Defines how the client can use the service.
📌 Injector - The object responsible for constructing the services and injecting them into the client.
⭐️ If you're confused, no worries, here are examples of all the roles
📌 Service - Class Retriever, implements the IDog interface.
📌 Client - Class Walk, requires an IDog object.
📌 Interface - IDog, defines the methods for all Dogs.
📌 Injector - MyInjector, creates the correct type of dog and injects it.
⭐️ The power behind this design pattern is the loose coupling architecture.
💻 We could easily create a new type of dog, Pug, inject it, and the Walk class will work fine.
💻 Passing dependencies as arguments are no longer required.
💻 All dependency logic is offloaded into Injectors, allowing Dog to focus on being a dog.
💻 DI allows for factories to be injected also.
➡️ There is sometimes a performance cost depending on the implementation.
➡️ DI assets exist for Unity, we'll be covering this soon.
Leave a Reply.
Me First Games is an independent game studio dedicated to creating unique and bizarre video games.
Star Impact is an upcoming adventure platformer coming to PC, Mac, and Linux.
Copyright (c) 2020 Me First LLC