Saturday, 28 December 2013

Strategy Design pattern - Implementation [Travel]



Click here to watch in Youtube : https://www.youtube.com/watch?v=37zZUuAuk7w

Click the below Image to Enlarge
Strategy Design pattern - Implementation [Travel]



Strategy Design pattern - Implementation [Travel] - Class Diagram



































TravelStrategy.java

public interface TravelStrategy
{
public void gotoAirport();
}

AutoTravelStrategy.java

public class AutoTravelStrategy implements TravelStrategy
{

public void gotoAirport()
{
System.out.println("Traveler is going to Airport by Auto and will be charged 600 Rs");
}

}

BusTravelStrategy.java

public class BusTravelStrategy implements TravelStrategy
{

public void gotoAirport()
{
System.out.println("Traveler is going to Airport by bus and will be charged 200 Rs");
}

}

TaxiTravelStrategy.java

public class TaxiTravelStrategy implements TravelStrategy
{

public void gotoAirport()
{
System.out.println("Traveler is going to Airport by Taxi and will be charged 1000 Rs");
}

}


TrainTravelStrategy.java

public class TrainTravelStrategy implements TravelStrategy
{

public void gotoAirport()
{
System.out.println("Traveler is going to Airport by Train and will be charged 300 Rs");
}

}


TravelContext.java

public class TravelContext
{
private TravelStrategy travelStrategy;

// Client will set what TravelStrategy to use by calling this method
public void setTravelStrategy(TravelStrategy strategy)
{
this.travelStrategy = strategy;
}

public TravelStrategy getTravelStrategy()
{
return travelStrategy;
}

public void gotoAirport()
{
travelStrategy.gotoAirport();
}

}


Traveler.java

import java.util.Scanner;

public class Traveler
{

public static void main( String[] args )
{

System.out.println("Please enter Travel Type : 'Bus' or 'Train' or 'Taxi' or 'Auto' ");
Scanner scanner = new Scanner(System.in);
String travelType = scanner.next();
System.out.println("Travel type is : " + travelType);

TravelContext ctx = null;
ctx = new TravelContext();

if( "Bus".equalsIgnoreCase(travelType) )
{
ctx.setTravelStrategy(new BusTravelStrategy());
}
else if("Train".equalsIgnoreCase(travelType))
{
ctx.setTravelStrategy(new TrainTravelStrategy());
}
else if("Taxi".equalsIgnoreCase(travelType))
{
ctx.setTravelStrategy(new TaxiTravelStrategy());
}
else if("Auto".equalsIgnoreCase(travelType))
{
ctx.setTravelStrategy(new AutoTravelStrategy());
}
System.out.println("Travel context has : "+ctx.getTravelStrategy());
ctx.gotoAirport();

}
}


Output

Please enter Travel Type : 'Bus' or 'Train' or 'Taxi' or 'Auto'
bus
Travel type is : bus
Travel context has : BusTravelStrategy@1e4cbc4
Traveler is going to Airport by bus and will be charged 200 Rs


Please enter Travel Type : 'Bus' or 'Train' or 'Taxi' or 'Auto' 
train
Travel type is : train
Travel context has : TrainTravelStrategy@578ceb
Traveler is going to Airport by Train and will be charged 300 Rs

See also:

  • Strategy Design pattern - Introduction
  • Strategy Design pattern - Real Time Example [Compress files]
  • Strategy Design pattern - Real Time Example [Payment]
  • Strategy Design pattern - Real Time Example [Travel]
  • Strategy Design pattern - Real Time Example [Sorting]
  • Strategy Design pattern - Real Time Example [Search]
  • Strategy Design pattern - Class Diagram
  • Strategy Design pattern - Sequence Diagram
  • Strategy Design pattern - Implementation [Compress files]
  • Strategy Design pattern - Implementation [Payment]
  • Strategy Design pattern - Implementation [Search]
  • Strategy Design pattern - Implementation [Sort]
  • Strategy Design pattern - KeyPoints
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge