Home > Backend Development > PHP Tutorial > (6) Object-oriented design principle two

(6) Object-oriented design principle two

WBOY
Release: 2016-07-30 13:31:56
Original
1030 people have browsed it

one. Introduction:
Command mode: The command mode is divided into "requester of command" and "implementer of command". This completes the decoupling of command requests and implementation.

2. Example:

<?php
/*
 * 模拟服务员与厨师
 */
class MealCommand implements Command{
    private $cook;
    public function __construct(cook $cook){
        $this->cook = $cook;
    }
    public function execute(){
        $this->cook->meal();
    }
}

class DrinkCommand implements Command{
    private $cook;
    public function __construct(cook $cook){
        $this->cook = $cook;
    }

    public function execute(){
        $this->cook->drink();
    }
}

/*
 * 模拟类
 */
class cookControl{
    private $mealCommand;
    private $drinkCommand;

    public function addCommand(Command $mealCommand,Command $drinkCommand){
        $this->mealCommand = $mealCommand;
        $this->drinkCommand = $drinkCommand;
    }

    public function callMeal(){
        $this->mealCommand->execute();
    }

    public function callDrink(){
        $this->drinkCommand->execute();
    }
}

$control = new cookControl();
$cook = new cook;
$mealCommand = new MealCommand($cook);
$drinkCommand = new DrinkCommand($cook);
$control->addCommand($mealCommand,$drinkCommand);
$control->callMeal();
$control->callDrink();
Copy after login

Three. Principles of use:
1. Based on the business process, refine the business process. Abstraction step by step until 'appropriate'.
2. Pay attention to the classification of responsibilities.

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced the second principle of (6) object-oriented design, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template