您好,登錄后才能下訂單哦!
本文實(shí)例講述了Java橋接模式。分享給大家供大家參考,具體如下:
橋接模式簡(jiǎn)單版
一 代碼
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("餐后甜點(diǎn):蘋果派"); } 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("餐后甜點(diǎn): 冰淇淋"); } 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("輸入?yún)?shù)有誤!"); System.err.println("正確用法: java Customer [American|Italian]"); System.exit(1); } else { aMeal = new Meal(args[0]); } } else { // error System.err.println("輸入?yún)?shù)有誤!"); System.err.println("正確用法: java Customer [American|Italian]"); System.exit(1); } CustomerBridgeDemo cus = new CustomerBridgeDemo(aMeal); cus.eat(); } }
二 運(yùn)行
開胃菜 : 烤干酪
肉食 :牛排
酒水 :啤酒
餐后甜點(diǎn):蘋果派
三 類圖
橋接模式升級(jí)版
一 代碼
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("餐后甜點(diǎn):蘋果派"); } 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("餐后甜點(diǎn): 冰淇淋"); } public void getSorbet() { } public void getSandwich() { } } class Snack extends Meal { Snack() { super(); } Snack( String type ) { super(type); } public void getSnack() { //甜點(diǎn) 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(); //單點(diǎn)一個(gè)甜點(diǎn),老菜譜依然有效 } 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("輸入?yún)?shù)有誤!"); System.err.println("正確用法: java Customer [American|Italian]"); System.exit(1); } else { aMeal = new FiveCourseMeal(args[0]); } } else { System.err.println("輸入?yún)?shù)有誤!"); System.err.println("正確用法: java Customer [American|Italian]"); System.exit(1); } CustomerBridgeDemo2 cus = new CustomerBridgeDemo2(aMeal); cus.eat(); } }
二 運(yùn)行
開胃菜 : 烤干酪
肉食 :牛排
餐后甜點(diǎn):蘋果派
酒水 :啤酒
餐后甜點(diǎn):蘋果派
三 類圖
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。