1. 基本介绍:
- 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上
2. 实际用例
- 创建接口interface1,其中有5个未实现的方法 operation1~5
- 创建类A实现interface1中的5个接口
- 创建类B实现interface1中的5个接口
- 创建类C,类C中需要依赖interface1中的1,2,3接口
- 创建类D,类D中需要依赖interface1中的1,3,4接口
- 接下来,我们发现,不论是类C还是类D,调用interface的实现类A,B时,都会有用不到的方法
- 例如类C,只需要1,2,3这三个接口,但是实现类A中却实现了1~5所有接口,这样就违背了接口隔离原则
public class Segregation {
public static void main(String[] args) {
C c = new C();
A a = new A();
c.depend1(a);
c.depend2(a);
c.depend3(a);
D d = new D();
B b = new B();
d.depend1(b);
d.depend4(b);
d.depend5(b);
}
}
interface interface1{
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
class A implements interface1{
@Override
public void operation1() {
System.out.println("A 实现了operation1");
}
@Override
public void operation2() {
System.out.println("A 实现了operation2");
}
@Override
public void operation3() {
System.out.println("A 实现了operation3");
}
@Override
public void operation4() {
System.out.println("A 实现了operation4");
}
@Override
public void operation5() {
System.out.println("A 实现了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 C{
public void depend1(interface1 interface1){
interface1.operation1();
}
public void depend2(interface1 interface1){
interface1.operation2();
}
public void depend3(interface1 interface1){
interface1.operation3();
}
}
class D{
public void depend1(interface1 interface1){
interface1.operation1();
}
public void depend4(interface1 interface4){
interface4.operation4();
}
public void depend5(interface1 interface5){
interface5.operation5();
}
}
3. 用例改进
- 将interface1,通过细粒度拆分成interface1,interface2,interface3
- A实现interface1与interface2
- B实现interface1与interface3
- 这样就可以将多余的方法剔除出去,C在使用A调用时,A里面只实现了1,2,3这三个方法,没有多余的4,5这两个方法,这样就实现了接口隔离原则
public class Segregation{
public static void main(String[] args) {
C c = new C();
A a = new A();
c.depend1(a);
c.depend2(a);
c.depend3(a);
D d = new D();
B b = new B();
d.depend1(b);
d.depend4(b);
d.depend5(b);
}
}
interface interface1{
void operation1();
}
interface interface2{
void operation2();
void operation3();
}
interface interface3{
void operation4();
void operation5();
}
class A implements interface1 ,interface2{
@Override
public void operation1() {
System.out.println("A 实现了operation1");
}
@Override
public void operation2() {
System.out.println("A 实现了operation2");
}
@Override
public void operation3() {
System.out.println("A 实现了operation3");
}
}
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 C{
public void depend1(interface1 interface1){
interface1.operation1();
}
public void depend2(interface2 interface1){
interface1.operation2();
}
public void depend3(interface2 interface1){
interface1.operation3();
}
}
class D{
public void depend1(interface1 interface1){
interface1.operation1();
}
public void depend4(interface3 interface4){
interface4.operation4();
}
public void depend5(interface3 interface5){
interface5.operation5();
}
}