What is the difference between the bridge pattern and the strategy pattern?

后端 未结 14 2099
予麋鹿
予麋鹿 2020-11-30 20:37

I tried to read many articles on dofactory, wikipedia and many sites. I have no idea on differences between bridge pattern and the strategy pattern.

I know both of t

相关标签:
14条回答
  • 2020-11-30 20:55

    The Bridge pattern is a structural pattern (HOW DO YOU BUILD A SOFTWARE COMPONENT?). The Strategy pattern is a dynamic pattern (HOW DO YOU WANT TO RUN A BEHAVIOUR IN SOFTWARE?).

    The syntax is similar but the goals are different:

    • Strategy: you have more ways for doing an operation; with strategy, you can choose the algorithm at run-time and you can modify a single Strategy without a lot of side-effects at compile-time;
    • Bridge: you can split the hierarchy of interface and class, join it with an abstract reference (see explication)
    0 讨论(0)
  • 2020-11-30 20:56

    Adding to willcodejavaforfood's answer, they can be the same, in implementation. However you use strategy to swap strategies such as sorting strategy, while you use bridge to bridge the implementations of two object's say a database wrapper, and a network adaptor so the client code can use either working against the same API. So the naming actually says it all

    0 讨论(0)
  • 2020-11-30 20:59

    For strategy pattern only the implementation varies.

    Suppose, class A is using class B which has multiple implementations available. So in that case B would be abstract with actual implementation provided at runtime. This is strategy pattern

    Now if A itself is abstract. Both A and B may vary. You would use Bridge pattern.

    0 讨论(0)
  • 2020-11-30 21:00

    Semantics. From wikipedia:

    The UML class diagram for the Strategy pattern is the same as the diagram for the Bridge pattern. However, these two design patterns aren't the same in their intent. While the Strategy pattern is meant for behavior, the Bridge pattern is meant for structure.

    The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.

    As I understand it, you're using the strategy pattern when you're abstracting behavior that could be provided from an external source (eg. config could specify to load some plugin assembly), and you're using the bridge pattern when you use the same constructs to make your code a bit neater. The actual code will look very similar - you're just applying the patterns for slightly different reasons.

    0 讨论(0)
  • 2020-11-30 21:00

    Bridge: ( A structural pattern)

    Bridge pattern decouples abstraction and implementation and allows both to vary independently.

    Use this pattern when :

    1. Abstractions and implementations have not been decided at compile time
    2. Abstractions and implementations should be changed independently
    3. Changes in implementation of abstraction should not affect caller application
    4. Client should be insulated from implementation details.

    Strategy: ( Behavioural pattern)

    Strategy patterns enable you to switch between multiple algorithms from a family of algorithms at run time.

    Use Strategy pattern when :

    1. Multiple versions of algorithms are required
    2. The behaviour of class has to be changed dynamically at run time
    3. Avoid conditional statements

    Related posts:

    When do you use the Bridge Pattern? How is it different from Adapter pattern?

    Real World Example of the Strategy Pattern

    0 讨论(0)
  • 2020-11-30 21:00

    Strategy pattern is used when you wish to plug algorithm or strategy at run time. As category of pattern also implies that it deals with behaviour of the objects. On the other hand bridge is structural pattern and deals with structural hierarchy of the objects. It decouples the abstraction from implementation by introducing a refined abstraction between them. Refined abstraction can be confused with the run time strategy plugged (In Strategy pattern). Bridge pattern deals with the structural aspects by providing a mechanism to avoid creating n number of classes.

    0 讨论(0)
提交回复
热议问题