定义:
以上截图的 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();
- }
- }
来源: http://www.bubuko.com/infodetail-3438579.html