重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要讲解了“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桥接模式实例详解这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联网站建设公司,,小编将为大家推送更多相关知识点的文章,欢迎关注!