Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4.  
  5. namespace WzorceProjektowe
  6. {
  7. // Nie zmieniaj poniższego kodu
  8. public enum Type { LargeTree, Tree, Bush }
  9.  
  10. // Nie zmieniaj poniższego kodu
  11. public interface Plant
  12. {
  13. void Display(int positionX, int positionY);
  14. }
  15. // Nie zmieniaj poniższej klasy
  16. public class LargeTree : Plant
  17. {
  18. private string Texture = "large_tree.png";
  19. public void Display(int x, int y)
  20. {
  21. Console.WriteLine($"Duże drzewo (plik \"{Texture}\") znajduje się na pozycji {x},{y}");
  22. }
  23. }
  24. // Nie zmieniaj poniższej klasy
  25. public class Tree : Plant
  26. {
  27. private string Texture = "tree.png";
  28. public void Display(int x, int y)
  29. {
  30. Console.WriteLine($"Normalne drzewo (plik \"{Texture}\") znajduje się na pozycji {x},{y}");
  31. }
  32. }
  33. // Nie zmieniaj poniższej klasy
  34. public class Bush : Plant
  35. {
  36. private string Texture = "bush.png";
  37. public void Display(int x, int y)
  38. {
  39. Console.WriteLine($"Krzak (plik \"{Texture}\") znajduje się na pozycji {x}, {y}");
  40. }
  41. }
  42. public class PlantFactory
  43. {
  44. // TODO
  45. private Dictionary<Type, Plant> flyweights = new Dictionary<Type, Plant>();
  46. public Plant GetPlant(Type category)
  47. {
  48. Plant flyweight = null;
  49. if (flyweights.ContainsKey(category))
  50. {
  51. flyweight = flyweights[category] as Plant;
  52. Console.WriteLine("Wykorzystuję istniejący obiekt");
  53. }
  54. else
  55. {
  56. switch(category)
  57. {
  58. case Type.Bush:
  59. flyweight = new Bush();
  60. break;
  61. case Type.LargeTree:
  62. flyweight = new LargeTree();
  63. break;
  64. case Type.Tree:
  65. flyweight = new Tree();
  66. break;
  67.  
  68. }
  69. flyweights.Add(category, flyweight);
  70. Console.WriteLine("Tworzę nowy obiekt typu {0}", category);
  71. }
  72. return flyweight;
  73. }
  74.  
  75. }
  76.  
  77. // Nie zmieniaj poniższej klasy
  78. class Program
  79. {
  80. static void Main(string[] args)
  81. {
  82. var factory = new PlantFactory();
  83.  
  84. var plant = factory.GetPlant(Type.Tree);
  85. plant.Display(0, 0);
  86. plant = factory.GetPlant(Type.LargeTree);
  87. plant.Display(0, 7);
  88. plant = factory.GetPlant(Type.Tree);
  89. plant.Display(3, 16);
  90. plant = factory.GetPlant(Type.Bush);
  91. plant.Display(10, 9);
  92. plant = factory.GetPlant(Type.Tree);
  93. plant.Display(7, 7);
  94. plant = factory.GetPlant(Type.LargeTree);
  95. plant.Display(20, 0);
  96. plant = factory.GetPlant(Type.Tree);
  97. plant.Display(3, 28);
  98. plant = factory.GetPlant(Type.Bush);
  99. plant.Display(1, 5);
  100. plant = factory.GetPlant(Type.Tree);
  101. plant.Display(8, 8);
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement