Home > Back-end >  Why the dependency injection can reduce the coupling than new objects directly, I always don't
Why the dependency injection can reduce the coupling than new objects directly, I always don't

Time:05-24

Object will be invoked inside B object such as A, B object to invoke the C...

ClassA {
Public void mathod1 () {

Class B b1=new ClassB ();
B1. Mathodb1 ();
}
}

ClassB {
Public void mathodb1 () {

Class C C1=new ClassC ();
C1. Mathodc1 ();
}
}

ClassC {
Public void mathodc1 () {
.
}
}
I want to use ClassA mathod1, directly (new ClassA ()). Mathod1 () have what not good, don't want to inject a ClassA, don't bother

CodePudding user response:

One thousand one day, your ClassA is no longer meet the needs of the current version, and keep ClassA, in order to use the old instincts, how do you do?

CodePudding user response:

This is not in polymorphism,
Public class ClassA2 extend ClassA {
@ override
Public void mathod1 () {
.
}
}

}
The old version directly (
ClassA a1=new ClassA ();
A1. Mathod1 ()
The new version
ClassA a1=new ClassA2 ();
A1. Mathod1 ()
Feeling is a polymorphic problem, not injection
  • Related