php – SOLID – 单一责任原则适用于类中的方法吗?
发布时间:2020-12-13 16:24:44 所属栏目:PHP教程 来源:网络整理
导读:我不确定我班内的这种方法是否违反了单一责任原则, 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(
我不确定我班内的这种方法是否违反了单一责任原则,
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','=','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','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',$identity_id,'id'); } else { $q->where('AND','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; }
该方法将注释保留在数据库中.如果这是它应该做的,那么这是一个单一的责任,实施是好的.你需要把决定是否插入或更新的逻辑,这似乎是一个好的地方.
只有当你需要在没有隐式决策逻辑的情况下明确地进行插入或更新时,才有必要将这两者分成不同的方法,这些方法可以单独调用.但目前,保持它们采用相同的方法简化了代码(因为后半部分是共享的),所以这可能是最好的实现. 免责声明: public function save(Note $note) { if (..) { $this->insert($note); } else { $this->update($note); } } public function insert(Note $note) { .. } public function update(Note $note) { .. } 如果您有时需要因任何原因明确调用insert或update,上述内容将有意义.然而,SRP并不是这种分离的真正原因. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |