Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct undestanding of Bridge Pattern

Am I correctly undestand Bridge Pattern:

BEFORE:

  public class Main2 {
            @SuppressWarnings("unused")
            public static void main(String[] args) {
                Car car11 = new BadNativeCar();
                Car car12 = new GoodNativeCar();
                Car car21 = new BadForeignCar();
                Car car22 = new GoodForeignCar();
            }
        }

        interface Car{
            public void drive();
            public void stop();
        }

        class NativeCar implements Car{
            @Override
            public void drive() {
            }
            @Override
            public void stop() {
            }
        }

        class ForeignCar implements Car{
            @Override
            public void drive() {
            }
            @Override
            public void stop() {
            }
        }

        class GoodNativeCar extends NativeCar{
        }
        class BadNativeCar extends NativeCar{
        }
        class GoodForeignCar extends ForeignCar{
        }
        class BadForeignCar extends ForeignCar{
        }

AFTER(BRIDGE):

 public class Main2 {
        public static void main(String[] args) {
            BadCar badCar = new BadCar();
            GoodCar goodCar = new GoodCar();
            CarAbstraction car11 = new NativeCar(badCar);
            CarAbstraction car12 = new NativeCar(goodCar);
            CarAbstraction car21 = new ForeignCar(badCar);
            CarAbstraction car22 = new ForeignCar(goodCar);
        }
    }

    interface CarAbstraction{
        public void drive();
        public void stop();
    }

    //Abstraction
    abstract class CarAbstractionImpl implements CarAbstraction{
        private CarImplementor carImplementor;

        public CarAbstractionImpl(CarImplementor carImplementor) {
            this.carImplementor = carImplementor;
        }

        @Override
        public void drive() {
            carImplementor.drive();
        }
        @Override
        public void stop() {
            carImplementor.stop();
        }
    }

    //RefinedAbstraction1
    class NativeCar extends CarAbstractionImpl{
        public NativeCar(CarImplementor carImplementor) {
            super(carImplementor);
        }
    }
    //RefinedAbstraction2
    class ForeignCar extends CarAbstractionImpl{
        public ForeignCar(CarImplementor carImplementor) {
            super(carImplementor);
        }
    }



    //Implementor
    interface CarImplementor extends CarAbstraction{
    }

    //ConcreteImplementor1
    class GoodCar implements CarImplementor{
        @Override
        public void drive() {
        }
        @Override
        public void stop() {
        }
    }

    //ConcreteImplementor2
    class BadCar implements CarImplementor{
        @Override
        public void drive() {
        }
        @Override
        public void stop() {
        }
    }
like image 932
drifter Avatar asked Nov 04 '22 23:11

drifter


1 Answers

From your question, I got following class diagram:

BEFORE:

                    ________Car_________
                    /                    \
               NativeCar              ForeignCar
               /       \              /        \
   GoodNativeCar   BadNativeCar GoodForeignCar  BadForeignCar

AFTER:

            CarAbstraction
                 |
         CarAbstractionImpl-------------HAS-A-------> CarImplementor
          /             \                              /          \
     NativeCar       ForeignCar                    GoodCar       BadCar

If we look the class diagram of bridge pattern at http://www.oodesign.com/bridge-pattern.html, it seems like bridge pattern. However, class hierarchy CarAbstraction--CarAbstractionImpl can be ommited. Means CarAbstraction HAS A CarImplementor and NativeCar & ForeignCar will be inherited by CarAbstraction.

So, from class diagram, it looks like bridge pattern.

But what about conceptual points? Are NativeCar and ForeignCar abstraction or can they be used as implementation as well? Are they swappable with GoodCar and BadCar? This fact also needs consideration. If NativeCar and ForeignCar are abstraction and they GoodCar and BadCar as implementation, then this bridge pattern for this situation.

like image 118
doptimusprime Avatar answered Dec 15 '22 19:12

doptimusprime