Code Snippet : Bridge Pattern or Strategy Pattern?

69 Views Asked by At

The following code is an example of Strategy Design Pattern or Bridge Design Pattern. If Strategy , why not Bridge and vice-versa. I am quite confused after reading different articles and comments. Both patterns appear to have similar UMLs.

public interface IDriveStrategy
{
    void Drive();
}

public class NormalDriveStrategy : IDriveStrategy
{
    public void Drive(){    }
}

public class SportsDriveStrategy : IDriveStrategy
{
    public void Drive(){    }
}

public class Vehicle
{
    private readonly IDriveStrategy _driveStrategy;
    public Vehicle(IDriveStrategy driveStrategy)
    {
        _driveStrategy = driveStrategy;
    }
    public void Engine(){   }
    public void Display(){  }
    public void PerformDrive()
    {
        _driveStrategy.Drive();
    }
}

public class GoodsVehicle : Vehicle
{
    public GoodsVehicle() : base(new NormalDriveStrategy())
    {
    }
}

public class SportVehicle : Vehicle
{
    public SportVehicle() : base(new SportsDriveStrategy())
    {
    }
}

public class OffRoadVehicle : Vehicle
{
    public OffRoadVehicle() : base(new SportsDriveStrategy())
    { 
    }
}
0

There are 0 best solutions below