参考这条评论,
当一个类有很长的参数列表时,它可能是一种“代码味道”,表明你的类试图做太多事情并且可能没有遵循单一责任原则。如果您的类试图做太多事情,请考虑将您的代码重构为许多相互消耗的较小类。
我应该如何处理下面的这个控制器类 - 它是“试图做太多”吗?
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;
}
}
}
我怎样才能把它分解成更小的类——如果它是一个控制器,它包含我需要输出页面的这些基本类?
我应该怎么做才能遵循依赖注入的原则?