关注点分离 (SoC)
关键实施示例
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 < 0) { throw new exception('invalid total amount'); } // process order... }}// good - separated validationclass ordervalidator { public function validate(order $order): array { $errors = []; if (empty($order->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 "<h1>{$product->name}</h1>"; echo "<p>price: ${$product->price}</p>"; }}// good - separated presentationclass productcontroller { public function show($id) { $product = $this->productrepository->find($id); return $this->view->render('product/show', ['product' => $product]); }}// product/show.php template<h1><?= htmlspecialchars($product->name) ?></h1><p>price: $<?= htmlspecialchars($product->price) ?></p>
这个很好的例子将显示逻辑分离到模板中,使代码更易于维护,并允许设计人员独立工作。
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 }}
配置与实现分离,使代码更加灵活和可维护。无需修改代码即可更改设置。