设计模式之接口分离原则
定义:
以上截图的uml图已添加注释,代码实现如下:
public class Segregation {
public static void main(String[] args) {
A a = new A();
B b = new B();
C c = new C();
D d = new D();
a.useInterface1(b);
a.useInterface2(b);
a.useInterface3(b);
c.useInterface1(d);
c.useInterface4(d);
c.useInterface5(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 {
public void useInterface1(Interface1 interface1){
interface1.operation1();
}
public void useInterface2(Interface1 interface1){
interface1.operation2();
}
public void useInterface3(Interface1 interface1){
interface1.operation3();
}
}
class C {
public void useInterface1(Interface1 interface1){
interface1.operation1();
}
public void useInterface4(Interface1 interface1){
interface1.operation4();
}
public void useInterface5(Interface1 interface1){
interface1.operation5();
}
}
以上代码的问题在于A类只是用接口中的1,2,3方法,C类只使用接口中的1,4、5方法。所以B类其实只要实现1,2,3方法。D类只要实现1,4,5方法。
解决办法:
修改后的类图:
接口需要拆分成3个接口, 代码实现如下:
public class Segregation { public static void main(String[] args) { A a = new A(); B b = new B(); C c = new C(); D d = new D(); a.useInterface1(b); a.useInterface2(b); a.useInterface3(b); c.useInterface1(d); c.useInterface4(d); c.useInterface5(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实现operation1"); } @Override public void operation2() { System.out.println("B实现operation2"); } @Override public void operation3() { System.out.println("B实现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 useInterface1(Interface1 interface1){ interface1.operation1(); } public void useInterface2(Interface2 interface2){ interface2.operation2(); } public void useInterface3(Interface2 interface2){ interface2.operation3(); } } class C { public void useInterface1(Interface1 interface1){ interface1.operation1(); } public void useInterface4(Interface3 interface3){ interface3.operation4(); } public void useInterface5(Interface3 interface3){ interface3.operation5(); } }