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

后端 未结 14 2182
予麋鹿
予麋鹿 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:07

    In the Strategy pattern, the activities of the "Parent" for a particular operation are constant whereas the activities of the "Child" can vary. However, in the Bridge Pattern, the activities of the Parent, as well as the Child, can vary.

    So, for example,

    public class Ticket {
        
        Date dateOfTravel;
        int distance;
        Vehicle vehicle;
        Seat  seat;
        
        public float getTotalFare(){
             //depends on 
                   //Distance
                   //Vehicle - whether Vehicle is AC or non-AC. 
                   //Seat - based on the location of the Seat.
         
            //Fare = vehicleBaseFare*seatMultiplier*distance
    
        }
        
    }
    

    In the above, the variations depend on the Parent (distance) as well as the children (Vehicle and Seat). So, here Vehicle and Seat both acted like Bridge.

    Now, here

    public class Vehicle {
    
       TrackingDevice device;
    
       public Coordinates getCoordinates(){
           return device.getCoordinates();
       }
    }
    

    Here, the Parent's role was constant, i.e., nothing! So, this was a Strategy pattern.

提交回复
热议问题