設計模式之介面隔離原則
阿新 • • 發佈:2021-02-09
客戶端不應該依賴它不需要的介面,即一個類對另外一個類依賴應該建立在最小的介面上
package com.yang.segregation;
public class Segretion {
public static void main(String[] args) {
}
}
interface Interface{
void a();
void b();
void c();
}
class B implements Interface{
@Override
public void a() {
// TODO Auto-generated method stub
System.out.println("B實現了a");
}
@Override
public void b() {
// TODO Auto-generated method stub
System.out.println("B實現了B");
}
@Override
public void c() {
// TODO Auto-generated method stub
System.out.println("B實現了C");
}
}
class C implements Interface{
@Override
public void a() {
// TODO Auto-generated method stub
System.out.println("C實現了a");
}
@Override
public void b() {
// TODO Auto-generated method stub
System.out.println("C實現了B");
}
@Override
public void c() {
// TODO Auto-generated method stub
System.out.println("C實現了C" );
}
}
class A{
public void depend(Interface i) {
i.a();
}
public void depend1(Interface i) {
i.b();
}
public void depend2(Interface i) {
i.c();
}
}
class C1{
public void depend(Interface i) {
i.a();
}
public void depend1(Interface i) {
i.b();
}
public void depend2(Interface i) {
i.c();
}
}