依賴倒置原則:面向接口編程截汪,不要面向?qū)崿F(xiàn)編程梭依。
//韶關(guān)網(wǎng)店
class Customer
{
public void shopping(ShaoguanShop shop)
{
//購物
System.out.println(shop.sell());
}
}
//婺源網(wǎng)店
class Customer
{
public void shopping(WuyuanShop shop)
{
//購物
System.out.println(shop.sell());
}
}
顧客每更換一次商店掏湾,就要重寫一次代碼囤耳。
class Customer
{
public void shopping(Shop shop)
{
//購物
System.out.println(shop.sell());
}
}
//韶關(guān)網(wǎng)店
class ShaoguanShop implements Shop
{
public String sell()
{
return "韶關(guān)土特產(chǎn):香菇宛官、木耳……";
}
}
//婺源網(wǎng)店
class WuyuanShop implements Shop
{
public String sell()
{
return "婺源土特產(chǎn):綠茶媒咳、酒糟魚……";
}
}
顧客購物程序類圖