Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- public class Encyclopedia : Book { }
- public class TreasureIsland : Book { }
- public class LordOfTheRings : Book { }
- public class Dracula : Book { }
- public class Book { }
- public abstract class BookReader{
- public BookReader(){
- Book = BuyBook();
- }
- public Book Book { get; set; }
- public abstract Book BuyBook();
- public Book BuyBook<T>() where T : Book, new(){
- return new T();
- }
- public void DisplayOwnedBooks(){
- string message;
- message = string.Format("Owned book: {0}", Book);
- Console.WriteLine(message);
- }
- }
- public abstract class GenericReader{
- public GenericReader()
- {
- Book = BuyBook();
- }
- public Book Book { get; set; }
- public abstract Book BuyBook();
- public Book BuyBook<T>() where T : Book, new()
- {
- return new T();
- }
- public void DisplayOwnedBooks()
- {
- string message;
- message = string.Format("Owned book: {0}", Book);
- Console.WriteLine(message);
- }
- }
- public class Encyklopedia : GenericReader{
- public override Book BuyBook(){
- return new Encyclopedia();
- }
- }
- public class AdventureBookReader : BookReader{
- public override Book BuyBook(){
- return new TreasureIsland();
- }
- }
- public class FantasyBookReader : BookReader{
- public override Book BuyBook(){
- return new LordOfTheRings();
- }
- }
- public class HorrorBookReader : BookReader{
- public override Book BuyBook(){
- return new Dracula();
- }
- }
- class MainClass{
- public static void Main (string[] args){
- GenericReader genericReader = new Encyklopedia();
- Book book = genericReader.BuyBook<Encyclopedia>();
- Console.WriteLine(book);
- Console.WriteLine("\n/* ********************************** */\n");
- List<BookReader> bookReaderList = new List<BookReader>();
- bookReaderList.Add(new AdventureBookReader());
- bookReaderList.Add(new FantasyBookReader());
- bookReaderList.Add(new HorrorBookReader());
- foreach (var reader in bookReaderList){
- Console.WriteLine(reader);
- Console.WriteLine(reader.Book + "\n");
- }
- Console.ReadKey();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement