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