Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Задача.
- Дан ООП часть кода, который описывает бизнес-логику загрузки в некую базу данных о собаках.
- Не имеет значения куда и откуда. Поставляется список собак с их свойствами: порода, цвет,
- картинка, имя, имя собственника, возраст. Сам ООП написан с избытком.
- Требуется:
- - почистить ООП код, оставить только непосредственный код для данного процесса
- - определить паттерны проектирования, которые здесь использованы, ответить на вопрос - зачем
- и почему их здесь применили и нужны ли они вообще (последнее будет исходить от вашей чистки
- кода)
- - добавить клиентский код. На вход построить загрузку данных из CSV файла. По ходу ООП
- реализовать функции profile() и findDog(). Ограничений в количестве клиентского кода нет
- (зависит от вашего времени)
- - запустить код (по возможности)
- - нарисовать UML-диаграмму
- <?php
- namespace ;
- interface DogArrayInterface
- {
- public function fill();
- }
- class DogArray implements DogArrayInterface
- {
- protected $dogArray;
- protected $dogs = [];
- public function __construct(DogArrayInterface $dogArray)
- {
- $this->dogArray = $dogArray;
- }
- public function fill()
- {
- return $this->dogArray->fill();
- }
- public function newDog($name, $age, $owner, $group)
- {
- $this->dogs[] = new Dog($name, $age, $owner, $group);
- }
- }
- class Group extends DogArray
- {
- private $groups = [];
- public function fill
- ( string $name,
- string $age,
- string $owner,
- string $breed,
- string $image,
- string $color
- ) {
- $group = $this->getGroup($breed, $image, $color);
- $this->newDog($name, $age, $owner, $group);
- }
- public function getGroup(
- string $breed,
- string $image,
- string $color
- ) {
- $key = 'key_' . $breed . $image . $color;
- if (!isset($this->groups[$key])) {
- $this->groups[$key] = new DogGroup($breed, $image, $color);
- }
- return $this->groups[$key];
- }
- public function findDog(array $query)
- {
- // the query to find one dog
- }
- }
- class DogGroup
- {
- public $breed;
- public $image;
- public $color;
- public function __construct(string $breed, string $image, string $color)
- {
- $this->breed = $breed;
- $this->image = $image;
- $this->color = $color;
- }
- public function profile(string $name, string $age, string $owner)
- {
- // Show info about one dog
- }
- }
- class Dog
- {
- public $name;
- public $age;
- public $owner;
- private $group;
- public function __construct(string $name, string $age, string $owner, DogGroup $group)
- {
- $this->name = $name;
- $this->age = $age;
- $this->owner = $owner;
- $this->group = $group;
- }
- public function render()
- {
- $this->group->profile($this->name, $this->age, $this->owner);
- }
- }
- /**
- * Клиент: создается огромнейшая база собак со свойствами каждой: порода, изображение, цвет, имя, собственник, возраст
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement