Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782460
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-02-13 11:17:17

According to GoF definition: bridge pattern is ,decouple an abstraction from its implementation 
so that the two can vary independently. 
我实在是理解无力, 还是看code吧。

Usage in Jdk:
AWT (It provides an abstraction layer which maps onto the native OS the windowing support)
JDBC


点击(此处)折叠或打开

  1. package BridgePattern;

  2. interface VehicleType{
  3.     abstract public void book();
  4. }

  5. abstract class Vehicle{
  6.     protected VehicleType type1;
  7.     protected VehicleType type2;

  8.     public Vehicle(VehicleType type1, VehicleType type2){
  9.         this.type1 = type1;
  10.         this.type2 = type2;
  11.     }

  12.     abstract public void purchase();
  13. }


  14. class Car extends Vehicle{
  15.     //Constructor
  16.     public Car(VehicleType type1, VehicleType type2){
  17.         super(type1, type2);
  18.     }

  19.     @Override
  20.     public void purchase(){
  21.         System.out.println("car");
  22.         type1.book();
  23.         type2.book();
  24.     }
  25. }

  26. class Bike extends Vehicle{
  27.     public Bike(VehicleType type1, VehicleType type2){
  28.         super(type1, type2);
  29.     }

  30.     @Override
  31.     public void purchase(){
  32.         System.out.println("Bike");
  33.         type1.book();
  34.         type2.book();
  35.     }
  36. }


  37. class NewVehicle implements VehicleType{

  38.     @Override
  39.     public void book(){
  40.         System.out.println("New Vehicle");
  41.     }
  42. }

  43. class OldVehicle implements VehicleType{
  44.     @Override
  45.     public void book(){
  46.         System.out.println("Old Vehicle");
  47.     }
  48. }

  49. public class BridgePatternDemo {
  50.     public static void main(String[] args){
  51.         Vehicle vehicle1 = new Car(new NewVehicle(), new OldVehicle());
  52.         vehicle1.purchase();

  53.         Bike bike1 = new Bike(new NewVehicle(), new OldVehicle());
  54.         bike1.purchase();
  55.     }
  56. }


阅读(495) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~