您好,登錄后才能下訂單哦!
本文實例講述了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面向對象程序設計入門與進階教程》、《Java數據結構與算法教程》、《Java操作DOM節點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。