1 概念
客户端不应该依赖他不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上
2 引入
package study.wyy.design.principle.segregation;
/**
* @author by wyaoyao
* @Description
* @Date 2020/10/13 8:30 下午
*/
public class Segregation1 {
public static void main(String[] args) {
InterFace a = new A();
InterFace b = new B();
C c = new C(a);
c.depend1();
c.depend2();
c.depend3();
D d = new D(b);
d.depend1();
d.depend4();
d.depend5();
}
}
interface InterFace{
void method1();
void method2();
void method3();
void method4();
void method5();
}
class A implements InterFace{
public void method1() {
System.out.println("A 实现了 method1");
}
public void method2() {
System.out.println("A 实现了 method2");
}
public void method3() {
System.out.println("A 实现了 method3");
}
public void method4() {
System.out.println("A 实现了 method4");
}
public void method5() {
System.out.println("A 实现了 method5");
}
}
class B implements InterFace{
public void method1() {
System.out.println("B 实现了 method1");
}
public void method2() {
System.out.println("B 实现了 method2");
}
public void method3() {
System.out.println("B 实现了 method3");
}
public void method4() {
System.out.println("B 实现了 method4");
}
public void method5() {
System.out.println("B 实现了 method5");
}
}
// C通过接口InterFace 依赖类A
class C {
private final InterFace interFace;
C(InterFace interFace) {
this.interFace = interFace;
}
public void depend1(){
interFace.method1();
}
public void depend2(){
interFace.method2();
}
public void depend3(){
interFace.method3();
}
}
// D 通过接口InterFace 依赖类B
class D {
private final InterFace interFace;
D(InterFace interFace) {
this.interFace = interFace;
}
public void depend1(){
interFace.method1();
}
public void depend4(){
interFace.method4();
}
public void depend5(){
interFace.method5();
}
}
类图
简单介绍:
- 接口Interface定义了5个方法,分别有A,B两个实现类
- 类C通过接口Interface 依赖了类A,使用了1,2,3三个方法
- 类D通过接口Interface 依赖了类B,使用了1,4,5三个方法
问题
Interface对于A,B来说不是最小接口,但是C,D又通过Interface依赖A,B,最终A只用到了接口中的1,2,3三个方法,B只用,3,4,5三个方法,没必要实现接口中的全部方法
3 改进
思路
将Interface这个接口拆成三个接口
package study.wyy.design.principle.segregation.improve;
/**
* @author by wyaoyao
* @Description
* @Date 2020/10/13 8:30 下午
*/
public class Segregation2 {
public static void main(String[] args) {
InterFace1 interFaceA1 = new A();
InterFace2 interFaceA2 = new A();
InterFace1 interFaceB1 = new B();
InterFace3 interFaceB3 = new B();
C c = new C(interFaceA1, interFaceA2);
D d = new D(interFaceB1, interFaceB3);
c.depend1();
c.depend2();
c.depend3();
d.depend1();
d.depend4();
d.depend5();
}
}
interface InterFace1{
void method1();
}
interface InterFace2{
void method2();
void method3();
}
interface InterFace3{
void method4();
void method5();
}
class A implements InterFace1,InterFace2{
public void method1() {
System.out.println("A 实现了 method1");
}
public void method2() {
System.out.println("A 实现了 method2");
}
public void method3() {
System.out.println("A 实现了 method3");
}
}
class B implements InterFace1,InterFace3{
public void method1() {
System.out.println("B 实现了 method1");
}
public void method4() {
System.out.println("B 实现了 method4");
}
public void method5() {
System.out.println("B 实现了 method5");
}
}
// C通过接口InterFace 依赖类A
class C {
private final InterFace1 interFace1;
private final InterFace2 interFace2;
C(InterFace1 interFace1, InterFace2 interFace2) {
this.interFace1 = interFace1;
this.interFace2 = interFace2;
}
public void depend1(){
interFace1.method1();
}
public void depend2(){
interFace2.method2();
}
public void depend3(){
interFace2.method3();
}
}
// D 通过接口InterFace 依赖类B
class D {
private final InterFace1 interFace1;
private final InterFace3 interFace3;
D(InterFace1 interFace1, InterFace3 interFace3) {
this.interFace1 = interFace1;
this.interFace3 = interFace3;
}
public void depend1(){
interFace1.method1();
}
public void depend4(){
interFace3.method4();
}
public void depend5(){
interFace3.method5();
}
}