目录
1、基本介绍
客户端不依赖它不需要的接口,即一个类对另一个的依赖应该建立在最小的接口上。(只是看文字比较难理解,直接看下面的应用实例)
2、应用实例
2.1、需求
有一个类A,它通过Interface1来依赖(使用)类C中的三个方法:operation1()、operation2()、operation3();
有一个类B,它通过Interface1来依赖(使用)类D中的三个方法:operation1()、operation4()、operation5();
编写代码实现其细节;
2.2、传统方式解决需求
2.2.1、类图
2.2.2、代码实现
interface Interface1{
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
class C implements Interface1{
@Override
public void operation1() {
System.out.println("C 中实现了operation1");
}
@Override
public void operation2() {
System.out.println("C 中实现了operation2");
}
@Override
public void operation3() {
System.out.println("C 中实现了operation3");
}
@Override
public void operation4() {
System.out.println("C 中实现了operation4");
}
@Override
public void operation5() {
System.out.println("C 中实现了operation5");
}
}
class D implements Interface1{
@Override
public void operation1() {
System.out.println("D 中实现了operation1");
}
@Override
public void operation2() {
System.out.println("D 中实现了operation2");
}
@Override
public void operation3() {
System.out.println("D 中实现了operation3");
}
@Override
public void operation4() {
System.out.println("D 中实现了operation4");
}
@Override
public void operation5() {
System.out.println("D 中实现了operation5");
}
}
class A{
public void depend1(Interface1 interface1){
interface1.operation1();
}
public void depend2(Interface1 interface1){
interface1.operation2();
}
public void depend3(Interface1 interface1){
interface1.operation3();
}
}
class B{
public void depend1(Interface1 interface1){
interface1.operation1();
}
public void depend4(Interface1 interface1){
interface1.operation4();
}
public void depend5(Interface1 interface1){
interface1.operation5();
}
}
2.2.3、传统方式总结
这样设计达到了需求效果,但是违反了接口隔离原则。
A类只使用C类的1,2,3方法,所以C类没必要实现4,5方法;
同样,B类只使用D类的1,4,5方法,所以D类没必要实现2,3方法;
改进思路:将Interface1拆分成多个接口,使得程序遵循接口隔离原则;
2.3、遵循接口隔离原则解决需求
2.3.1、类图
2.3.2、代码实现
interface Interface1{
void operation1();
}
interface Interface2{
void operation2();
void operation3();
}
interface Interface3{
void operation4();
void operation5();
}
class C implements Interface1,Interface2{
@Override
public void operation1() {
System.out.println("C 中实现了operation1");
}
@Override
public void operation2() {
System.out.println("C 中实现了operation2");
}
@Override
public void operation3() {
System.out.println("C 中实现了operation3");
}
}
class D implements Interface1,Interface3{
@Override
public void operation1() {
System.out.println("D 中实现了operation1");
}
@Override
public void operation4() {
System.out.println("D 中实现了operation4");
}
@Override
public void operation5() {
System.out.println("D 中实现了operation5");
}
}
class A{
public void depend1(Interface1 interface1){
interface1.operation1();
}
public void depend2(Interface2 interface2){
interface2.operation2();
}
public void depend3(Interface2 interface2){
interface2.operation3();
}
}
class B{
public void depend1(Interface1 interface1){
interface1.operation1();
}
public void depend4(Interface3 interface3){
interface3.operation4();
}
public void depend5(Interface3 interface3){
interface3.operation5();
}
}
2.3.3、遵循接口隔离原则解决问题总结
为了达到一个类对另一个的依赖应该建立在最小的接口上这个要求,我们将原来的一个大的接口分散成几个小的接口。
用一个比较形象的比喻:
- 去面馆吃面,拌面的调料,有:盐、油、酱、醋、香菇;
- 第一种方式(传统方式):Interface1就是5种调料。C类和D类是Interface1的实现类,里面也必须有5种调料。但是我拌面的时候,不一定5种调料都需要,假如我只需要盐和油,那么剩下的三种调料就是多余的。不仅是多余的,还有可能带来问题。假如明天,老板把香菇换成了青菜,那么意味着C类和D类也要跟着变化。。。本来我拌面只是需要盐和油,香菇换不换青菜跟我没有半点关系,为什么还要改变呢?这就是问题。
- 第二种方式(遵循接口隔离原则):将5种调料分开,我想要加哪个就加哪个,就算老板将其中一种换了(比如香菇换青菜),如果我还是只需要加油和盐,那么香菇换青菜就和我没关系,我也没必要改变。这样类与类之间的耦合性也降低了。
这个比喻纯属个人理解,不知道是否恰当,有大神看到有问题请联系我。