六大原则
1.开闭原则
核心:对更改封闭,对拓展开放;类模块应该可以扩展,但是不能修改
项目刚开始,业务没理清,可以适当调整,后续最好不要调整接口
优点:提高系统的可复用性和可维护性
缺点:容易引起类爆炸
public interface notify{
boolean notify();
}
public class email implements notify{
boolean notify(){
email notfy
}
}
public interface notifyAccumulate{//新增业务,30分钟内重复不能继续发送,核心接口,对扩展开放,不修改notify
boolean notifyAccumulate(int time,notify bean){}//重复的三十分钟不在发送
}
public class notifyAccumulateImpl implement notifyAccumulate{
boolean notifyAccumulate(int time,notify bean){
//实现定时任务和重复检测,如果定时任务内且不重复调用bean发送
}//重复的三十分钟不在发送
}
2.依赖倒置原则
核心:高层模块不应该依赖于底层模块,二者应该依赖抽象;针对接口编程;抽象不应该依赖细节,细节应该依赖抽象
优点:可以减少类间耦合性,提高系统稳定性,提高代码可读性和可为核心,降低修改程序造成的风险
public intefcace SystemInfo {
public float getCpuInfo(System sys){
sys.getCpuInfo();
}
}
public interface SysOs{//核心重点 中间抽象,也就是依赖抽象
public float getCpuInfo();
}
public class Windows implements SysOs{
public float getCpuInfo(getCpuInfo);
}
main{
SystemInfo info =new SystemInfo ();
SysOs os = new Windows();
info.getCpuInfo(os);
}