依賴倒置原則就是抽象類和接口在使用時的一些規(guī)范和建議,我們的應用上層模塊不直接依賴于下層模塊,不具體依賴某個類或者對象,而是依賴于某個抽象。
接著上一節(jié)的Demo,我們有3個手機的對象。分別是LumiaPhone,Galaxy,ApplePhone,現(xiàn)在我們新增一個Student學生類,學生會使用手機。有可能使用LumiaPhone手機,也有可能使用Galaxy,也有可能使用
ApplePhone,那我們的Student看起來的得實現(xiàn)成這樣:
public class Student { public int Id { get; set; } public string Name { get; set; } public void PlayPhone(LumiaPhone lumiaPhone) { Console.WriteLine($"{Name} use {lumiaPhone.Name}"); } public void PlayApplePhone(ApplePhone applePhone) { Console.WriteLine($"{Name} use {applePhone.Name}"); } public void PlayGalaxy(Galaxy galaxy) { Console.WriteLine($"{Name} use {galaxy.Name}"); } }
具體使用時,我們可能會是這樣: 先創(chuàng)建一個Student再根據(jù)學生使用的某種手機 調(diào)用不同的play方法
LumiaPhone lumiaPhone = new LumiaPhone(); ApplePhone applePhone = new ApplePhone(); Galaxy galaxy = new Galaxy(); Student student = new Student(); student.PlayPhone(lumiaPhone); student.PlayApplePhone(applePhone); student.PlayGalaxy(galaxy);
這個時候假設Student舊的手機壞了,換了新的華為手機,那我們新增一個Honor對象
public class Honor { public void System() { Console.WriteLine("Hua Wei Honor"); } }
再修改Student類 新增一個方法
public void PlayHonor(Honor honor) { Console.WriteLine($"{Name} use {honor.Name}"); }
使用時
Honor honor = new Honor(); student.PlayHonor(honor);
手機有太多種類,每次新增對象我們都要在Student中新增方法 在調(diào)用處進行手機的實例化,維護起來 非常麻煩,容易出錯。不利于擴展,所以我們的Student 不應該依賴于具體的類,而是應該依賴抽象 上一遍提到的BasePhone,我們來改造下代碼 Student會變成這樣
public class Student { public int Id { get; set; } public string Name { get; set; } public void PlayPhone(BasePhone basePhone) { Console.WriteLine($"{Name} use {basePhone.Name}"); } //public void PlayPhone(LumiaPhone lumiaPhone) //{ // Console.WriteLine($"{Name} use {lumiaPhone.Name}"); //} //public void PlayApplePhone(ApplePhone applePhone) //{ // Console.WriteLine($"{Name} use {applePhone.Name}"); //} //public void PlayGalaxy(Galaxy galaxy) //{ // Console.WriteLine($"{Name} use {galaxy.Name}"); //} //public void PlayHonor(Honor honor) //{ // Console.WriteLine($"{Name} use {honor.Name}"); //} }
我們的調(diào)用處
static void Main(string[] args)
{
{
LumiaPhone lumiaPhone = new LumiaPhone();
ApplePhone applePhone = new ApplePhone();
Galaxy galaxy = new Galaxy();
Honor honor = new Honor();
Student student = new Student();
student.PlayPhone(lumiaPhone);
student.PlayPhone(applePhone);
student.PlayPhone(galaxy);
student.PlayPhone(honor);
//student.PlayPhone(lumiaPhone);
//student.PlayApplePhone(applePhone);
//student.PlayGalaxy(galaxy);
//student.PlayHonor(honor);
}
Console.ReadKey();
}
這個時候感覺Main還是依賴了具體的對象Student,以學生使用honor手機為例
BasePhone honor = new Honor(); Student student = new Student(); student.PlayPhone(honor);
我們新增一個SimpleFactory及接口IPlayPhone
public static class SimpleFactory { public static BasePhone CreatePhone() { return new Honor(); } public static Student CreateStudent() { return new Student(); } }
public interface IPlayPhone { void PlayPhone(BasePhone basePhone); }
那我們的Main方法則變成
static void Main(string[] args) { { BasePhone honor = SimpleFactory.CreatePhone(); IPlayPhone student = SimpleFactory.CreateStudent(); student.PlayPhone(honor); //Honor honor = new Honor(); //Student student = new Student(); //student.PlayPhone(honor); //student.PlayPhone(lumiaPhone); //student.PlayApplePhone(applePhone); //student.PlayGalaxy(galaxy); //student.PlayHonor(honor); } Console.ReadKey(); }
我在這個demo SimpleFactory里直接返回了一個對象,項目中是可以通過讀取配置文件反射來構造實的
這樣只要通過改配置文件 就可以實現(xiàn)學生更換手機的功能,同樣通過配置文件 也可以實現(xiàn) 老師使用手機只要我們新增一個Teacher類 實現(xiàn)IPlayPhone接口。其他代碼不需要任何改動, 這就是我們原則 依賴抽象或接口 而不應該依賴于具體的細節(jié)的好處, 這里學生,老師其實就是上層,而我們的手機則是下層,上層不應該依賴下層 而是應該依賴抽象
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
本文標題:依賴倒置原則(二)-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://www.rwnh.cn/article28/ppecp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供響應式網(wǎng)站、軟件開發(fā)、品牌網(wǎng)站建設、搜索引擎優(yōu)化、外貿(mào)建站、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容