void Start()
    {
        WallE   wallE   = new WallE();
        Robocop robocop = new Robocop();

        // Calling Move() (from IRobotHelper)
        // First it will execute the shared functionality, as specified in IRobotHelper
        // Then it will execute any implementation-specific functionality,
        // depending on which class called it. In this case, WallE's OnMove().
        wallE.Move(1);
        // Now if we call the same Move function on a different implementation of IRobot
        // It will again begin by executing the shared functionality, as specified in IRobotHlper's Move function
        // And then it will proceed to executing Robocop's OnMove(), for Robocop-specific functionality.
        robocop.Move(1);
        // The whole concept is similar to inheritence, but for interfaces.
        // This structure offers an - admittedly dirty - way of having some of the benefits of a multiple inheritence scheme in C#, using interfaces.
    }
Example #2
0
        static void Main(string[] args)
        {
            Robot R1 = new Robocop(new FunkyMove(), new laserAttack());

            R1.Show();
            R1.DoMove();
            R1.DoAttack();

            Console.WriteLine();
            Console.WriteLine("-----------------------------------");
            Console.WriteLine();

            Robot R2 = new Grendaizer(new CurrvyMove(), new RocketAttack());

            R2.Show();
            R2.DoMove();
            R2.DoAttack();

            Console.WriteLine();
            Console.WriteLine("-----------------------------------");
            Console.WriteLine();
            Console.WriteLine("now changing behaviors!");
            Console.WriteLine();
            Console.WriteLine("-----------------------------------");
            Console.WriteLine();

            R1.MoveAlgorithem   = new CurrvyMove();
            R1.AttackAlgorithem = new RocketAttack();

            R1.Show();
            R1.DoMove();
            R1.DoAttack();

            Console.WriteLine();
            Console.WriteLine("-----------------------------------");
            Console.WriteLine();

            R2.MoveAlgorithem   = new FunkyMove();
            R2.AttackAlgorithem = new laserAttack();

            R2.Show();
            R2.DoMove();
            R2.DoAttack();
        }