Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6.  
  7.  
  8. public class Encyclopedia : Book { }
  9. public class TreasureIsland : Book { }
  10. public class LordOfTheRings : Book { }
  11. public class Dracula : Book { }
  12. public class Book { }
  13.  
  14. public abstract class BookReader{
  15. public BookReader(){
  16. Book = BuyBook();
  17. }
  18.  
  19. public Book Book { get; set; }
  20.  
  21. public abstract Book BuyBook();
  22.  
  23. public Book BuyBook<T>() where T : Book, new(){
  24. return new T();
  25. }
  26.  
  27. public void DisplayOwnedBooks(){
  28. string message;
  29. message = string.Format("Owned book: {0}", Book);
  30. Console.WriteLine(message);
  31. }
  32. }
  33. public abstract class GenericReader{
  34. public GenericReader()
  35. {
  36. Book = BuyBook();
  37. }
  38.  
  39. public Book Book { get; set; }
  40. public abstract Book BuyBook();
  41. public Book BuyBook<T>() where T : Book, new()
  42. {
  43. return new T();
  44. }
  45. public void DisplayOwnedBooks()
  46. {
  47. string message;
  48. message = string.Format("Owned book: {0}", Book);
  49. Console.WriteLine(message);
  50. }
  51. }
  52.  
  53.  
  54. public class Encyklopedia : GenericReader{
  55. public override Book BuyBook(){
  56. return new Encyclopedia();
  57. }
  58. }
  59. public class AdventureBookReader : BookReader{
  60. public override Book BuyBook(){
  61. return new TreasureIsland();
  62. }
  63. }
  64. public class FantasyBookReader : BookReader{
  65. public override Book BuyBook(){
  66. return new LordOfTheRings();
  67. }
  68. }
  69. public class HorrorBookReader : BookReader{
  70. public override Book BuyBook(){
  71. return new Dracula();
  72. }
  73. }
  74.  
  75. class MainClass{
  76. public static void Main (string[] args){
  77. GenericReader genericReader = new Encyklopedia();
  78. Book book = genericReader.BuyBook<Encyclopedia>();
  79. Console.WriteLine(book);
  80.  
  81. Console.WriteLine("\n/* ********************************** */\n");
  82.  
  83. List<BookReader> bookReaderList = new List<BookReader>();
  84.  
  85. bookReaderList.Add(new AdventureBookReader());
  86. bookReaderList.Add(new FantasyBookReader());
  87. bookReaderList.Add(new HorrorBookReader());
  88. foreach (var reader in bookReaderList){
  89. Console.WriteLine(reader);
  90. Console.WriteLine(reader.Book + "\n");
  91. }
  92. Console.ReadKey();
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement