Flask接口签名sign原理与实例代码浅析
252
2023-01-01
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();
}
SnacktHPVkKK( 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操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~