none
Single Responsibility Principle usage how can i call sub method correctly? RRS feed

  • Question

  • i try to learn SOLID prencibles. i writed two type of code style. which one is :
    1)Single Responsibility Principle_2.cs : if you look main program all instance generated from interface
    1)Single Responsibility Principle_3.cs : if you look main program all instance genareted from normal class 
    My question: which one is correct usage? which one can i prefer?

    namespace Single_Responsibility_Principle_2
    {
      class Program
      {
        static void Main(string[] args)
        {
          IReportManager raporcu = new ReportManager();
           IReport wordraporu = new WordRaporu();
    
           raporcu.RaporHazırla(wordraporu, "data");
          Console.ReadKey();
        }
      }
    
      interface IReportManager
      {
        void RaporHazırla(IReport rapor, string bilgi);
      }
    
      class ReportManager : IReportManager
      {
        public void RaporHazırla(IReport rapor, string bilgi)
        {
          rapor.RaporYarat(bilgi);
        }
      }
    
    
      interface IReport
      {
        void RaporYarat(string bilgi);
      }
    
      class WordRaporu : IReport
      {
        public void RaporYarat(string bilgi)
        {
          Console.WriteLine("Word Raporu yaratıldı:{0}",bilgi);
        }
    
      }
    
      class ExcellRaporu : IReport
      {
        public void RaporYarat(string bilgi)
        {
         Console.WriteLine("Excell raporu yaratıldı:{0}",bilgi);
        }
    }
    
      class PdfRaporu : IReport
      {
        public void RaporYarat(string bilgi)
        {
         Console.WriteLine("pdf raporu yaratıldı:{0}",bilgi);
        }
    }
    }

    Second 0ne all instance genareted from normal class

    namespace Single_Responsibility_Principle_3
    {
      class Program
      {
        static void Main(string[] args)
        {
          WordRaporu word = new WordRaporu();
          ReportManager manager = new ReportManager();
          manager.RaporHazırla(word,"test");
        }
      }
       interface IReportManager
      {
        void RaporHazırla(IReport rapor, string bilgi);
      }
    
      class ReportManager : IReportManager
      {
        public void RaporHazırla(IReport rapor, string bilgi)
        {
          rapor.RaporYarat(bilgi);
        }
      }
    
    
      interface IReport
      {
        void RaporYarat(string bilgi);
      }
    
      class WordRaporu : IReport
      {
        public void RaporYarat(string bilgi)
        {
          Console.WriteLine("Word Raporu yaratıldı:{0}",bilgi);
        }
    
      }
    
      class ExcellRaporu : IReport
      {
        public void RaporYarat(string bilgi)
        {
         Console.WriteLine("Excell raporu yaratıldı:{0}",bilgi);
        }
    }
    
      class PdfRaporu : IReport
      {
        public void RaporYarat(string bilgi)
        {
         Console.WriteLine("pdf raporu yaratıldı:{0}",bilgi);
        }
    }
    }

    Saturday, May 29, 2010 8:48 AM

All replies

  • Maybe I missed something, at first glance both your examples seem to implement interfaces.

    As a  general principle either inheriting classes or interfaces are valid approaches.  The choice is more one of style and type of application. 

    For example, if your concrete classes will have a set of complex behaviours which will not vary then you may find that inheriting from one class which has all the functionality is simplest and best.

    On the other hand, if behaviour in concrete classes will vary widely then that would require an inordinately complex base class so interfaces would be better suited. 

    Saturday, May 29, 2010 10:51 AM