設計模式

六大原則
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);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章