INTRODUCTION
The strategy design pattern will choose the different algorithms or implementations dynamically. We may have different strategies for a requirement, but dynamically it can be accessed based on the need. In the real world, a different person will have a different solution for the same problem, but based on the need anyone of the solution will be utilized.
USECASE
Assembling different types of vehicles.
IMPLEMENTATION
PROBLEM
Consider we want to assemble the bike based on the type client wants. The possible implementation would be below
- package jbr.strategypattern.problem;
- import jbr.common.VehicleType;
- import jbr.common.model.Vehicle;
- public class AssembleVehicle {
- public static void main(String[] args) {
- Vehicle car = assemble(VehicleType.CAR);
- System.out.println(car.getVehicleType());
- }
- public static Vehicle assemble(VehicleType vehicleType) {
- Vehicle vehicle = null;
- switch (vehicleType) {
- case CAR:
- vehicle = new Vehicle(4, "honda", VehicleType.CAR, 100000);
- break;
- case BIKE:
- vehicle = new Vehicle(2, "hero", VehicleType.BIKE, 300000);
- break;
- case BUS:
- vehicle = new Vehicle(4, "ashok leyland", VehicleType.BUS, 1300000);
- break;
- default:
- break;
- // add implementations for new type of vehicles
- }
- return vehicle;
- }
- }
SOLUTION
The issue with the above code is, it is not easily maintainable. We need to alter a lot of code if any new strategy needs to be added in future. The solution would be below.
Create an Enum for the different type of vehicles.
- package jbr.common;
- public enum VehicleType {
- CAR, BIKE, BUS;
- }
Create a model for Vehicle.
- public class Vehicle {
- private int noOfWheels;
- private String manufacturer;
- private VehicleType vehicleType;
- private int price;
- //constructor
- //getters and setters
- }
Create a strategy (interface) for bike assembling.
- package jbr.strategypattern.solution;
- import jbr.common.model.Vehicle;
- public interface Assembler {
- Vehicle assemble();
- }
Now create separate strategies for Car, Bike and Bus.
Car assembling strategy.
- package jbr.strategypattern.solution;
- import jbr.common.VehicleType;
- import jbr.common.model.Vehicle;
- public class CarAssembler implements Assembler {
- @Override
- public Vehicle assemble() {
- return new Vehicle(4, "honda", VehicleType.CAR, 100000);
- }
- }
Bike assembling strategy.
- package jbr.strategypattern.solution;
- import jbr.common.VehicleType;
- import jbr.common.model.Vehicle;
- public class BikeAssembler implements Assembler {
- @Override
- public Vehicle assemble() {
- return new Vehicle(2, "hero", VehicleType.BIKE, 300000);
- }
- }
Bus assembling strategy.
- package jbr.strategypattern.solution;
- import jbr.common.VehicleType;
- import jbr.common.model.Vehicle;
- public class BusAssembler implements Assembler {
- @Override
- public Vehicle assemble() {
- return new Vehicle(4, "ashok leyland", VehicleType.BUS, 1300000);
- }
- }
Now create a client which accepts different strategies dynamically based on the need of the client.
- package jbr.strategypattern.solution;
- import jbr.common.VehicleType;
- import jbr.common.model.Vehicle;
- public class MyVehicle {
- public static void main(String[] args) {
- Vehicle car = assemble(VehicleType.CAR);
- System.out.println(car.getVehicleType());
- }
- public static Vehicle assemble(VehicleType vehicleType) {
- CarAssembler carAssembler = new CarAssembler();
- BikeAssembler bikeAssembler = new BikeAssembler();
- Vehicle vehicle = null;
- switch (vehicleType) {
- case CAR:
- vehicle = carAssembler.assemble();
- break;
- case BIKE:
- vehicle = bikeAssembler.assemble();
- break;
- default:
- break;
- // add implementations for new type of vehicles
- }
- return vehicle;
- }
- }
Still we have all the logic to call the different strategies into the client code itself, now we will try to enhance our client code lighter by creating a factory for assembling different vehicles based on the type.
Create a factory.
- package jbr.strategypattern.solution;
- import jbr.common.VehicleType;
- public class AssemblerFactory {
- public static Assembler assemble(VehicleType vehicleType) {
- switch (vehicleType) {
- case CAR:
- return new CarAssembler();
- case BIKE:
- return new BikeAssembler();
- case BUS:
- return new BusAssembler();
- default:a
- break;
- }
- return null;
- }
- }
Now our enhanced client code will be.
- package jbr.strategypattern.solution;
- import jbr.common.VehicleType;
- import jbr.common.model.Vehicle;
- public class MyVehicleEnhanced {
- public static void main(String[] args) {
- Vehicle vehicle = assemble(VehicleType.CAR);
- System.out.println(vehicle.toString());
- vehicle = assemble(VehicleType.BIKE);
- System.out.println("\n" + vehicle.toString());
- vehicle = assemble(VehicleType.BUS);
- System.out.println("\n" + vehicle.toString());
- }
- public static Vehicle assemble(VehicleType vehicleType) {
- return AssemblerFactory.assemble(vehicleType)
- .assemble();
- }
- }
OUTPUT
Type: CAR
No Of Wheels: 4
Manufacturer: honda
Price: 100000
Type: BIKE
No Of Wheels: 2
Manufacturer: hero
Price: 300000
Type: BUS
No Of Wheels: 4
Manufacturer: ashok leyland
Price: 1300000
|
Hope this example clarifies the Strategy Design Pattern. Please share your thoughts in comment box.
Happy Knowledge Sharing!!!
No comments :
Post a Comment