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

后端 未结 14 2167
予麋鹿
予麋鹿 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 21:17

    Strategy:

    • Context tied to the Strategy: The context Class (possibly Abstract but not really an interface! as u wish to encapsulate out a specific behavior and not the entire implementation) would know/contain the strategy interface reference and the implementation to invoke the strategy behavior on it.
    • Intent is ability to swap behavior at runtime

      class Context {
      
           IStrategy strategyReference;
      
           void strategicBehaviour() {
      
              strategyReference.behave();
           }
      
      }
      

    Bridge

    • Abstraction not tied to the Implementation: The abstraction interface (or abstract class with most of the behavior abstract) would not know/contain the implementation interface reference
    • Intent is to completely decouple the Abstraction from the Implementation

      interface IAbstraction {
      
          void behaviour1();
      
          .....
      
      }
      
      interface IImplementation {
      
           void behave1();
      
           void behave2();
      
           .....
      
      }
      
      class ConcreteAbstraction1 implements IAbstraction {
      
            IImplementation implmentReference;
      
            ConcreteAbstraction1() {
      
                 implmentReference = new ImplementationA() // Some implementation
      
            }
      
            void behaviour1() {
      
                  implmentReference.behave1();
      
            }
      
            .............
      
      }
      
      class ConcreteAbstraction2 implements IAbstraction {
      
            IImplementation implmentReference;
      
            ConcreteAbstraction1() {
      
                 implmentReference = new ImplementationB() // Some Other implementation
      
            }
      
            void behaviour1() {
      
                  implmentReference.behave2();
      
            }
      
            .............
      
      }
      

提交回复
热议问题