allows the interface of an existing class to be used as another interface
Type 1: Object Adapter Pattern
Type 2: Class Adapter Pattern
Pick a fishing trip
Pick a boat
public interface iAdapterPattern
{
string getFishingTripDetails(string inTrip, string inBoat);
}
public abstract class AdapterPattern
{
public abstract iAdapterPattern getFishingTripDetails(string inTrip, string inBoat);
}
public class TunaTrip : iAdapterPattern
{
public string getFishingTripDetails(string inTrip, string inBoat)
{
return ("you requested a Tuna trip on the " + inBoat);
}
}
public class TroutTrip : iAdapterPattern
{
public string getFishingTripDetails(string inTrip, string inBoat)
{
return ("you requested a Trout trip");
}
}
public class GetFishingTrip : AdapterPattern
{
public override iAdapterPattern getFishingTripDetails(string inTrip, string inBoat)
{
switch (inTrip)
{
case "Tuna": return new TunaTrip();
case "Trout": return new TroutTrip();
default: throw new ApplicationException(string.Format("Trip '{0}' cannot be created", inTrip));
}
}
}
//end of Adapter class
//Controller methods
public ActionResult AdapterPattern()
{
return View();
}
public string GetFishingTrip(string inTrip, string inBoat)
{
switch (inTrip)
{
case "Tuna":
TunaTrip tuna = new TunaTrip();
return (tuna.getFishingTripDetails(inTrip, inBoat));
case "Trout":
TroutTrip trout = new TroutTrip();
return (trout.getFishingTripDetails(inTrip, inBoat));
default:
return string.Empty;
}
}
//end of Controller methods