
关键实施示例
1. 数据库层分离
// bad - mixed concernsclass user { public function save() { $db = new pdo('mysql:host=localhost;dbname=app', 'user', 'pass'); $stmt = $db->prepare("insert into users (name, email) values (?, ?)"); $stmt->execute([$this->name, $this->email]); }}// good - separated database logicclass user { private string $name; private string $email;}class userrepository { private pdo $db; public function save(user $user) { $stmt = $this->db->prepare("insert into users (name, email) values (?, ?)"); $stmt->execute([$user->getname(), $user->getemail()]); }}
这个很好的例子将数据结构(user)与存储逻辑(userrepository)分开。这使得代码更易于维护,并且允许在不修改 user 类的情况下更改存储方法。
2. 验证分离
// bad - mixed validation and business logicclass order { public function process() { if (empty($this->items)) { throw new exception('order cannot be empty'); } if ($this->total getitems())) { $errors[] = 'order cannot be empty'; } if ($order->gettotal() < 0) { $errors[] = 'invalid total amount'; } return $errors; }}class order { public function process() { // only handles order processing }}
验证逻辑移至专用验证器类,使 order 类能够专注于业务逻辑。
3.视图/模板分离
// bad - mixed html and logicclass productpage { public function show($id) { $product = $this->getproduct($id); echo "{$product->name}
"; echo "price: ${$product->price}
"; }}// good - separated presentationclass productcontroller { public function show($id) { $product = $this->productrepository->find($id); return $this->view->render('product/show', ['product' => $product]); }}// product/show.php templatename) ?>
price: $price) ?>
爱克网络企业网站建设系统 No.090730 系统特点:功能简洁实用。目前互联网上最简洁的企业网站建设系统!原创程序代码。非网络一般下载后修改的代码。更安全。速度快!界面模版分离。原创的分离思路,完全不同于其他方式,不一样的简单感受!搜索引擎优化。做了基础的seo优化。对搜索引擎更友好系统功能关于我们:介绍企业介绍类信息,可自由添加多个介绍栏目!资讯中心:公司或行业资讯类内容展示。可自由添加多个资讯内容!产品展示:支持类别设置,可添加产品图片
0 查看详情
这个很好的例子将显示逻辑分离到模板中,使代码更易于维护,并允许设计人员独立工作。
4. 服务层分离
// bad - mixed business logicclass ordercontroller { public function checkout() { $order = new order($_post['items']); $payment = new payment($_post['card']); $payment->process(); $order->updatestatus('paid'); $email = new emailservice(); $email->sendconfirmation($order); }}// good - separated servicesclass orderservice { private paymentservice $paymentservice; private emailservice $emailservice; public function processorder(order $order, paymentdata $paymentdata): void { $this->paymentservice->process($paymentdata); $order->updatestatus('paid'); $this->emailservice->sendconfirmation($order); }}class ordercontroller { public function checkout() { $this->orderservice->processorder($order, $paymentdata); }}
服务层处理复杂的业务逻辑,使控制器专注于请求处理。
5、配置分离
// Bad - Hardcoded configurationclass EmailSender { private $host = 'smtp.example.com'; private $port = 587; public function send($message) { // Sending logic using hardcoded values }}// Good - Separated configuration// config/mail.phpreturn [ 'host' => 'smtp.example.com', 'port' => 587];class EmailSender { private array $config; public function __construct(array $config) { $this->config = $config; } public function send($message) { // Sending logic using config values }}
配置与实现分离,使代码更加灵活和可维护。无需修改代码即可更改设置。
以上就是关注点分离 (SoC)的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1251885.html
微信扫一扫
支付宝扫一扫