Dependency Injection as application design pattern

1. Inflexible

2. Untestable

3. Hard to maintain

Introduction to Dependency Injection

1. Flexible

2. Testable

  1. It has two optional parameters in the constructor. We can pass them as static coordinates to return from getCurrentCoordinates and getAddressCoordinates methods.
  2. It has one additional method generateRandomCoordinates to avoid code duplication in two other methods.
  3. Implementation of getCurrentCoordinates and getAddressCoordinates doesn’t perform any network requests. Results are either static or completely random.
  1. Navigator has nothing to do with service construction anymore. So it doesn’t care what parameters should or can be passed to the service upon creation.
  2. Injected service should implement specific interface, that is, contain at least all the methods Navigator will use. The key word here is “at least”. So additional methods don’t bother us anyhow.
  3. What about meaningless implementation, Navigator has no idea how coordinates are figured out. It just expects them to be an object with lat and lng properties. This is exactly what we get.

3. Easy to maintain

Factory functions alternative

Dependency graph

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

LEARN TO CODE | BTC

Why we invested in our Platform early and how we did it

The hyperexponential bloom: upgrading the economy to an open operational system

The Generous April: Courses for Students, Tools for Instructors, Support for Reskillers and…

April Monthly Update

Distributed File Synchronization in FlowWright

How to set up In App Purchases in Apple Connect and Google Play Console

Intellij…You can’t find what??

Uber Simulation Model

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rafayel Arakelyan

Rafayel Arakelyan

More from Medium

Device Farm for Mobile application Testing (Part 1- Setup)

API request/response tracking while performing black box testing over apps on non rooted Android.

Git and Mongo DB

File System Of Android