高层模块不应该依赖底层模块,二者都应该依赖其抽象
抽象不应该依赖细节,细节应该依赖抽象
依赖倒置的中心思想是面向接口编程
如果你了解点设计模式,应该理解上面的话,但是如果不了解的话,感觉说的云里雾里的,是不是一脸懵,懵这就对了,那就先看例子吧,会帮助你理解的!
package com.liu.susu.principle.inversion.example1; /** * @FileName FamilyFarm * @Description * @Author susu * @date 2022-02-13 **/ public class FamilyFarm { public void raiseDog(DogGrowthProcess dogGrowthProcess){ dogGrowthProcess.dogBorn(); dogGrowthProcess.dogGrowUp(); dogGrowthProcess.dogBeRaised(); } } class DogGrowthProcess { public void dogBorn(){ System.out.println("狗狗-->麦兜出生了……"); } public void dogGrowUp(){ System.out.println("狗狗-->麦兜长大了……"); } public void dogBeRaised(){ System.out.println("狗狗-->麦兜被饲养员带走了……"); } } class ClientTest{ public static void main(String[] args) { //狗狗长大了,开始饲养狗狗 new FamilyFarm().raiseDog(new DogGrowthProcess()); } }
package com.liu.susu.principle.inversion.example2; /** * @FileName Breeder * @Description * @Author susu * @date 2022-02-13 **/ public class FamilyFarm { public void raiseDog(DogGrowthProcess dogGrowthProcess){ dogGrowthProcess.dogBorn(); dogGrowthProcess.dogGrowUp(); dogGrowthProcess.dogBeRaised(); } public void raiseCat(CatGrowthProcess catGrowthProcess){ catGrowthProcess.catBorn(); catGrowthProcess.catGrowUp(); catGrowthProcess.catBeRaised(); } } class DogGrowthProcess { public void dogBorn(){ System.out.println("狗狗-->麦兜出生了……"); } public void dogGrowUp(){ System.out.println("狗狗-->麦兜长大了……"); } public void dogBeRaised(){ System.out.println("狗狗-->麦兜被饲养员带走了……"); } } class CatGrowthProcess { public void catBorn(){ System.out.println("小猫-->阿苔出生了……"); } public void catGrowUp(){ System.out.println("小猫-->阿苔长大了……"); } public void catBeRaised(){ System.out.println("小猫-->阿苔被饲养员带走了……"); } } class ClientTest{ public static void main(String[] args) { //开始饲养狗狗 new FamilyFarm().raiseDog(new DogGrowthProcess()); //开始饲养小猫 new FamilyFarm().raiseCat(new CatGrowthProcess()); } }
package com.liu.susu.principle.inversion.example3; /** * @FileName FamilyFarm * @Description 使用 依赖倒置原则 优化后的代码 * @Author susu * @date 2022-02-13 **/ public class FamilyFarm { public void raisePet(petGrowthProcess petGrowthProcess){ petGrowthProcess.petBorn(); petGrowthProcess.petGrowUp(); petGrowthProcess.petBeRaised(); } } interface petGrowthProcess{ void petBorn(); void petGrowUp(); void petBeRaised(); } class DogGrowthProcess implements petGrowthProcess{ public void petBorn(){ System.out.println("狗狗-->麦兜出生了……"); } public void petGrowUp(){ System.out.println("狗狗-->麦兜长大了……"); } public void petBeRaised(){ System.out.println("狗狗-->麦兜被饲养员带走了……"); } } class CatGrowthProcess implements petGrowthProcess{ public void petBorn(){ System.out.println("小猫-->阿苔出生了……"); } public void petGrowUp(){ System.out.println("小猫-->阿苔长大了……"); } public void petBeRaised(){ System.out.println("小猫-->阿苔被饲养员带走了……"); } } class ClientTest{ public static void main(String[] args) { FamilyFarm familyFarm = new FamilyFarm(); //饲养狗狗 familyFarm.raisePet(new DogGrowthProcess()); //饲养小猫 familyFarm.raisePet(new CatGrowthProcess()); } }
package com.liu.susu.principle.inversion.example4; /** * @FileName FamilyFarm * @Description 使用 依赖倒置原则 优化后的代码 * @Author susu * @date 2022-02-13 **/ public class FamilyFarm { public void raisePet(petGrowthProcess petGrowthProcess){ petGrowthProcess.petBorn(); petGrowthProcess.petGrowUp(); petGrowthProcess.petBeRaised(); } } interface petGrowthProcess{ void petBorn(); void petGrowUp(); void petBeRaised(); } class DogGrowthProcess implements petGrowthProcess{ public void petBorn(){ System.out.println("狗狗-->麦兜出生了……"); } public void petGrowUp(){ System.out.println("狗狗-->麦兜长大了……"); } public void petBeRaised(){ System.out.println("狗狗-->麦兜被饲养员带走了……"); } } class CatGrowthProcess implements petGrowthProcess{ public void petBorn(){ System.out.println("小猫-->阿苔出生了……"); } public void petGrowUp(){ System.out.println("小猫-->阿苔长大了……"); } public void petBeRaised(){ System.out.println("小猫-->阿苔被饲养员带走了……"); } } //新增功能饲养羊 class sheepGrowthProcess implements petGrowthProcess{ public void petBorn(){ System.out.println("小羊-->点点出生了……"); } public void petGrowUp(){ System.out.println("小羊-->点点长大了……"); } public void petBeRaised(){ System.out.println("小羊-->点点被饲养员带走了……"); } } class ClientTest{ public static void main(String[] args) { FamilyFarm familyFarm = new FamilyFarm(); //饲养狗狗 familyFarm.raisePet(new DogGrowthProcess()); //饲养小猫 familyFarm.raisePet(new CatGrowthProcess()); //新增功能饲养羊 familyFarm.raisePet(new sheepGrowthProcess()); } }