예제 #1
0
        public static void Factory()
        {
            var bookReaderList = new List <BookReader>
            {
                new AdventureBookReader(),
                new FantasyBookReader(),
                new HorrorBookReader()
            };

            foreach (var reader in bookReaderList)
            {
                Console.WriteLine(reader.GetType().ToString());

                reader.DisplayOwnedBooks();

                Console.WriteLine();
            }

            var  genericReader = new AdventureBookReader();
            Book book          = genericReader.BuyBook2 <Encyclopedia>();

            Console.WriteLine(book.GetType().ToString());

            Console.ReadLine();
        }
예제 #2
0
        private static void FactoryMethod()
        {
            var bookReaderList = new List <BookReader>();

            bookReaderList.Add(new AdventureBookReader());
            bookReaderList.Add(new FantasyBookReader());
            bookReaderList.Add(new HorrorBookReader());

            foreach (BookReader reader in bookReaderList)
            {
                Console.WriteLine(reader.GetType().ToString());
                // Language agnostic solution
                reader.DisplayOwnedBooks();

                Console.WriteLine();
            }

            // C# specific solution using generics
            var  genericReader = new AdventureBookReader();
            Book book          = genericReader.BuyBook <Encyclopedia>();

            Console.WriteLine(book.GetType().ToString());

            Console.ReadKey();
        }