SOLID - 单一责任原则是否适用于类中的方法


SOLID - does Single responsibility principle apply to methods in a class?

我不确定我的类中的这种方法是否违反了单一责任原则,

public function save(Note $note)
{
    if (!_id($note->getid())) {
        $note->setid(idGenerate('note'));
        $q = $this->db->insert($this->table)
                      ->field('id', $note->getid(), 'id');
    } else {
        $q = $this->db->update($this->table)
                      ->where('AND', 'id', '=', $note->getid(), 'id');
    }
    $q->field('title', $note->getTitle())
      ->field('content', $note->getContent());
    $this->db->execute($q);
    return $note;
}

基本上,它在一种方法中执行两项工作 - 插入或更新。

我是否应该将其分为两种方法以符合单一责任原则?

但是SRP仅适用于类,不是吗?它是否适用于类内的方法?

SRP -

一个类应该只有一个职责(即只有一个 软件规格的潜在变化应该能够 影响类的规范)

编辑:

列出注释(包括许多不同类型的列表)、搜索注释等的另一种方法......

public function getBy(array $params = array())
{
    $q = $this->db->select($this->table . ' n')
                  ->field('title')
                  ->field('content')
                  ->field('creator', 'creator', 'id')
                  ->field('created_on')
                  ->field('updated_on');
    if (isset($params['id'])) {
        if (!is_array($params['id'])) {
            $params['id'] = array($params['id']);
        }
        $q->where('AND', 'id', 'IN', $params['id'], 'id');
    }
    if (isset($params['user_id'])) {
        if (!is_array($params['user_id'])) {
            $params['user_id'] = array($params['user_id']);
        }
        # Handling of type of list: created / received
        if (isset($params['type']) && $params['type'] == 'received') {
            $q
                ->join(
                    'inner',
                    $this->table_share_link . ' s',
                    's.target_id = n.id AND s.target_type = ''note'''
                )
                ->join(
                    'inner',
                    $this->table_share_link_permission . ' p',
                    'p.share_id = s.share_id'
                )
                # Is it useful to know the permission assigned?
                ->field('p.permission')
                # We don't want get back own created note
                ->where('AND', 'n.creator', 'NOT IN', $params['user_id'], 'uuid');
            ;
            $identity_id = $params['user_id'];
            # Handling of group sharing
            if (isset($params['user_group_id']) /*&& count($params['user_group_id'])*/) {
                if (!is_array($params['user_group_id'])) {
                    $params['user_group_id'] = array($params['user_group_uuid']);
                }
                $identity_id = array_merge($identity_id, $params['user_group_id']);
            }
             $q->where('AND', 'p.identity_id', 'IN', $identity_id, 'id');
        } else {
            $q->where('AND', 'n.creator', 'IN', $params['user_id'], 'id');
        }
    }
    # If string search by title
    if (isset($params['find']) && $params['find']) {
        $q->where('AND', 'n.title', 'LIKE', '%' . $params['find'] . '%');
    }
    # Handling of sorting
    if (isset($params['order'])) {
        if ($params['order'] == 'title') {
            $orderStr = 'n.title';
        } else {
            $orderStr = 'n.updated_on';
        }
        if ($params['order'] == 'title') {
            $orderStr = 'n.title';
        } else {
            $orderStr = 'n.updated_on';
        }
        $q->orderBy($orderStr);
    } else {
        // Default sorting
        $q->orderBy('n.updated_on DESC');
    }
    if (isset($params['limit'])) {
        $q->limit($params['limit'], isset($params['offset']) ? $params['offset'] : 0);
    }
    $res = $this->db->execute($q);
    $notes = array();
    while ($row = $res->fetchRow()) {
        $notes[$row->uuid] = $this->fromRow($row);
    }
    return $notes;
}

该方法将注释保存到数据库中。如果这是它应该做的,那么这是一个单一的责任,实施很好。您需要将决定是否插入或更新的逻辑放在某个地方,这似乎是一个好地方。

只有当你需要在没有隐式决策逻辑的情况下显式执行插入或更新时,才值得将这两者分成可以单独调用的不同方法。但目前,将它们保持在同一方法中可以简化代码(因为后半部分是共享的),因此这可能是最好的实现。

示例 gratia:

public function save(Note $note) {
    if (..) {
        $this->insert($note);
    } else {
        $this->update($note);
    }
}
public function insert(Note $note) {
    ..
}
public function update(Note $note) {
    ..
}

如果您有时出于任何原因需要明确调用insertupdate,则上述内容是有意义的。不过,SRP并不是这种分离的真正原因。

SOLID 原则适用于类级术语,它们没有明确说明方法。SRP本身声明,类应该有一个改变的理由,所以只要你能替换一个被包装在一个类中的责任,你就没问题。

考虑一下:

$userMapper = new Mapper'MySQL();
// or
$userMapper = new Mapper'Mongo();
// or
$userMapper = new Mapper'ArangoDb();
$userService = new UserService($userMapper);

所有这些映射器都实现一个接口并承担一项职责 - 它们为用户进行抽象存储访问。因此,映射器有一个更改的理由,因为您可以轻松交换它们。

您的案件通常与SRP无关。它更多的是关于最佳实践。好吧,关于方法的最佳实践指出,他们应该尽可能只做一件事,并接受尽可能少的参数。这使得阅读和查找错误变得更加容易。

还有一个原则叫做最小惊讶原则。它只是声明方法名称应显式执行其名称所暗示的操作。

归结为您的代码示例:

save()意味着这一切都与数据保存(更新现有记录)有关,而不是创建。通过在那里进行插入和更新,您可以破坏 PoLA。

就是这样,当您显式调用insert()时,您知道并期望它会添加新记录。update()方法也是如此 - 您知道并期望它会更新一个方法,它不会创建一个新方法。

因此,我不会在save()中同时做这两件事。如果我想更新记录,我会打电话给update()。如果我想创建一条记录,我会打电话给insert().