1、定义
客户端不应该依赖它不需要的接口;一个类对另一个类的依赖应该建立在最小的接口上。
2、实现
(1)定义脸蛋、身材美女接口
interface IGoodBodyGirl {
void goodLooking();
void niceFigure();
}
(2)定义气质美女接口
interface IGreatTemperamentGirl {
void greatTemperament();
}
(3)定义星探抽象类
abstract class AbstractSearcher {
abstract void show();
}
(4)实现美女类
public class PettyGirl implements IGoodBodyGirl, IGreatTemperamentGirl {
private String name;
public PettyGirl(String name) {
this.name = name;
}
@Override
public void goodLooking() {
Log.d("JimmyZhang", name + "---脸蛋很漂亮");
}
@Override
public void greatTemperament() {
Log.d("JimmyZhang", name + "---气质非常好");
}
@Override
public void niceFigure() {
Log.d("JimmyZhang", name + "---身材非常棒");
}
}
(5)星探实现类
class Searcher extends AbstractSearcher {
public PettyGirl pettyGirl;
public Searcher(PettyGirl pettyGirl) {
this.pettyGirl = pettyGirl;
}
@Override
void show() {
pettyGirl.goodLooking();
pettyGirl.niceFigure();
pettyGirl.greatTemperament();
}
}
(7)测试实现
class Client {
public static void main(String[] args) throws IOException {
PettyGirl pettyGirl = new PettyGirl("景甜");
AbstractSearcher searcher = new Searcher(pettyGirl);
searcher.show();
}
}
3、接口隔离原则的规范约束
(1)接口要尽量小;
(2)接口要高内聚;
(3)接口设计是有限度的。