Exemplo n.º 1
0
        public void EngineInSeabird()
        {
            IAircraft seabird = new Seabird.Seabird();

            seabird.TakeOff();
            Assert.AreEqual(100, seabird.Height);
            Assert.IsTrue(seabird.Airborne);
        }
Exemplo n.º 2
0
        public void IncreaseSeabirdSpeed()
        {
            IAircraft seabird = new Seabird.Seabird();

            ((ISeacraft)seabird).IncreaseRevs();
            ((ISeacraft)seabird).IncreaseRevs();
            Assert.AreEqual(0, seabird.Height);
            Assert.AreEqual(20, ((ISeacraft)seabird).Speed);
        }
Exemplo n.º 3
0
        public static void Main()
        {
            // Classic usage of an adapter
            Console.WriteLine("\nExperiment 1: Use the engine in the Seabird");
            IAircraft seabird = new Seabird();

            seabird.TakeOff(); // And automatically increases speed
            Console.WriteLine("The Seabird took off");

            // Two-way adapter: using seacraft instructions on an IAircraft object
            // (where they are not in the IAircraft interface)
            Console.WriteLine("\nExperiment 1.5: Increase the speed of the Seabird:");
            (seabird as ISeacraft).IncreaseRevs();
            (seabird as ISeacraft).IncreaseRevs();
            if (seabird.Airborne)
            {
                Console.WriteLine("Seabird flying at height " + seabird.Height +
                                  " meters and speed " + (seabird as ISeacraft).Speed + " knots");
            }
            Console.WriteLine("Experiments successful; the Seabird flies!");

            Console.WriteLine("\nExperiment 2: Increase the speed of the Seabird2:");
            ISeacraft seabird2 = new SeabirdToAircraft();

            seabird2.IncreaseRevs();
            seabird2.IncreaseRevs();
            Console.WriteLine($"Seabird2 has speed {seabird2.Speed} knots");

            Console.WriteLine("\nExperiment 2.5: Use the engine in the Seabird2");
            (seabird2 as IAircraft).TakeOff();
            Console.WriteLine("The Seabird2 took off");
            if ((seabird2 as IAircraft).Airborne)
            {
                Console.WriteLine("Seabird2 flying at height " + (seabird2 as IAircraft).Height +
                                  " meters and speed " + seabird2.Speed + " knots");
            }
            Console.WriteLine("Experiments successful; the Seabird2 flies!");

            Console.ReadKey();
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            //Observers
            ControlPanel monitor1 = new Monitor();
            ControlPanel monitor2 = new Monitor();
            ControlPanel monitor3 = new Monitor();

            // No adapter
            Console.WriteLine("Experiment 1: test the aircraft engine");
            IAircraft aircraft = new Aircraft();

            //Attaching the observer to the subject
            aircraft.Attach(monitor1);

            aircraft.TurnEngineOn();
            aircraft.Accelerate(50);
            aircraft.Deaccelerate(0);
            aircraft.Accelerate(200);
            aircraft.RaiseNose(40);
            aircraft.TakeOff();
            aircraft.DownNose(0);
            aircraft.Deaccelerate(100);
            aircraft.DownNose(-30);
            aircraft.Land();
            aircraft.Deaccelerate(0);

            //Showing the notifications
            Console.WriteLine(monitor1.Message);

            // Classic usage of an adapter
            Console.WriteLine("\nExperiment 2: Use the engine in the Seabird");
            IAircraft seabird = new Seabird();

            //Attaching the observer to the subject
            seabird.Attach(monitor2);

            seabird.TurnEngineOn();
            seabird.Accelerate(40);
            seabird.Deaccelerate(0);
            seabird.Accelerate(190);
            seabird.RaiseNose(50);
            seabird.TakeOff();
            seabird.DownNose(0);
            seabird.Deaccelerate(120);
            seabird.DownNose(-20);
            seabird.Land();
            seabird.Deaccelerate(0);

            //Showing the notifications
            Console.WriteLine(monitor2.Message);

            // Two-way adapter: using seacraft instructions on an IAircraft object
            // (where they are not in the IAircraft interface)
            Console.WriteLine("\nExperiment 3: Increase the speed of the Seabird:");

            //Attaching the observer to the subject
            (seabird as ISeacraft).Attach(monitor3);
            (seabird as ISeacraft).IncreaseRevs(500);

            //Showing the notifications
            Console.WriteLine(monitor3.Message);

            Console.WriteLine("Seabird flying at height " + seabird.Height + " meters and speed " + (seabird as ISeacraft).Knots + " knots");
            Console.WriteLine("Experiments successful; the Seabird flies!");

            Console.ReadLine();

            monitor1 = null;
            monitor2 = null;
            monitor3 = null;
            aircraft = null;
            seabird  = null;
        }