lau*_*kok 7 php oop single-responsibility-principle solid-principles
我不确定我班内的这种方法是否违反了单一责任原则,
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;
}
Run Code Online (Sandbox Code Playgroud)
基本上它在方法中执行两个作业 - 插入或更新.
我应该将其分为两种方法,而不是遵守单一责任原则吗?
但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;
}
Run Code Online (Sandbox Code Playgroud)
dec*_*eze 12
该方法将注释保留在数据库中.如果这是它应该做的,那么这是一个单一的责任,实施是好的.你需要把决定是否插入或更新的逻辑的地方,这似乎是一个好地方一样.
只有当你需要在没有隐式决策逻辑的情况下明确地进行插入或更新时,才有必要将这两者分成不同的方法,这些方法可以单独调用.但目前,保持它们采用相同的方法简化了代码(因为后半部分是共享的),所以这可能是最好的实现.
免责声明:
public function save(Note $note) {
if (..) {
$this->insert($note);
} else {
$this->update($note);
}
}
public function insert(Note $note) {
..
}
public function update(Note $note) {
..
}
Run Code Online (Sandbox Code Playgroud)
如果您有时需要打电话insert或update明确出于任何原因,上述内容将有意义.然而,SRP并不是这种分离的真正原因.
| 归档时间: |
|
| 查看次数: |
965 次 |
| 最近记录: |