二、接口隔离原则
基本介绍:
客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上
应用实例:
例1、
public class Segregation1 {
public static void main(String[] args) {
A a = new A();
a.depend1(new B());
a.depend2(new B());
a.depend3(new B());
C c = new C();
c.depend1(new D());
c.depend4(new D());
c.depend5(new D());
}
}
interface Interface1{
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
class B implements Interface1 {
@Override
public void operation1() {
System.out.println("B实现了operation1");
}
@Override
public void operation2() {
System.out.println("B实现了operation2");
}
@Override
public void operation3() {
System.out.println("B实现了operation3");
}
@Override
public void operation4() {
System.out.println("B实现了operation4");
}
@Override
public void operation5() {
System.out.println("B实现了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{
//类 A 使用了B中的 1,2,3 方法
public void depend1(Interface1 i){
i.operation1();
}
public void depend2(Interface1 i){
i.operation2();
}
public void depend3(Interface1 i){
i.operation3();
}
}
class C{
//类 C 使用了D中的 1,4,5 方法
public void depend1(Interface1 i){
i.operation1();
}
public void depend4(Interface1 i){
i.operation4();
}
public void depend5(Interface1 i){
i.operation5();
}
}
结论:类 B 和 D 都实现了接口 Interface1,而类 A 和 C 并没有全部调用接口实现类的方法,违反了接口隔离原则
例2、
public class Segregation2 {
public static void main(String[] args) {
A a = new A();
a.depend1(new B());
a.depend2(new B());
a.depend3(new B());
C c = new C();
c.depend1(new D());
c.depend4(new D());
c.depend5(new D());
}
}
interface Interface1{
void operation1();
}
interface Interface2{
void operation2();
void operation3();
}
interface Interface3{
void operation4();
void operation5();
}
class B implements Interface1,Interface2{
@Override
public void operation1() {
System.out.println("B实现了interface1 中的 operation1");
}
@Override
public void operation2() {
System.out.println("B实现了interface2 中的 operation2");
}
@Override
public void operation3() {
System.out.println("B实现了interface2 中的 operation3");
}
}
class D implements Interface1,Interface3{
@Override
public void operation1() {
System.out.println("D实现了interface1 中的 operation1");
}
@Override
public void operation4() {
System.out.println("D实现了interface3 中的 operation4");
}
@Override
public void operation5() {
System.out.println("D实现了interface3 中的 operation5");
}
}
class A{
//类 A 使用了B中的 Interface1,Interface2 方法
public void depend1(Interface1 i){
i.operation1();
}
public void depend2(Interface2 i){
i.operation2();
}
public void depend3(Interface2 i){
i.operation3();
}
}
class C{
//类 C 使用了D中的 Interface1,Interface3 方法
public void depend1(Interface1 i){
i.operation1();
}
public void depend4(Interface3 i){
i.operation4();
}
public void depend5(Interface3 i){
i.operation5();
}
}
结论:接口最小化,没有多余的接口方法,实现了隔离原则
总结
根据代码的实际情况将接口中的方法拆分N个接口,使接口最小化,保证接口隔离原则