1, define,

As usual, let’s first look at the definition of dependency inversion.

Abstraction should not depend on details, details should depend on abstractions. In other words, program for the interface, not the implementation.

Why do you say that?

Because the details are volatile, very unstable. In many cases, the details change as the requirements change.

Abstract is relatively stable, abstraction is extracted from many things common, essential characteristics, is more difficult to be changed.

Therefore, we must choose to program for abstractions rather than details.

In Java, abstraction refers to abstract classes or interfaces; The details represent the concrete implementation.

Programming interfaces is fundamental to writing robust code, and is the basic quality of a good programmer.

2, meaning

1. High-level modules should not depend on low-level modules; both should depend on their abstractions

Whether it is a high-level module or a low-level module, all belong to the concrete implementation, belong to the details, and the details certainly cannot depend on each other.

They should all rely on abstractions.

2. Abstraction should not depend on details

Neither should depend on detail, especially abstraction should not depend on detail.

3. Details should depend on abstraction

All should depend on abstraction, details depend on abstraction, and abstraction depends on abstraction.

3, code,

Declare dependent objects in interface methods

package com.fanqiekt.principle.inversion; /** ** ** @author */ public interface IChef {/** ** ** / void cooking(); }Copy the code

A chef can cook.

package com.fanqiekt.principle.inversion; /** ** ** @author */ public class SiChuanChef implements IChef {@override public void cooking() { System.out.println(" Sichuan cooks, put lots of pepper." ); }}Copy the code

Chef implementation class, sichuan chef, which logic belongs to details, such as Sichuan chefs have their own cooking routines (can not not spicy).

package com.fanqiekt.principle.inversion; /** * @author */ public implements IChef {@override public void cooking() { System.out.println(" Shandong cooks with scallions, ginger and garlic." ); }}Copy the code

Another chef implementation class, Lu chefs, Lu chefs also have their own cooking routines (using Onions, ginger and garlic).

package com.fanqiekt.principle.inversion; Public interface IWaiter {** ** @param chef */ void order(IChef chef); }Copy the code

An abstract interface for waiters, who are required to order food for guests.

The ORDER (IChef Chef) method declares dependent objects in the interface method.

Define the method in the interface and pass in the dependent abstract object as a parameter.

package com.fanqiekt.principle.inversion; Public implements IWaiter{@override public implements IWaiter (IChef chef){ if(chef! =null) { chef.cooking(); }}}Copy the code

The waiter implementation class, ordering is to let the incoming chef do the cooking.

IChef sichuanChef = new SiChuanChef();
IChef shandongChef = new ShanDongChef();
IWaiter waiter = new Waiter();
waiter.order(sichuanChef);
waiter.order(shandongChef);
Copy the code

Take the abstract object as the order method parameter.

Sichuan cooks and Shandong cooksCopy the code

Run result.

Each time a method is called, the dependent object is passed in.

The advantage is flexibility, and you can pass in different dependent objects each time.

The disadvantage is that you need to pass in dependent objects every time.

Constructors pass dependent objects

package com.fanqiekt.principle.inversion; Public interface IWaiter {/** ** ** / void cooking(); }Copy the code

Server interface modification: Remove parameter from cooking method.

package com.fanqiekt.principle.inversion; /** * public implements IWaiter {private IChef chef; /** * public Waiter(IChef chef){this.chef = chef; } @Override public void cooking(){ if(chef! =null) { chef.cooking(); }}}Copy the code

The dependent abstract object is passed in through the constructor.

Waiter waiter1 = new Waiter(sichuanChef);
waiter1.cooking();
Waiter waiter2 = new Waiter(shandongChef);
waiter2.cooking();
Copy the code

Let’s run it and see what happens.

Sichuan cooks and Shandong cooksCopy the code

Dependencies are established when they are first created, which is both a plus and a minus.

The advantage is that it avoids being modified.

The disadvantage is that changing dependencies requires the object to be created all over again.

3. Setter methods pass dependent objects

package com.fanqiekt.principle.inversion; /** * public class implements IWaiter {private IChef chef; public void setChef(IChef chef){ this.chef = chef; } @Override public void cooking(){ if(chef! =null) { chef.cooking(); }}}Copy the code

Assign dependent objects using the set method.

Waiter plan = new Waiter();
plan.setChef(sichuanChef);
plan.cooking();
plan.setChef(shandongChef);
plan.cooking();
Copy the code

Let’s run it and see what happens.

Sichuan cooks and Shandong cooksCopy the code

Setters can replace dependent objects without passing them in every time a method is called.

3, strengths,

Let’s summarize several advantages of the dependency inversion principle.

Risk reduction relies on abstraction, greatly improving code robustness, and risk is naturally reduced.

Easy maintenance easy extension depends on abstraction, only then can there be a framework, based on the framework, extension will be more convenient, maintenance is also more convenient.

Increase the speed of development by setting the abstraction structure so that it can be developed in parallel without too much interference from other people’s schedules.

Hip-hop said

Next, enjoy an original song that relies on the inversion principle.

Hip Hop says: Reliance on inversion principle Lazy people need to rely on chefs to cook a meal for me, don't care what he does. Sichuan or Hui only rely on chefs interface method how beautiful it is, otherwise it is too much trouble to modify it involves too much. Set method construction method interface method can make a configuration of dependency inversion principle is such a thing abstract does not depend on details Detail depends on abstraction at the top level, not at the bottom level, but at the bottom level it should depend on abstraction and interface oriented programming with that in mind so that the code path is long enough that it's easy to extend, easy to maintain, reduce risk and speed up developmentCopy the code

Idle no matter to listen to music, knowledge has filled the brain to;

Learning new ways to review, wearing headphones is a big deal.