Advertisement
Guest User

Untitled

a guest
Nov 18th, 2022
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.74 KB | None | 0 0
  1. Задача.
  2. Дан ООП часть кода, который описывает бизнес-логику загрузки в некую базу данных о собаках.
  3. Не имеет значения куда и откуда. Поставляется список собак с их свойствами: порода, цвет,
  4. картинка, имя, имя собственника, возраст. Сам ООП написан с избытком.
  5. Требуется:
  6. - почистить ООП код, оставить только непосредственный код для данного процесса
  7. - определить паттерны проектирования, которые здесь использованы, ответить на вопрос - зачем
  8. и почему их здесь применили и нужны ли они вообще (последнее будет исходить от вашей чистки
  9. кода)
  10. - добавить клиентский код. На вход построить загрузку данных из CSV файла. По ходу ООП
  11. реализовать функции profile() и findDog(). Ограничений в количестве клиентского кода нет
  12. (зависит от вашего времени)
  13. - запустить код (по возможности)
  14. - нарисовать UML-диаграмму
  15.  
  16. <?php
  17.  
  18. namespace ;
  19.  
  20. interface DogArrayInterface
  21. {
  22.     public function fill();
  23. }
  24.  
  25. class DogArray implements DogArrayInterface
  26. {
  27.     protected $dogArray;
  28.     protected $dogs = [];
  29.  
  30.     public function __construct(DogArrayInterface $dogArray)
  31.     {
  32.         $this->dogArray = $dogArray;
  33.     }
  34.  
  35.     public function fill()
  36.     {
  37.         return $this->dogArray->fill();
  38.     }
  39.    
  40.     public function newDog($name, $age, $owner, $group)
  41.     {
  42.         $this->dogs[] = new Dog($name, $age, $owner, $group);
  43.     }
  44. }
  45.  
  46. class Group extends DogArray
  47. {
  48.     private $groups = [];
  49.    
  50.     public function fill
  51.     (   string $name,
  52.         string $age,
  53.         string $owner,
  54.         string $breed,
  55.         string $image,
  56.         string $color
  57.     ) {
  58.         $group = $this->getGroup($breed, $image, $color);
  59.         $this->newDog($name, $age, $owner, $group);
  60.     }
  61.    
  62.     public function getGroup(
  63.         string $breed,
  64.         string $image,
  65.         string $color
  66.     ) {
  67.         $key = 'key_' . $breed . $image . $color;
  68.        
  69.         if (!isset($this->groups[$key])) {
  70.             $this->groups[$key] = new DogGroup($breed, $image, $color);
  71.         }
  72.  
  73.         return $this->groups[$key];
  74.     }
  75.  
  76.     public function findDog(array $query)
  77.     {
  78.         // the query to find one dog
  79.     }
  80. }
  81.  
  82. class DogGroup
  83. {
  84.     public $breed;
  85.     public $image;
  86.     public $color;
  87.  
  88.  
  89.     public function __construct(string $breed, string $image, string $color)
  90.      {
  91.         $this->breed = $breed;
  92.         $this->image = $image;
  93.         $this->color = $color;
  94.     }
  95.  
  96.     public function profile(string $name, string  $age, string $owner)
  97.     {
  98.         // Show info about one dog
  99.     }
  100. }
  101.  
  102. class Dog
  103. {
  104.     public $name;
  105.     public $age;
  106.     public $owner;
  107.  
  108.     private $group;
  109.  
  110.     public function __construct(string $name, string $age, string $owner, DogGroup $group)
  111.     {
  112.         $this->name = $name;
  113.         $this->age = $age;
  114.         $this->owner = $owner;
  115.         $this->group = $group;
  116.     }
  117.  
  118.     public function render()
  119.     {
  120.         $this->group->profile($this->name, $this->age, $this->owner);
  121.     }
  122. }
  123.  
  124.  
  125. /**
  126.  * Клиент: создается огромнейшая база собак со свойствами каждой: порода, изображение, цвет, имя, собственник, возраст
  127.  */
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement