$ ❯ CrowderSoup

A website about programming, technology, and life.

design patterns

Day 10: Dependency Injection

by Aaron Crowder on in Programming

What is it? Dependency Injection is a design pattern whereby an object/module’s dependencies are provided to it. It is one of several techniques for implementing Inversion of Control. So rather than: func NewClient() *Client { return &Client{ Service: service.NewService(), } } You would have: func NewClient(service *service.Service) *Client { return &Client{ Service: service, } } Why tho?? If you wanted to write a test for the Client first example above, you wouldn’t be able to mock it’s Service.

Read more...