您好,登錄后才能下訂單哦!
這篇文章主要講解了“Java橋接模式實例詳解”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Java橋接模式實例詳解”吧!
橋接模式簡單版
一 代碼
class Meal{ protected MealImp imp; public Meal() { imp = new AmericanMealImp(); } public Meal(String type) { if (type.equals("American")) imp = new AmericanMealImp(); if (type.equals("Italian")) imp = new ItalianMealImp(); } public void getFirstCourse() { imp.getAppetizer(); } public void getSecondCourse() { imp.getMeat(); } public void getBeverage() { imp.getBeverage(); } public void getDessert() { imp.getDessert(); }}interface MealImp { public abstract void getAppetizer(); public abstract void getSoup(); public abstract void getSalad(); public abstract void getFish(); public abstract void getMeat(); public abstract void getSorbet(); public abstract void getPasta(); public abstract void getBeverage(); public abstract void getDessert(); public abstract void getSandwich();}class AmericanMealImp implements MealImp { public void getAppetizer() { System.out.println("開胃菜 : 烤干酪"); } public void getSoup() {} public void getSalad() {} public void getFish() {} public void getMeat() { System.out.println("肉食 :牛排"); } public void getPasta() {} public void getBeverage() { System.out.println("酒水 :啤酒"); } public void getDessert() { System.out.println("餐后甜點:蘋果派"); } public void getSorbet() {} public void getSandwich() {}}class ItalianMealImp implements MealImp { public void getAppetizer() { System.out.println("開胃菜: 意大利蔬菜拼盤"); } public void getSoup() {} public void getSalad() {} public void getFish() {} public void getMeat() { System.out.println("肉食: 意大利檸檬香煎雞排"); } public void getPasta() {} public void getCheesePlate() {} public void getBeverage() { System.out.println("酒水: 卡布奇諾咖啡"); } public void getDessert() { System.out.println("餐后甜點: 冰淇淋"); } public void getSorbet() {} public void getSandwich() {}}public class CustomerBridgeDemo { private Meal meal; public CustomerBridgeDemo(Meal aMeal) { meal = aMeal; } public void eat() { meal.getFirstCourse(); meal.getSecondCourse(); meal.getBeverage(); meal.getDessert(); } public static void main(String[] args) { Meal aMeal = null; if (args.length == 0) { aMeal = new Meal(); } else if (args.length == 1) { if (!(args[0].equals("American")) && !(args[0].equals("Italian")) ) { System.err.println("輸入參數有誤!"); System.err.println("正確用法: java Customer [American|Italian]"); System.exit(1); } else { aMeal = new Meal(args[0]); } } else { // error System.err.println("輸入參數有誤!"); System.err.println("正確用法: java Customer [American|Italian]"); System.exit(1); } CustomerBridgeDemo cus = new CustomerBridgeDemo(aMeal); cus.eat(); }}
二 運行
開胃菜 : 烤干酪肉食 :牛排酒水 :啤酒餐后甜點:蘋果派
橋接模式升級版
一 代碼
class Meal { protected MealImp imp; public Meal() { imp = new AmericanMealImp(); } public Meal( String type ) { if (type.equals("American")) imp = new AmericanMealImp(); if (type.equals("Italian")) imp = new ItalianMealImp(); } public void getFirstCourse() { imp.getAppetizer(); } public void getSecondCourse() { imp.getMeat(); } public void getBeverage() { imp.getBeverage(); } public void getDessert() { imp.getDessert(); }}interface MealImp { public abstract void getAppetizer(); public abstract void getSoup(); public abstract void getSalad(); public abstract void getFish(); public abstract void getMeat(); public abstract void getSorbet(); public abstract void getPasta(); public abstract void getBeverage(); public abstract void getDessert(); public abstract void getSandwich();}class AmericanMealImp implements MealImp { public void getAppetizer() { System.out.println("開胃菜 : 烤干酪"); } public void getSoup() { } public void getSalad() { } public void getFish() { } public void getMeat() { System.out.println("肉食 :牛排"); } public void getPasta() { } public void getBeverage() { System.out.println("酒水 :啤酒"); } public void getDessert() { System.out.println("餐后甜點:蘋果派"); } public void getSorbet() { } public void getSandwich() { }}class ItalianMealImp implements MealImp { public void getAppetizer() { System.out.println("開胃菜: 意大利蔬菜拼盤"); } public void getSoup() { } public void getSalad() { } public void getFish() { } public void getMeat() { System.out.println("肉食: 意大利檸檬香煎雞排"); } public void getPasta() { } public void getCheesePlate() { } public void getBeverage() { System.out.println("酒水: 卡普齊諾咖啡"); } public void getDessert() { System.out.println("餐后甜點: 冰淇淋"); } public void getSorbet() { } public void getSandwich() { }}class Snack extends Meal { Snack() { super(); } Snack( String type ) { super(type); } public void getSnack() { //甜點 imp.getAppetizer(); }}class Lunch extends Meal { Lunch() { super(); } Lunch( String type ) { super(type); } public void getLunch() { //午餐 imp.getSandwich(); //三明治 imp.getBeverage(); //飲品 }}class FiveCourseMeal extends Meal { FiveCourseMeal() { super(); } FiveCourseMeal( String type ) { super(type); } public void getEnormousDinner() { //大餐 imp.getAppetizer(); //開胃菜 imp.getSorbet(); //果汁冰水 imp.getSoup(); //湯 imp.getSorbet(); //果汁冰水 imp.getSalad(); //沙拉 imp.getSorbet(); //果汁冰水 imp.getFish(); //魚 imp.getSorbet(); //果汁冰水 imp.getMeat(); //肉 imp.getDessert(); //果汁冰水 imp.getBeverage(); //酒水 }}public class CustomerBridgeDemo2 { private FiveCourseMeal bigMeal; public CustomerBridgeDemo2( FiveCourseMeal meal ) { this.bigMeal = meal; } public void eat() { bigMeal.getEnormousDinner(); bigMeal.getDessert(); //單點一個甜點,老菜譜依然有效 } public static void main( String[] args ) { FiveCourseMeal aMeal = null; if (args.length == 0) { aMeal = new FiveCourseMeal(); } else if (args.length == 1) { if (!(args[0].equals("American")) && !(args[0].equals("Italian"))) { System.err.println("輸入參數有誤!"); System.err.println("正確用法: java Customer [American|Italian]"); System.exit(1); } else { aMeal = new FiveCourseMeal(args[0]); } } else { System.err.println("輸入參數有誤!"); System.err.println("正確用法: java Customer [American|Italian]"); System.exit(1); } CustomerBridgeDemo2 cus = new CustomerBridgeDemo2(aMeal); cus.eat(); }}
二 運行
開胃菜 : 烤干酪肉食 :牛排餐后甜點:蘋果派酒水 :啤酒餐后甜點:蘋果派
感謝各位的閱讀,以上就是“Java橋接模式實例詳解”的內容了,經過本文的學習后,相信大家對Java橋接模式實例詳解這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。