Java桥接模式实例详解【简单版与升级版】

本文实例讲述了Java桥接模式。分享给大家供大家参考,具体如下:

创新互联-专业网站定制、快速模板网站建设、高性价比洛川网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式洛川网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖洛川地区。费用合理售后完善,10多年实体公司更值得信赖。

桥接模式简单版

一 代码

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();
  }
}

二 运行

开胃菜    : 烤干酪
肉食        :牛排
酒水        :啤酒
餐后甜点:苹果派

三 类图

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() {
  }
}
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数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。


文章标题:Java桥接模式实例详解【简单版与升级版】
浏览地址:http://scyanting.com/article/geoped.html