Two-Way Adapter pattern – example from C# Design Patterns book

Role:

The two-way adapter addresses the problem of two systems where the characteristics of one system have to be used in the other, and vice versa.

Output:

Experiment 1: test the aircraft engine
Aircraft engine takeoff
The aircraft engine is fine, flying at 200 meters

Experiment 2: Use the engine in the Seabird
Seacraft engine increas revs to 10 knots
Seacraft engine increas revs to 20 knots
Seacraft engine increas revs to 30 knots
Seacraft engine increas revs to 40 knots
Seacraft engine increas revs to 50 knots
The Seabird took off

Experiment 3: Increase the speed of the Seabird:
Seacraft engine increas revs to 60 knots
Seacraft engine increas revs to 70 knots
Seabird flying at height 300 meters and speed 70 knots
Experiments successful; the Seabird flies!

Implementation:

using System;

namespace SeabirdTwoWayAdapter
{
    public class ExperimentMakeSeaBirdFly
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Experiment 1: test the aircraft engine");
            IAircraft aircraft = new Aircraft();
            aircraft.TakeOff();
            if (aircraft.Airborne)
            {
                Console.WriteLine("The aircraft engine is fine, flying at {0} meters", aircraft.Height);
            }

            Console.WriteLine("\nExperiment 2: Use the engine in the Seabird");
            IAircraft seabird = new Seabird();
            seabird.TakeOff();
            Console.WriteLine("The Seabird took off");

            Console.WriteLine("\nExperiment 3: Increase the speed of the Seabird:");
            ((ISeacraft) seabird).IncreseRevs();
            ((ISeacraft) seabird).IncreseRevs();
            if (seabird.Airborne)
            {
                Console.WriteLine("Seabird flying at height {0} meters and speed {1} knots", seabird.Height,
                                  ((ISeacraft) seabird).Speed);
            }

            Console.WriteLine("Experiments successful; the Seabird flies!");
        }
    }

    public class Seabird : Seacraft, IAircraft
    {
        public Seabird()
        {
            this.Height = 0;
        }

        #region IAircraft Members

        public void TakeOff()
        {
            while (!this.Airborne)
            {
                this.IncreseRevs();
            }
        }

        public bool Airborne
        {
            get { return this.Height > 50; }
        }

        public int Height { get; private set; }

        #endregion

        public override void IncreseRevs()
        {
            base.IncreseRevs();
            if (Speed > 40)
            {
                this.Height += 100;
            }
        }
    }

    public interface ISeacraft
    {
        int Speed { get; }
        void IncreseRevs();
    }

    public class Seacraft : ISeacraft
    {
        public Seacraft()
        {
            this.Speed = 0;
        }

        #region ISeacraft Members

        public virtual void IncreseRevs()
        {
            this.Speed += 10;
            Console.WriteLine("Seacraft engine increas revs to {0} knots", this.Speed);
        }

        public int Speed { get; private set; }

        #endregion
    }

    public sealed class Aircraft : IAircraft
    {
        public Aircraft()
        {
            this.Airborne = false;
            this.Height = 0;
        }

        #region IAircraft Members

        public void TakeOff()
        {
            Console.WriteLine("Aircraft engine takeoff");
            this.Airborne = true;
            this.Height = 200;
        }

        public bool Airborne { get; private set; }

        public int Height { get; private set; }

        #endregion
    }

    public interface IAircraft
    {
        bool Airborne { get; }
        int Height { get; }
        void TakeOff();
    }
}

Bibliography

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s