一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。
课程接口 ICourse
public interface ICourse {
Integer getId();
String getName();
Double getPrice();
}
Java 课程 JavaCourse
public class JavaCourse implements ICourse {
private Integer id;
private String name;
private Double price;
public JavaCourse(Integer id, String name, Double price) {
this.id = id;
this.name = name;
this.price = price;
}
public Integer getId() {
return this.id;
}
public String getName() {
return this.name;
}
public Double getPrice() {
return this.price;
}
}
Java 打折课程 JavaDiscountCourse
public class JavaDiscountCourse extends JavaCourse {
public JavaDiscountCourse(Integer id, String name, Double price) {
super(id, name, price);
}
public Double getOriginPrice() {
return super.getPrice();
}
@Override
public Double getPrice() {
return super.getPrice() * 0.8;
}
}
测试类 Test
public class Test {
public static void main(String[] args) {
ICourse iCourse = new JavaDiscountCourse(96, "Java从零到企业级电商开发", 348d);
JavaDiscountCourse javaCourse = (JavaDiscountCourse) iCourse;
System.out.println("课程ID: " + javaCourse.getId() + " 课程名字: " + javaCourse.getName() + "课程原价: " + javaCourse.getOriginPrice() + " 课程价格: " + javaCourse.getPrice());
}
}
高层模块不应该依赖底层模块,二者都应该依赖其抽象
课程 ICourse
public interface ICourse {
void studyCourse();
}
Java 课程 JavaCourse
public class JavaCourse implements ICourse {
public void studyCourse() {
System.out.println("Yuu 正在学习 Java 课程");
}
}
FE 课程 FECourse
public class FECourse implements ICourse {
public void studyCourse() {
System.out.println("Yuu 正在学习 FE 课程");
}
}
Python 课程 PythonCourse
public class PythonCourse implements ICourse {
public void studyCourse() {
System.out.println("Yuu 正在学习 Python 课程");
}
}
Yuu
public class Yuu {
private ICourse iCourse;
public void setiCourse(ICourse iCourse) {
this.iCourse = iCourse;
}
public void studyImoocCourse() {
iCourse.studyCourse();
}
}
Test
public class Test {
public static void main(String[] args) {
Yuu yuu = new Yuu();
yuu.setiCourse(new JavaCourse());
yuu.studyImoocCourse();
yuu.setiCourse(new FECourse());
yuu.studyImoocCourse();
yuu.setiCourse(new PythonCourse());
yuu.studyImoocCourse();
}
}
不要存在多余一个导致类变更的原因
FlyBird
public class FlyBird {
public void mainMoveMode(String birdName) {
System.out.println(birdName + "用翅膀飞");
}
}
WalkBird
public class WalkBird {
public void mainMoveMode(String birdName) {
System.out.println(birdName + "用脚走");
}
}
Test
public class Test {
public static void main(String[] args) {
FlyBird flyBird = new FlyBird();
flyBird.mainMoveMode("大雁");
WalkBird walkBird = new WalkBird();
walkBird.mainMoveMode("鸵鸟");
}
}
用多个专门的接口,而不使用单一的总接口,客户端不应该依赖它不需要的接口
IEatAnimalAction
public interface IEatAnimalAction {
void eat();
}
IFlyAnimalAction
public interface IFlyAnimalAction {
void fly();
}
ISwimAction
public interface ISwimAction {
void swim();
}
Bird
public class Bird implements IEatAnimalAction, IFlyAnimalAction {
public void eat() {
}
public void fly() {
}
}
Dog
public class Dog implements ISwimAction, IEatAnimalAction {
public void eat() {
}
public void swim() {
}
}
一个对象应该对其他对象保持最少的了解。又叫最少知道原则
朋友
: 出现在成员变量、方法的输入、输出参数中的类称为成员朋友类,而出现在方法体内部的类不属于朋友类。Boss
public class Boss {
public void commandCheckNumber(TeamLeader teamLeader) {
teamLeader.checkNumberOfCourse();
}
}
Course
public class Course {
}
TeamLeader
public class TeamLeader {
public void checkNumberOfCourse() {
List courseList = new ArrayList();
for (int i = 0; i < 20; i++) {
courseList.add(new Course());
}
System.out.println("在线课程的数量是: " + courseList.size());
}
}
Test
public class Test {
public static void main(String[] args) {
Boss boss = new Boss();
TeamLeader teamLeader = new TeamLeader();
boss.commandCheckNumber(teamLeader);
}
}