Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of Java bridging Mode example

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "detailed explanation of the example of Java bridging mode". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "detailed explanation of Java bridging mode example".

Bridge mode simple version

One code

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 ("appetizers: grilled cheese");} public void getSoup () {} public void getSalad () {} public void getFish () {} public void getMeat () {System.out.println ("Meat: steak") } public void getPasta () {} public void getBeverage () {System.out.println ("drinks: beer");} public void getDessert () {System.out.println ("dessert: Apple pie");} public void getSorbet () {} public void getSandwich () {} class ItalianMealImp implements MealImp {public void getAppetizer () {System.out.println ("appetizers: Italian vegetable platter") } public void getSoup () {} public void getSalad () {} public void getFish () {} public void getMeat () {System.out.println ("meat: Italian lemon fried chicken steak");} public void getPasta () {} public void getCheesePlate () {} public void getBeverage () {System.out.println ("drinks: cappuccino") } public void getDessert () {System.out.println ("dessert: ice cream");} 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 ("incorrect input parameters!") ; System.err.println ("correct usage: java Customer [American | Italian]"); System.exit (1);} else {aMeal = new Meal (args [0]);}} else {/ / error System.err.println ("incorrect input parameters!") ; System.err.println ("correct usage: java Customer [American | Italian]"); System.exit (1);} CustomerBridgeDemo cus = new CustomerBridgeDemo (aMeal); cus.eat ();}}

Second operation

Appetizers: grilled cheese meat: steak drinks: beer dessert: Apple pie

Bridge mode upgrade

One code

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 ("appetizers: grilled cheese");} public void getSoup () {} public void getSalad () {} public void getFish () {} public void getMeat () {System.out.println ("Meat: steak") } public void getPasta () {} public void getBeverage () {System.out.println ("drinks: beer");} public void getDessert () {System.out.println ("dessert: Apple pie");} public void getSorbet () {} public void getSandwich () {} class ItalianMealImp implements MealImp {public void getAppetizer () {System.out.println ("appetizers: Italian vegetable platter") } public void getSoup () {} public void getSalad () {} public void getFish () {} public void getMeat () {System.out.println ("meat: fried chicken steak with lemon");} public void getPasta () {} public void getCheesePlate () {} public void getBeverage () {System.out.println ("drinks: cappuccino") } public void getDessert () {System.out.println ("dessert: ice cream");} public void getSorbet () {} public void getSandwich () {} class Snack extends Meal {Snack () {super ();} Snack (String type) {super (type);} public void getSnack () {/ / dessert imp.getAppetizer ();}} class Lunch extends Meal {Lunch () {super () } Lunch (String type) {super (type);} public void getLunch () {/ / lunch imp.getSandwich (); / / sandwich imp.getBeverage (); / / drinks}} class FiveCourseMeal extends Meal {FiveCourseMeal () {super ();} FiveCourseMeal (String type) {super (type);} public void getEnormousDinner () {/ / feast imp.getAppetizer () / / appetizer imp.getSorbet (); / Juice Water imp.getSoup (); / Soup imp.getSorbet (); / Juice Ice Water imp.getSalad (); / / Salad imp.getSorbet (); / / Juice Water imp.getFish (); / / Fish imp.getSorbet (); / / Juice Water imp.getMeat (); / / Meat imp.getDessert () / / Juice imp.getBeverage (); / / drinks}} public class CustomerBridgeDemo2 {private FiveCourseMeal bigMeal; public CustomerBridgeDemo2 (FiveCourseMeal meal) {this.bigMeal = meal;} public void eat () {bigMeal.getEnormousDinner (); bigMeal.getDessert (); / / order a dessert, the old recipe is still valid} 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 ("incorrect input parameters!") ; System.err.println ("correct usage: java Customer [American | Italian]"); System.exit (1);} else {aMeal = new FiveCourseMeal (args [0]);}} else {System.err.println ("incorrect input parameters!") ; System.err.println ("correct usage: java Customer [American | Italian]"); System.exit (1);} CustomerBridgeDemo2 cus = new CustomerBridgeDemo2 (aMeal); cus.eat ();}}

Second operation

Appetizers: grilled cheese meat: steak dessert: Apple pie drinks: beer dessert: Apple pie

Thank you for your reading, the above is the content of "detailed explanation of Java bridging mode examples". After the study of this article, I believe you have a deeper understanding of the detailed explanation of Java bridging mode examples, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report