<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chan Chen Coding...

    Five: Bridge Design Pattern

    “Decouple an abstraction from its implementation so that the two can vary independently” is the intent for bridge design pattern as stated by GoF.
    Bridge design pattern is a modified version of the notion of “prefer composition over inheritance”.
    Problem and Need for Bridge Design Pattern
    When there are inheritance hierarchies creating concrete implementation, you loose flexibility because of interdependence. Oops! these kind of sentencies shows that the author(I) didn’t understand and tries to escape! Okay, I will decrypt this sentence in the coming paragraphs.
    Decouple implentation from interface and hiding implementation details from client is the essense of bridge design pattern.
    Elements of Bridge Design Pattern
    Abstraction – core of the bridge design pattern and defines the crux. Contains a reference to the implementer.
    Refined Abstraction – Extends the abstraction takes the finer detail one level below. Hides the finer elements from implemetors.
    Implementer - This interface is the higer level than abstraction. Just defines the basic operations.
    Concrete Implementation – Implements the above implementer by providing concrete implementation.
    Example for core elements of Bridge Design Pattern
    Vehicle -> Abstraction
    manufacture()
    Car -> Refined Abstraction 1
    manufacture()
    Bike -> Refined Abstraction 2
    manufacture()
    Workshop -> Implementor
    work()
    Produce -> Concrete Implementation 1
    work()
    Assemble -> Concrete Implementation 2
    work()
    Generic UML Diagram for Bridge Design Pattern
    Before Bridge Design Pattern
    After Bridge Design Pattern
    Sample Java Code for Bridge Design Pattern
    package com.javapapers.designpattern;
     
    /**
     * abstraction in bridge pattern
     * */
    abstract class Vehicle {
      protected Workshop workShop1;
      protected Workshop workShop2;
     
      protected Vehicle(Workshop workShop1, Workshop workShop2) {
        this.workShop1 = workShop1;
        this.workShop2 = workShop2;
      }
     
      abstract public void manufacture();
    }
     
    package com.javapapers.designpattern;
     
    /**
     * Refine abstraction 1 in bridge pattern
     */
    public class Car extends Vehicle {
     
      public Car(Workshop workShop1, Workshop workShop2) {
        super(workShop1, workShop2);
      }
     
      @Override
      public void manufacture() {
        System.out.print("Car ");
        workShop1.work();
        workShop2.work();
     
      }
     
    }
     
    package com.javapapers.designpattern;
     
    /**
     * Refine abstraction 2 in bridge pattern
     */
    public class Bike extends Vehicle {
     
      public Bike(Workshop workShop1, Workshop workShop2) {
        super(workShop1, workShop2);
      }
     
      @Override
      public void manufacture() {
        System.out.print("Bike ");
        workShop1.work();
        workShop2.work();
      }
     
    }
     
    package com.javapapers.designpattern;
     
    /**
     * Implementor for bridge pattern
     * */
    public interface Workshop {
      abstract public void work();
    }
     
    package com.javapapers.designpattern;
     
    /**
     * Concrete implementation 1 for bridge pattern
     * */
    public class Produce implements Workshop {
     
      @Override
      public void work() {
        System.out.print("Produced");
      }
     
    }
     
    package com.javapapers.designpattern;
     
    /**
     * Concrete implementation 2 for bridge pattern
     * */
    public class Assemble implements Workshop {
     
      @Override
      public void work() {
        System.out.println(" Assembled.");
      }
     
    }
     
    package com.javapapers.designpattern;
     
    /*
     * Demonstration of bridge design pattern
     */
    public class BridgePattern {
     
      public static void main(String[] args) {
     
        Vehicle vehicle1 = new Car(new Produce(), new Assemble());
        vehicle1.manufacture();
        Vehicle vehicle2 = new Bike(new Produce(), new Assemble());
        vehicle2.manufacture();
     
      }
    }
     
    Output:
    Car Produced Assembled.
    Bike Produced Assembled.
    Summary of Bridge Design Pattern
    Creates two different hierarchies. One for abstraction and another for implementation.
    Avoids permanent binding by removing the dependency between abstraction and implementation.
    We create a bridge that coordinates between abstraction and implementation.
    Abstraction and implementation can be extended separately.
    Should be used when we have need to switch implementation at runtime.
    Client should not be impacted if there is modification in implementation of abstraction.
    Best used when you have multiple implementations.
    Bridge Vs Adapter Design Pattern
    The adapter design pattern helps it two incompatible classes to work together. But, bridge design pattern decouples the abstraction and implementation by creating two different hierarchies.


    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-11-01 16:00 Chan Chen 閱讀(237) 評論(0)  編輯  收藏 所屬分類: Design Pattern

    主站蜘蛛池模板: 亚洲综合综合在线| 成人免费午间影院在线观看| 中国一级特黄高清免费的大片中国一级黄色片 | 亚洲性天天干天天摸| 日韩亚洲欧洲在线com91tv| 亚洲午夜未满十八勿入网站2| 亚洲午夜福利精品无码| 免费在线黄色网址| 亚洲欧洲国产成人综合在线观看| 又粗又硬免费毛片| 亚洲精品tv久久久久久久久久| 青青青国产色视频在线观看国产亚洲欧洲国产综合 | 久久成人无码国产免费播放| 全免费a级毛片免费看| 免费看男人j放进女人j免费看| 日韩电影免费在线观看| 久久免费区一区二区三波多野| 四虎国产精品永久免费网址| 99精品视频免费观看| 亚洲黄色免费观看| 手机在线看永久av片免费| 在线视频免费观看www动漫| 在线永久免费观看黄网站| 免费在线观看污网站| 亚洲精品乱码久久久久久久久久久久 | 午夜影院免费观看| 免费大片黄在线观看yw| 在线A级毛片无码免费真人| 国产jizzjizz免费视频| 在线亚洲精品自拍| 亚洲男人天堂2017| 日本亚洲免费无线码 | 亚洲人成综合网站7777香蕉| 亚洲AV无码片一区二区三区| 一区二区三区AV高清免费波多| 伊人免费在线观看高清版| 亚洲一级毛片免费看| 国产又大又粗又硬又长免费 | 成人免费无码视频在线网站| 免费播放春色aⅴ视频| 亚洲国产精品一区第二页|