Interface Segregation Principle,ISP
。
design-1.5-0
|——src
|——main
|--java
|--com.lino.design
|--HeroHouYi.java
|--HeroLianPo.java
|--ISkill.java
|——test
|--java
|--com.lino.design.test
|--ApiTest.java
ISkill.java
package com.lino.design;
/**
* @description: 英雄技能接口
*/
public interface ISkill {
/**
* 射箭
*/
void doArchery();
/**
* 隐袭
*/
void doInvisible();
/**
* 沉默
*/
void doSilent();
/**
* 眩晕
*/
void doVertigo();
}
HeroHouYi.java
package com.lino.design;
/**
* @description: 英雄:后裔
*/
public class HeroHouYi implements ISkill {
@Override
public void doArchery() {
System.out.println("后裔的灼日之矢");
}
@Override
public void doInvisible() {
System.out.println("后裔的隐身技能");
}
@Override
public void doSilent() {
System.out.println("后裔的沉默技能");
}
@Override
public void doVertigo() {
// 无此技能的实现
}
}
HeroLianPo.java
package com.lino.design;
/**
* @description: 英雄:廉颇
*/
public class HeroLianPo implements ISkill {
@Override
public void doArchery() {
// 无此技能的实现
}
@Override
public void doInvisible() {
System.out.println("廉颇的隐身技能");
}
@Override
public void doSilent() {
System.out.println("廉颇的沉默技能");
}
@Override
public void doVertigo() {
System.out.println("廉颇的眩晕技能");
}
}
ApiTest.java
@Test
public void test_ISKill() {
// 后裔
HeroHouYi heroHouYi = new HeroHouYi();
heroHouYi.doArchery();
// 廉颇
HeroLianPo heroLianPo = new HeroLianPo();
heroLianPo.doInvisible();
}
测试结果
后裔的灼日之矢
廉颇的隐身技能
design-1.5-1
|——src
|——main
|--java
|--com.lino.design
|--impl
| |-HeroHouYi.java
| |-HeroLianPo.java
|--ISkillArchery.java
|--ISkillInvisible.java
|--ISkillSilent.java
|--ISkillVertigo.java
|——test
|--java
|--com.lino.design.test
|--ApiTest.java
ISkillArchery
ISkillInvisible
ISkillSilent
ISkillVertigo
ISkillArchery.java
package com.lino.design;
/**
* @description: 射箭技能
*/
public interface ISkillArchery {
/**
* 射箭
*/
void doArchery();
}
ISkillInvisible.java
package com.lino.design;
/**
* @description: 影袭技能
*/
public interface ISkillInvisible {
/**
* 隐袭
*/
void doInvisible();
}
ISkillSilent.java
package com.lino.design;
/**
* @description: 沉默技能
*/
public interface ISkillSilent {
/**
* 沉默
*/
void doSilent();
}
ISkillSilent.java
package com.lino.design;
/**
* @description: 晕眩技能
*/
public interface ISkillVertigo {
/**
* 眩晕
*/
void doVertigo();
}
HeroHouYi.java
package com.lino.design.impl;
import com.lino.design.ISkillArchery;
import com.lino.design.ISkillInvisible;
import com.lino.design.ISkillSilent;
/**
* @description: 英雄:后裔
*/
public class HeroHouYi implements ISkillArchery, ISkillInvisible, ISkillSilent {
@Override
public void doArchery() {
System.out.println("后裔的灼日之矢");
}
@Override
public void doInvisible() {
System.out.println("后裔的隐身技能");
}
@Override
public void doSilent() {
System.out.println("后裔的沉默技能");
}
}
HeroLianPo.java
package com.lino.design.impl;
import com.lino.design.ISkillInvisible;
import com.lino.design.ISkillSilent;
import com.lino.design.ISkillVertigo;
/**
* @description: 英雄:廉颇
*/
public class HeroLianPo implements ISkillInvisible, ISkillSilent, ISkillVertigo {
@Override
public void doInvisible() {
System.out.println("廉颇的隐身技能");
}
@Override
public void doSilent() {
System.out.println("廉颇的沉默技能");
}
@Override
public void doVertigo() {
System.out.println("廉颇的眩晕技能");
}
}
ApiTest.java
@Test
public void test_ISKill() {
// 后裔
HeroHouYi heroHouYi = new HeroHouYi();
heroHouYi.doArchery();
// 廉颇
HeroLianPo heroLianPo = new HeroLianPo();
heroLianPo.doInvisible();
}
测试结果
后裔的灼日之矢
廉颇的隐身技能