What is a Utility Token?

As a result of the many Initial Coin Offers (ICO) that are emerging in the economic panorama, we have a term that still causes some confusion: The TOKEN of Utility. It is clear that many startups can…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




The Dependency Inversion Principle

This last principle has a lot to do when designing software and its responsibilities. Large software comes in modules, many times as stated in many of the previous principles, the developers try to avoid tight cohesion between them, but, this is not the case most of the time.

This principle sets that high-level modules should not depend on the low-level modules, and both should depend on code abstractions, I see this as a middlemen component.

Abstractions should not depend on details, the opposite.

High-level modules refer to the major business functions, of course, this will depend on others. Low-level modules handle the implementation details

In the following example, a SQL catalog and repository, the ProductCatalog is the high-level module, and the SQLProductRepository class is the low-level module

Here’s the code.

The ProductCatalog class instantiates a SQLProductRepository and calls the method gettAllProductsNames()

And the SQLProductRepository class contains a method the code to run a Select statement and return a list of product list.

Another common example is found on E-commerce sites, the Payment processor is the high-level module and every payment solution will the low-level one.

This of course is very relative, because your code can have a lower module but N modules in a higher position. Again, in this principle, the highest, depends on the lower (and possible lowest) level.

The example above commits a violation of this principle because both classes depend on the product. Both should depend on abstractions, code abstractions.

For this:

This is all about using abstractions.

To sum things up, the ProductCatalog will talk with the ProductRepository and so the SQLProductRepository

The details are dependent on the abstraction, they are strict as per the definition.

Here’s the code

This is the ProductCatalog class.

Here’s the ProductFactory class.

Here’s the ProductRepository class.

And the SQLProductRepository class.

And the Application:

At this point, we are injecting the dependency into the ProductCatalog instead of ProductCatalog worrying about instantiating the dependency. All the required independencies are injected from the outside without worrying about how to resolve dependencies.

Dependency Injection avoids tight coupling, takes it one step ahead, and completely dissociates a class from going out and getting its dependencies instantiated.

There’s another term related to this principle and is the Inversion of Control (IoC). At this point the ProductCatalog have a constructor which took the productRepository object.

The injection happens outside the ProductCatalog , the injection still happens on the MainApplication control flow, this is the main thread execution.

But, what if we want all injections in a separate thread/context. The main control flow is kept isolated from injection by using frameworks.

Many frameworks will take care of injecting the required dependencies of the class instead of doing it yourself.

This is a huge and completely different world.

Add a comment

Related posts:

Four Sentences for Ascension Sunday

As he goes to the Father’s right hand, Jesus commissions his friends to become intimate witnesses to all people. Jesus ascends that our hearts might be oriented, aimed toward the Father — “lift up…

The Nigerian health insurance industry

The health insurance industry of Nigeria is anchored to a 12 year old National health insurance scheme (NHIS) compared to the National Healthcare industry which is 158 years old. Globally adopted…

Can Immigrants have Tax compliance if they win the Powerball?

If you win the Powerball jackpot, you’ll owe taxes on your prize. The federal government will take 25% of your winnings, and your state government will take an additional amount, depending on where…