What is Dependency Injection IoC

Examples of high coupling

Here is a father telling stories to children examples are instantiated by the new class

 class Program
    {
        static void Main(string[] args)
        {
            Father father = new Father();
            father.Read();
        }
    }

    public class Book
    {
        public string GetContent()
        {
            return  " A long time ago, a man called Pangea " ;
        }
     }

    public class Father
    {
        public void Read()
        {
            Book book = new Book();
            Console.WriteLine ( " father began telling stories " );
            Console.WriteLine(book.GetContent());
        }
    }

Suddenly one day, the child does not want to hear a story, I want to hear the contents of the newspaper. There is need to add a class newspaper

  class Program
    {
        static void Main(string[] args)
        {
            Father father = new Father();
            father.Read();
        }
    }

    public class Book
    {
        public string GetContent()
        {
            return  " A long time ago, a man called Pangea " ;
        }
     }

    public class paper
    {
        public string GetContent()
        {
            return  " newspaper content .... " ;
        }
    }

    public class Father
    {
        public void Read()
        {
            // Book Book = new new Book ();
             // Console.WriteLine ( "father began telling stories");
             // Console.WriteLine (book.GetContent ()); 

            Paper Paper = new new Paper ();
            Console.WriteLine ( " father began reading the newspaper " );
            Console.WriteLine(paper.GetContent());
        }
    }

 

Father found here is modified, suddenly one day they want to listen to children read the news on the page. . . Here Father is not going to modify the content of it? That is certainly so Father will continue to be modified, the coupling is too high, resulting in code that is frequently changed. Here you can not decouple it? Hey, it interfaces with it, reduce the coupling. Defined interfaces

    public interface IReader
    {
        string GetContent();
    }

 

Let Book and paper inherit IReader

 class Program
    {
        static void Main(string[] args)
        {
            // first reading to 
            Father Father = new new Father ( new new Book ());
            father.Read();
        }
    }

    public interface IReader
    {
        string GetContent();
    }

    public class Book:IReader
    {
        public string GetContent()
        {
            return  " A long time ago, a man called Pangea " ;
        }
     }

    public class Paper : IReader
    {
        public string GetContent()
        {
            return  " newspaper content .... " ;
        }
    }

    public class Father
    {

        public IReader Reader { GET ; SET ;}
         // the constructor example interfaces 
        public Father (IReader Reader) {
            Reader = reader;
        }
        public void Read()
        {
            // Book Book = new new Book ();
             // Console.WriteLine ( "father began telling stories");
             // Console.WriteLine (book.GetContent ());

            // Paper Paper Paper = new new ();
             // Console.WriteLine ( "father began reading the newspaper");
             // Console.WriteLine (paper.GetContent ()); 

            Console.WriteLine ( " Dad started .. " ) ;
            Console.WriteLine(Reader.GetContent());

        }
    }

 

 

 In a change Paper

 static void Main(string[] args)
  {
            // first reading to 
            Father Father = new new Father ( new new Paper ());
            father.Read();
  }

 

 

 This realization of decoupling. Without frequent repair Father, but at the time of the call, just call.

 

Guess you like

Origin www.cnblogs.com/youmingkuang/p/11963404.html