可能违反单一职责原则的控制者? [英] A controller that might violate Single Responsibility Principle?

本文介绍了可能违反单一职责原则的控制者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考此评论,

<块引用>

当一个类有很长的参数列表时,它可以是一个代码闻到你的班级试图做的太多,可能没有遵循单一责任原则.如果你的班级正在尝试做太多,考虑将您的代码重构为多个相互消耗的小类.

对于下面的这个控制器类我应该怎么做 - 它是试图做太多事情"吗?

类控制器{公共 $template;公开 $translation;公共 $auth;公开$文章;公共 $nav;公共函数 __construct(数据库 $connection, $template){$this->template = $template;$this->translation = new Translator($connection);$this->nav = new Nav($connection);$this->article = 新文章($connection);$this->auth = new Auth($connection);}公共函数 getHtml(){if(isset($_REQUEST['url'])){$item = $this->article->getRow(['url' =>'home','is_admin' => $this->auth->is_admin]);包括 $this->template->path;}}}

我怎样才能把它分解成更小的类——如果它是一个包含我需要输出页面的基本类的控制器?

我该怎么做才符合依赖注入?

解决方案

注意:这将是简短版本,因为我在工作.晚上我会详细说明

所以……您的代码存在以下违规行为:

  • SRP(以及扩展 - SoC):您的控制器负责验证输入、授权、数据收集、用数据填充模板并呈现所述模板.此外,您的 Article 似乎负责数据库抽象域逻辑.

  • LoD:您传递$connection ,因为您需要将其传递给其他结构.

  • 封装:您的所有类属性都具有公开可见性,并且可以随时更改.

  • 依赖注入:虽然你的控制器"有几个直接依赖,但你只是传入模板(实际上不应该由控制器在适当的 MVC 中管理).>

  • 全局状态:你的代码依赖于 $_REQUEST 超全局.

  • 松散耦合:您的代码直接与类的名称和这些类的构造函数的占用空间相关联,您在构造函数中对其进行初始化.

Referring to this comment,

When a class has a very long list of arguments, it can be a "code smell" that your class is trying to do too much and possibly not following the single responsibility principle. If your class is trying to do too much, consider refactoring your code into a number of smaller classes that consume each other.

What should I do about this controller class below - is it "trying to do too much"?

class Controller
{
    public $template;
    public $translation;
    public $auth;
    public $article;
    public $nav;

    public function __construct(Database $connection, $template) 
    {
        $this->template = $template;
        $this->translation = new Translator($connection);
        $this->nav = new Nav($connection);
        $this->article = new Article($connection);
        $this->auth = new Auth($connection);
    }

    public function getHtml() 
    {
        if(isset($_REQUEST['url'])) 
        {
            $item = $this->article->getRow(['url' => 'home','is_admin' => $this->auth->is_admin]);
            include $this->template->path;
        }
    }
}

How can I break it up into smaller classes - if it is a controller that holds these basic classes that I need to output a page?

And what should I do so that it is follows the principle of dependency injection?

解决方案

Note: this will be the short version, because I'm at work. I will elaborate in the evening

So ... your code has following violations:

  • SRP (and by extension - SoC): your controller is responsible for validation of input, authorization, data gathering, populating template with data and rendering said template. Also, your Article seems to be responsible for both DB abstraction and domain logic.

  • LoD: you are passing the $connection only because you need to pass it on to other structures.

  • encapsulation: all your class attributes have public visibility and can be altered at any point.

  • dependency injection: while your "controller" has several direct dependencies, you are only passing in the template (which actually shouldn't be managed by controllers in proper MVC).

  • global state: your code depends on $_REQUEST superglobal.

  • loose coupling: your code is directly tied to the names of classes and the footprint of constructors for those classes, that you initializing in the constructor.

这篇关于可能违反单一职责原则的控制者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆