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

后端 未结 14 2100
予麋鹿
予麋鹿 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:14
    1. Strategy Pattern is used for Behavioural decisions, while Bridge Pattern is used for Structural decisions.

    2. Brigde Pattern separats the abstract elements from the implementation details, while Strategy Pattern is concerned making algorithms more interchangeable.

    Strategy Pattern in UML

    Brigde Pattern in UML

    Strategy Pattern in Swift:

    protocol PrintStrategy {
       func print(_ string: String) -> String
    }
    
    class Printer {
       let strategy: PrintStrategy
    
       init(strategy: PrintStrategy) {
          self.strategy = strategy
        }
    
      func print(_ string: String) -> String {
         return self.strategy.print(string)
      }
    }
    
    class UpperCaseStrategy: PrintStrategy {
        internal func print(_ string: String) -> String {
            return string.uppercased()
        }
    }
    
    class LowerCaseStrategy: PrintStrategy {
        internal func print(_ string: String) -> String {
            return string.lowercased()
        }
    }
    
    var lower = Printer(strategy: LowerCaseStrategy())
    lower.print("I love Software Patterns")
    
    var upper = Printer(strategy: UpperCaseStrategy())
    upper.print("I love Software Patterns")
    

    Brigde Pattern in Swift:

    protocol Appliance {
       func run()
    }
    
    protocol Switch {
       let appliance: Appliance {get set}
       func turnOn()
    }
    
    class RemoteControl: Switch {
       var appliance: Appliance
    
       init(appliance: Appliance) {
           self.appliance = appliance
       }
    
       internal func turnOn() {
          appliance.run()
       }
    }
    
    class TV: Appliance {
       internal func run() {
          print("TV is ON")
       }
    }
    
    class Stereo: Appliance {
       internal func run() {
          print("Stereo is ON")
       }
    }
    
    var tvRemote = RemoteControl.init(appliance: TV())
    tvRemote.turnOn()
    
    var stereoRemote = RemoteControl.init(appliance: Stereo())
    stereoRemote.turnOn()
    
    0 讨论(0)
  • 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();
      
            }
      
            .............
      
      }
      
    0 讨论(0)
提交回复
热议问题