5

我是否正确理解桥接模式:

前:

  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{
        }

后(桥):

 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() {
        }
    }
4

2 回答 2

2

从您的问题中,我得到了以下类图:

前:

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

后:

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

如果我们在http://www.oodesign.com/bridge-pattern.html查看桥接模式的类图,它看起来像桥接模式。但是,可以省略类层次结构 CarAbstraction--CarAbstractionImpl。意味着 CarAbstraction 有一个 CarImplementor 并且 NativeCar 和 ForeignCar 将被 CarAbstraction 继承。

所以,从类图中,它看起来像桥模式。

但是概念点呢?NativeCar 和 ForeignCar 是抽象还是它们也可以用作实现?它们可以与 GoodCar 和 BadCar 互换吗?这个事实也需要考虑。如果 NativeCar 和 ForeignCar 是抽象的,而 GoodCar 和 BadCar 作为实现,那么这种桥接模式适用于这种情况。

于 2012-12-20T05:09:51.460 回答
0

As I understand you try to do something like:

 When:

        A
     /     \
    Aa      Ab
   / \     /  \
 Aa1 Aa2  Ab1 Ab2

Refactor to:

     A         N
  /     \     / \
Aa(N) Ab(N)  1   2

(Got from here : When do you use the Bridge Pattern? How is it different from Adapter pattern?)

I am not very close familiar with Bridge Pattern but it looks right.

于 2012-05-23T08:24:43.003 回答