<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

    主站蜘蛛池模板: 最近免费中文在线视频| 成人无遮挡毛片免费看| 久久精品亚洲AV久久久无码| 免费视频中文字幕| eeuss在线兵区免费观看| 久久久久亚洲AV片无码下载蜜桃 | 久久久99精品免费观看| 亚洲av一本岛在线播放| 亚洲免费视频一区二区三区| 最近免费视频中文字幕大全| 污污免费在线观看| 亚洲美免无码中文字幕在线| 亚洲AV无码成人精品区大在线| 最近中文字幕免费完整| 美女扒开屁股让男人桶爽免费 | 18禁亚洲深夜福利人口| 亚洲Aⅴ无码专区在线观看q | 国产成人精品日本亚洲直接| 国产成人99久久亚洲综合精品| 在线看片韩国免费人成视频| 香蕉视频在线免费看| 亚洲欧美日韩国产精品一区| 亚洲成人免费在线| 亚洲精品一级无码中文字幕 | 亚洲尹人九九大色香蕉网站| 免费人成网站在线播放| 日韩免费一区二区三区在线| 国产自国产自愉自愉免费24区| 亚洲av无码日韩av无码网站冲| 亚洲综合在线成人一区| 亚洲一区二区三区在线观看精品中文| A级毛片内射免费视频| 久久A级毛片免费观看| 黄色网址免费在线观看| 男男gay做爽爽的视频免费| 亚洲最大福利视频| 久久夜色精品国产噜噜噜亚洲AV | 亚洲短视频在线观看| 亚洲成av人片在线观看无码不卡| 国产免费啪嗒啪嗒视频看看| 免费观看黄网站在线播放|