Dependency Inversion Principle: How Google Developers write code

What is Dependency Inversion Principle?

Can you see what’s wrong with this code?

import java.util.Arrays;
import java.util.List;
// High Level Module
class ProductCatalog {

public void listAllProducts() {

SQLProductRepository sqlProductRepository = new SQLProductRepository();

List<String> allProductNames = sqlProductRepository.getAllProductNames();

// Display product names

}

}
// Low Level Module
class SQLProductRepository {

public List<String> getAllProductNames() {
return Arrays.asList("soap", "toothpaste", "shampoo");
}

}

What is Abstraction?

class Benz  {

public void drive() {

}

}

class CarUtil {

public static void drive(Benz benz) {
benz.drive();
}

}
interface Car {
public void drive();
}

class Benz implements Car {

@Override
public void drive() {


}

}

class Tesla implements Car {

@Override
public void drive() {

}

}

class CarUtil {

public static void drive(Car car) {
car.drive();
}

}

Refactoring Previous Code with Abstraction

import java.util.Arrays;
import java.util.List;

// High Level Module
class ProductCatalog {

public void listAllProducts() {

// High Level Module depends on Abstraction
ProductRepository productRepository = new SQLProductRepository();

List<String> allProductNames = productRepository.getAllProductNames();

// Display product names

}

}
interface ProductRepository {

List<String> getAllProductNames();

}

// Low Level Module
class SQLProductRepository implements ProductRepository {

public List<String> getAllProductNames() {
return Arrays.asList("soap", "toothpaste", "shampoo");
}

}

What you have achieved now

Before refactoring
After Refactoring

One strep forward, Dependency Injection

import java.util.Arrays;
import java.util.List;

class ProductCatalog {

private ProductRepository productRepository;

public ProductCatalog(ProductRepository productRepository) {
this.productRepository = productRepository;
}

public void listAllProducts() {

List<String> allProductNames = productRepository.getAllProductNames();

// Display product names

}

}

interface ProductRepository {

List<String> getAllProductNames();

}

class SQLProductRepository implements ProductRepository {

public List<String> getAllProductNames() {
return Arrays.asList("soap", "toothpaste", "shampoo");
}

}

class EcommerceApplication {

public static void main(String[] args) {

ProductRepository productRepository = new SQLProductRepository();

ProductCatalog productCatalog = new ProductCatalog(productRepository);

productCatalog.listAllProducts();

}

}

Conclusion

  • “High-level modules should not depend on low-level modules. Both should depend on abstractions.”
  • “Abstractions should not depend on details. Details should depend on abstractions.”

--

--

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
KD Knowledge Diet

KD Knowledge Diet

2.4K Followers

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!