<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免费久久| 亚洲国产精品99久久久久久| 中国精品一级毛片免费播放| mm1313亚洲国产精品美女| 啦啦啦手机完整免费高清观看| 亚洲最新中文字幕| xx视频在线永久免费观看| 亚洲人成小说网站色| 大学生一级特黄的免费大片视频| 亚洲一久久久久久久久| 国产免费人视频在线观看免费 | 久久亚洲国产精品成人AV秋霞| 一级毛片免费不卡在线| 亚洲国产精品日韩在线| 我要看WWW免费看插插视频| 国产成人亚洲精品播放器下载| 亚洲国产综合久久天堂| 成人网站免费看黄A站视频| 麻豆亚洲AV永久无码精品久久 | 亚洲精品乱码久久久久久蜜桃不卡| 99免费在线视频| 亚洲国产精品无码久久久| 浮力影院第一页小视频国产在线观看免费| 18禁亚洲深夜福利人口| 亚洲人成人网站色www| 99热在线观看免费| 国产精品无码亚洲精品2021| 自拍偷自拍亚洲精品被多人伦好爽| a级大片免费观看| 亚洲欧美日韩国产精品一区| 亚洲婷婷国产精品电影人久久| 99视频精品全部免费观看| 亚洲国产高清国产拍精品| 亚洲精品亚洲人成在线观看| 无码国产精品一区二区免费| 免费高清A级毛片在线播放| 亚洲最大成人网色| 免费人成视频在线观看视频| 久久久99精品免费观看| 亚洲Av无码国产一区二区| 亚洲AV无码不卡在线播放|