English 中文(简体)
对桥桥型模式的不公的正确
原标题:Correct undestanding of 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{
        }

" 强 " 后(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() {
        }
    }
问题回答

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

<强>BEFORE:

                    ________Car_________
                    /                    
               NativeCar              ForeignCar
               /                     /        
   GoodNativeCar   BadNativeCar GoodForeignCar  BadForeignCar

<强 > 后:

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

如果我们在“http://www.oodesign.com/bridge-pattern.html”上查看桥型的类图,rel=“nofollow”>http://www.oodesign.com/bridge-pattern.html ,它看起来像桥型图。然而,等级等级结构的CarAbstraction-CarAbstractionImpl可以被封存。表示 CarAbstraction Has a Carimplementor和土著汽车 & amp;外国汽车将由Carbstraction继承。

从类图看,它看起来像桥形图。

但是,概念要点呢?土著汽车和外国汽车的抽象性还是也可以作为执行工具?它们可以与Goodcar和Badcar交换吗?这个事实也需要考虑。 如果土著汽车和外国汽车是抽象的,作为执行工具的则是Goodcar和Badcar,那么这一局面的桥梁模式。

据我所知,你试图做一些事情 比如:

 When:

        A
     /     
    Aa      Ab
   /      /  
 Aa1 Aa2  Ab1 Ab2

Refactor to:

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

(从这里开始 : < a href=""https://stackoverflow.com/ questions/319728/ when-do-you-use-the-bridge-patter" 何时使用桥型? 它与适应器模式有什么不同? )

我不太熟悉大桥模式,但看起来是正确的。





相关问题
Choosing the right subclass to instantiate programmatically

Ok, the context is some serialization / deserialization code that will parse a byte stream into an object representation that s easier to work with (and vice-versa). Here s a simplified example ...

Design pattern for managing queues and stacks?

Is there a design pattern for managing a queue or a stack? For example, we are looking to manage a list of tasks. These tasks will be added to a group queue, users will then be able to pull off the ...

Organizing classes using the repository design pattern

I have started upgrading one of our internal software applications, written in ASP.NET Web Forms, and moving to ASP.NET MVC. I am trying to leverage the Repository design pattern for my classes, ...

Misuse of Observer Pattern?

I have a Car object which contains a latitude field and a longitude field. I use the observer pattern so that any time either of these fields change in my application, my car object is notified. I ...

How are Models (in MVC) and DAOs supposed to interact?

How are models and DAOs supposed to interact? I m in the process of putting together a simple login module and I m unsure where to put the "business logic." If I put the logic with the data in the ...

热门标签