Two Way Adapter Pattern Code Review
Use the Two Way Adapter Pattern
Code Review
- Two-Way Adapter Pattern Code Review: Define the AirCraft Interface and Handler Object
- Two-Way Adapter Pattern Code Review: Define the Seacraft Interface and Handler Object
- Two-Way Adapter Pattern Code Review: Define the Seabird Two-Way Adapter Object
- Two-Way Adapter Pattern Code Review: Use the Two-Way Adapter Pattern
Code Walkthrough
The Program class creates an instance of both the class adapter (Aircraft) and the two-way adapter (SeabirdAdapter).
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Experiment 1: test the aircraft engine");
IAircraft aircraft = new Aircraft();
aircraft.TakeOff();
if (aircraft.Airbourne)
{
Console.WriteLine(String.Format("The aircraft engine is fine, flying at {0} meters.", aircraft.Height));
}
Console.WriteLine("\nExperiment 2: Use the engine in the Seabird"); IAircraft seabird = new SeabirdAdapter(); seabird.TakeOff(); Console.WriteLine("The Seabird took off");
Console.WriteLine("\nExperiment 3: Increase the speed of the Seabird:"); (seabird as SeabirdAdapter).IncreaseRevs(); (seabird as SeabirdAdapter).IncreaseRevs(); if (seabird.Airbourne) { Console.WriteLine(String.Format("Seabird flying at height {0} meters and speed {1} knots.", seabird.Height, (seabird as ISeacraft).Speed)); } Console.WriteLine("Experiments successful; the Seabird flies!");
Console.ReadLine(); } }
Console.WriteLine("\nExperiment 2: Use the engine in the Seabird"); IAircraft seabird = new SeabirdAdapter(); seabird.TakeOff(); Console.WriteLine("The Seabird took off");
Console.WriteLine("\nExperiment 3: Increase the speed of the Seabird:"); (seabird as SeabirdAdapter).IncreaseRevs(); (seabird as SeabirdAdapter).IncreaseRevs(); if (seabird.Airbourne) { Console.WriteLine(String.Format("Seabird flying at height {0} meters and speed {1} knots.", seabird.Height, (seabird as ISeacraft).Speed)); } Console.WriteLine("Experiments successful; the Seabird flies!");
Console.ReadLine(); } }
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!