Home > Back-end >  Why say with interface, switch implementation class doesn't have to change the code?
Why say with interface, switch implementation class doesn't have to change the code?

Time:05-15

For example:
Service now into a "myDao" :
 
Public class MyServiceImpl {

@ the Resource (" myDao ")
Private MyDao MyDao;

}
}

Now and then switch to the new "myDao2", is not to change the code?
 
Public class MyServiceImpl {

@ the Resource (" myDao2 ")
Private MyDao MyDao;

}
}

CodePudding user response:

This type configuration code, change the effect is very small, of course you can make it do not change the code, pure type configuration, also can do it, just do not have what is necessary

CodePudding user response:

reference 1st floor maradona1984 response:
this type configuration code, change the effect is very small, of course you can make it do not change the code, pure type configuration, also can do it, just do not have what necessary

That injection interface can is can not directly into the implementation class, too, to switch the new implementation class is only to change a line of the name of the class

CodePudding user response:

A, the first way: interface defining methods do (), the implementation class B, C, D, implement methods do (), the caller only ever need to introduce private aa, through XML or annotation configuration implementation class B, C, D can be one of them
Second way: X, Y, Z, have their own method of doX (), doY (), doZ (), customer want to use this month X method processing business, hope next month processing business, with the method of the Y again next month with Z method processing business, you need to change a code, repackage, so each month downtime, deployment, restart, if the business process changes frequently, be disastrous for the customer, so this is the meaning of the interface is, foreign unified method, the internal dynamic decide to implement party, so are the benefits of 1, exposed API unification, including the method name, into 2, dynamic configuration business implementation, 3, B, C, D developers to focus on their own business,

CodePudding user response:

refer to the second floor Wuicy response:
Quote: refer to 1st floor maradona1984 response:
this type configuration code, change the effect is very small, of course you can make it do not change the code, pure type configuration, also can do it, just do not have what necessary

That injection interface can is can not directly into the implementation class, too, to switch the new implementation class is only to change a line of the name of the class

Into the class? Interface can guarantee consistent methods, but can't ah, syntax, from compile-time to limit, can't go to limit the class from the language level

CodePudding user response:

Maradona1984
reference 4 floor response:
Quote: refer to the second floor Wuicy response:
Quote: refer to 1st floor maradona1984 response:
this type configuration code, change the effect is very small, of course you can make it do not change the code, pure type configuration, also can do it, just do not have what necessary

That injection interface can is can not directly into the implementation class, too, to switch the new implementation class is only to change a line of the name of the class

Into the class? Interface can guarantee consistent methods, but can't ah, syntax, from compile-time to limit, class from the language level can not be to limit the

I mean, two implementation class implements an interface, but when injected directly into the implementation class

CodePudding user response:

reference 5 floor Wuicy reply:
Quote: refer to 4th floor maradona1984 response:
Quote: refer to the second floor Wuicy response:
Quote: refer to 1st floor maradona1984 response:
this type configuration code, change the effect is very small, of course you can make it do not change the code, pure type configuration, also can do it, just do not have what necessary

That injection interface can is can not directly into the implementation class, too, to switch the new implementation class is only to change a line of the name of the class

Into the class? Interface can guarantee consistent methods, but can't ah, syntax, from compile-time to limit, class from the language level can not be to limit the

I mean, two implementation class implements an interface, but when injected directly into the implementation class

Is ok in theory, but why, you modify the injected bean name is equivalent to choose a different beans, why to change the type? As a programmer, less change the probability of a line of a separate bug less.
And you now that defines the interface, so why should choose to define a member variable implementation class, this is quite strange, code is not just run results, also let a person can understand the intentions of this code, can reduce the workload of others, others see an interface definition here, it revealed the author's intention, there is extensible, if you write a put the implementation class, even if others see the class interface, it also need more communication/testing to ensure that the intention of the code, and not as clear as the interface.
  • Related