views:

56

answers:

1

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);
       }
}
}

+3  A: 

Your example does not related to SRP. It relates another OO principle that "Program To Interfaces". I recommend to go for the first implementation.

SRP says, the class should have only one reason to change. In your case you have two distinct objects, ReportManager and Report. So as per SRP, ReportManager should only be responsible for Managing the instances of reports and Report should be responsible for reporting purpose. ReportManager can scale to contain information about various types of report implementation available as configuration and may be also take responsibility of creating the instances sometime.

this. __curious_geek
There's also the "Liskov substitution principle", which is close to "Program to interfaces".
ewernli