ME FIRST GAMES
Menu

games for me and you

Dependency Injection - Advanced Coding

9/11/2020

0 Comments

 
Picture
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.

0 Comments



Leave a Reply.

    Picture
    Me First Games is an independent game studio dedicated to creating unique and bizarre video games.
    GAMES HERE

    Red Matter

    Star Impact is an upcoming adventure platformer coming to PC, Mac, and Linux.

    Archives

    April 2021
    February 2021
    December 2020
    November 2020
    October 2020
    September 2020
    August 2020
    July 2020
    June 2020
    May 2020
    April 2019
    March 2019
    December 2018
    April 2018

    Categories

    All

    RSS Feed

Copyright (c) 2020 Me First LLC
  • STU the First AI
  • Blog
  • Contact
  • Unity Tools
    • Hierarchy Comments
  • STU the First AI
  • Blog
  • Contact
  • Unity Tools
    • Hierarchy Comments