Dependency Inversion Principle

Posted by Chris Paine on Programmers See other posts from Programmers or by Chris Paine
Published on 2014-08-23T00:58:55Z Indexed on 2014/08/23 10:34 UTC
Read the original article Hit count: 233

Filed under:

I have been studying also S.O.L.I.D. and watched this video:
https://www.youtube.com/watch?v=huEEkx5P5Hs 01:45:30 into the video he talks about the Dependency Inversion Principle and I am scratching my head??? I had to simplify it(if possible) to get it through this thick scull of mine and here is what I came up with. Code on the marked My_modified_code my version, code marked Original DIP video version. Can I accomplish the same with the latter code? Thanks in advance.

Original:

namespace simple.main
{
     class main
    {
        static void Main()
        {
            FirstClass FirstClass = new FirstClass(new OtherClass());
            FirstClass.Method();
            Console.ReadKey();

            //tempClass temp = new OtherClass();
            //temp.Method();
        }
    }

     public class FirstClass
     {
         private tempClass _LastClass;
         public FirstClass(tempClass tempClass)//ctor
         {
             _LastClass = tempClass;
         }

         public void Method()
         {
             _LastClass.Method();
         }
     }

    public abstract class tempClass{public abstract void Method();}

    public class LASTCLASS : tempClass
    {
        public override void Method()
        {
            Console.WriteLine("\nHello World!");
        }
    }

    public class OtherClass : tempClass
    {
        public override void Method()
        {
            Console.WriteLine("\nOther World!");
        }
    }
}

My_modified_code:

namespace simple.main
{
     class main
    {
        static void Main()
        {
            //FirstClass FirstClass = new FirstClass(new OtherClass());
            //FirstClass.Method();
            //Console.ReadKey();

            tempClass temp = new OtherClass();
            temp.Method();
        }
    }

     //public class FirstClass
     //{
     //    private tempClass _LastClass;
     //    public FirstClass(tempClass tempClass)//ctor
     //    {
     //        _LastClass = tempClass;
     //    }

     //    public void Method()
     //    {
     //        _LastClass.Method();
     //    }
     //}

    public abstract class tempClass{public abstract void Method();}

    public class LASTCLASS : tempClass
    {
        public override void Method()
        {
            Console.WriteLine("\nHello World!");
        }
    }

    public class OtherClass : tempClass
    {
        public override void Method()
        {
            Console.WriteLine("\nOther World!");
        }
    }

© Programmers or respective owner

Related posts about solid