接口隔离原则
基本介绍
客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上。
接口Interface1中有5个方法,
类A需要通过接口Interface1去掉用B类的第1,2,3个方法,
类C需要通过接口Interface1去调用D类的第1,4,5个方法,
这个时候B,D两个类必须重写完Interface1中的所有方法,这样会重写一些并没有使用的方法。
按照隔离原则,Interface1应该拆分成几个更小的独立的接口,供A类和C类使用。
问题代码示例
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("B实现了 operation5");
}
}
class A {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface1 i) {
i.operation2();
}
public void depend3(Interface1 i) {
i.operation3();
}
}
class C {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface1 i) {
i.operation4();
}
public void depend5(Interface1 i) {
i.operation5();
}
}
使用接口隔离分离出最小依赖接口
interface Interface1 {
void operation1();
}
interface Interface2 {
void operation2();
void operation3();
}
interface Interface3 {
void operation4();
void operation5();
}
class B implements Interface1,Interface3{
@Override
public void operation1() {
System.out.println("B实现了 operation1");
}
@Override
public void operation4() {
System.out.println("B实现了 operation4");
}
@Override
public void operation5() {
System.out.println("B实现了 operation5");
}
}
class D implements Interface1,Interface2{
@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实现了 operation2");
}
}
class A {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface2 i) {
i.operation2();
}
public void depend3(Interface2 i) {
i.operation3();
}
}
class C {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface3 i) {
i.operation4();
}
public void depend5(Interface3 i) {
i.operation5();
}
}
按照接口隔离模式优化后的代码
public class Segregation1 {
public static void main(String[] args) {
A a = new A();
a.depend1(new D());
a.depend2(new D());
a.depend3(new D());
C c =new C();
c.depend1(new B());
c.depend4(new B());
c.depend5(new B());
}
}
interface Interface1 {
void operation1();
}
interface Interface2 {
void operation2();
void operation3();
}
interface Interface3 {
void operation4();
void operation5();
}
class B implements Interface1,Interface3{
@Override
public void operation1() {
System.out.println("B实现了 operation1");
}
@Override
public void operation4() {
System.out.println("B实现了 operation4");
}
@Override
public void operation5() {
System.out.println("B实现了 operation5");
}
}
class D implements Interface1,Interface2{
@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实现了 operation2");
}
}
class A {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface2 i) {
i.operation2();
}
public void depend3(Interface2 i) {
i.operation3();
}
}
class C {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface3 i) {
i.operation4();
}
public void depend5(Interface3 i) {
i.operation5();
}
}
总结
- 主要是根据方法调用之间的关系,把一些接口再次分离出来更小的接口,尽量把用不到的方法拆分开,说白了就是拆接口,不然会出现根本不会使用的方法却还必须在实现类重写