views:

51

answers:

3

i writed some codes about oop pattern. But i cannot see clearly these two kind of usage advantage disadvantagages:

Firs one:


     class Program
    {
        static void Main(string[] args)
        {
            Report rp = new Report();
            rp.MakeReport();
            rp = new ExcelReport();
            rp.MakeReport();
            rp = new PdfReport();
            rp.MakeReport();
            rp = new WordReport();
            rp.MakeReport();
            Console.ReadKey();
       }
    }

Second usage

 class Program
    {
        static void Main(string[] args)
        {
            Report[] rp = new Report[4];
            rp[0] = new Report();
            rp[1] = new ExcelReport();
            rp[2] = new PdfReport();
            rp[3] = new WordReport();
            rp[0].MakeReport();
            rp[1].MakeReport();
            rp[2].MakeReport();
            rp[3].MakeReport();
            Console.ReadKey();
        }
    }

Class stracture:

   class Report
    {
       public virtual void MakeReport()
        {
            Console.WriteLine("Simple Report preparing...");
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine("Simple Report prepared...");
        }
    }

    class ExcelReport : Report
    {
        public override void MakeReport()
        {
            Console.WriteLine("Excel Report preparing...");
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine("Excel Report prepared...");
        }
    }

    class PdfReport : Report
    {
        public override void MakeReport()
        {
            Console.WriteLine("Pdf Report preparing...");
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine("Pdf Report prepared...");
        }
    }

    class WordReport : Report
    {
        public override void MakeReport()
        {
            Console.WriteLine("Word Report preparing...");
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine("Word Report prepared...");
        }
    }

What is the difference first one and second one? which one can i prefer ? what kind of stuation can i use one of them? What is adv or disAdv between two kind of usage?

A: 

I don't see any difference between the two.

If you want to fire-and-forget a report, use the first one. If you want to hold on to your report objects for some other calls, cache them in an array as in the second case. Not much difference really.

Gishu
i think that you make me realize : second one only caching?
Phsika
+2  A: 

The disadvantage of the second one is that explicit indexes are hard to maintain (come back later and modify), or even to copy-paste-modify. A typo will be very hard to find, and won't generate a compiler error.

Instead of either of these, you may want to consider doing a more common polymorphic practice: iterating over a list/array of the base type. A big advantage of this is that the code can be formatted to be both very brief, and easy to read.

class Program
{
    static void Main(string[] args)
    {
        Report[] reports = new Report[]
        {
            new Report(),
            new ExcelReport(),
            new PdfReport(),
            new WordReport(),
        };

        foreach(Report report in reports)
            report.MakeReport();

        Console.ReadKey();
    }
}

Also, in many cases, collection classes are more flexible/convenient than arrays. For example, you can add new values at a later time to a List<Report>:

class Program
{
    static void Main(string[] args)
    {
        List<Report> reports = new List<Report>()
        {
            new Report(),
            new ExcelReport(),
            new PdfReport(),
            new WordReport(),
        };

        foreach(Report report in reports)
            report.MakeReport();

        report.Add(new ExcelReport());

        foreach(Report report in reports)
            report.MakeReport();

        Console.ReadKey();
    }
}
Merlyn Morgan-Graham
+1  A: 

If you don't want to keep a reference to the report objects, you could do this:

class Program
{
    static void Main(string[] args)
    {
        new Report().MakeReport();
        new ExcelReport().MakeReport();
        new PdfReport().MakeReport();
        new WordReport().MakeReport();

        Console.ReadKey();
   }
}
Douglas