D
D
Dmitry Baibukhtin2014-10-22 23:59:46
Yii
Dmitry Baibukhtin, 2014-10-22 23:59:46

Designing a web program, did I do it right?

Hello. Learning to write flexible programs according to MVC, SOLID, etc.
I need a task management module. Can you please tell me if I made the classes correctly?
I made inheritance from abstract classes and following interfaces, so that in the future, if new types of the Task or TasksManager class appear, then I could simply create another successor, for example, AbstractTasksManager and modify or supplement the script functionality without changing the contents of other classes.

<?php

class Task extends AbstractTask implements TaskInterface
{
}

class TasksManager extends AbstractTasksManager implements TasksManagerInterface
{

}

class AbstractTasksManager implements asksManagerInterface
{
    public function find(TaskInterface $task)
    {
        /** Внедрить метод find в TasksManager правильно?
         * Или он должен быть в отдельном классе?
         * Метод find использует скоупы Yii2
         */
    }

    public function create(TaskInterface $task)
    {
        /** Содержимое метода */
    }

    public function update(TaskInterface $task)
    {
        /** Содержимое метода */
    }

    public function delete(TaskInterface $task)
    {
        /** Содержимое метода */
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-10-23
@PiloTeZ

AbstractTask is only needed for DRY. In your case, it is still superfluous, add a little later. Even more, you can not even make the interface separately yet. If in the future we need to add another implementation of, say ... Task or TaskManager, any normal IDE will do the allocation of the class interface. Name this interface TaskManager and you will have an implementation of this ScheduledTaskManager and DBTaskManager... for example. And the client code will not affect such things at all.
I advise you to arm yourself with PhpSpec and thus check how conveniently you have designed the API of your services.

A
Anton Robul, 2014-10-23
@anton_slim

In an abstract class, at least one abstract method must be declared, and possibly some common methods for all descendants (of any public, protected, private scope). An interface can only have public methods declared.
Accordingly, judging from your abstract class logic, you need to make all methods an
Accordingly, your code will look something like this:

<?php

interface TasksManagerInterface 
{
  public function find(TaskInterface $task);
  public function create(TaskInterface $task);
  public function update(TaskInterface $task);
  public function delete(TaskInterface $task);
}

interface TaskInterface 
{
    public function find(TaskInterface $task);
    public function create(TaskInterface $task);
    public function update(TaskInterface $task);
    public function delete(TaskInterface $task);
}


abstract class AbstractTask implements TaskInterface
{
    protected function _commonTask() {
        // какой то общий метод для всех потомков AbstractTask
    }

    abstract public function find(TaskInterface $task);

    abstract public function create(TaskInterface $task);

    abstract public function update(TaskInterface $task);

    abstract public function delete(TaskInterface $task);
}

abstract class AbstractTasksManager implements TasksManagerInterface
{
    protected function _commonTasksManager() {
        // какой то общий код для всех потомков AbstractTasksManager
    }

    abstract public function find(TaskInterface $task);

    abstract public function create(TaskInterface $task);

    abstract public function update(TaskInterface $task);

    abstract public function delete(TaskInterface $task);
}



class Task extends AbstractTask implements TaskInterface
{
    public function find(TaskInterface $task)
    {
        /** Внедрить метод find в TasksManager правильно?
         * Или он должен быть в отдельном классе?
         * Метод find использует скоупы Yii2
         */

        // можно вызывать
        $this->_commonTask();
    }

    public function create(TaskInterface $task)
    {
        /** Содержимое метода */
    }

    public function update(TaskInterface $task)
    {
        /** Содержимое метода */
    }

    public function delete(TaskInterface $task)
    {
        /** Содержимое метода */
    }
}

class TasksManager extends AbstractTasksManager implements TasksManagerInterface
{
    public function find(TaskInterface $task)
    {
        /** Внедрить метод find в TasksManager правильно?
         * Или он должен быть в отдельном классе?
         * Метод find использует скоупы Yii2
         */

        // можно вызывать
        $this->_commonTasksManager();
    }

    public function create(TaskInterface $task)
    {
        /** Содержимое метода */
    }

    public function update(TaskInterface $task)
    {
        /** Содержимое метода */
    }

    public function delete(TaskInterface $task)
    {
        /** Содержимое метода */
    }
}
$task = new Task();
$taskManager = new TasksManager();

D
Dmitry Baibukhtin, 2014-10-23
@PiloTeZ

Or is it superfluous to inherit from the abstract class until there is a need to create other managers and other methods of the Task class?
Do I need to make interfaces for all classes if there is a possibility of extending it in the future?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question