JavaScript design pattern PHP interface-oriented programming coupling design pattern Simple example

Source: Internet
Author: User
Copy CodeThe code is as follows:


Interface js{
Function ys ($a, $b);
}
Class Af implements js{
Function ys ($a, $b) {
Return "addition operation ... The result is: ". ($a + $b);
}
}
Class Jf implements js{
Function ys ($a, $b) {
Return "subtraction operation ... The result is: ". ($a-$b);
}
}
Class AAf implements js{
Function ys ($a, $b) {
Return "multiplication operation ... The result is: ". ($a * $b);
}
}
Class JJf implements js{
Function ys ($a, $b) {
Return "division operation ... The result is: ". ($a/$b);
}
}
Class MODF implements js{
Function ys ($a, $b) {
Return "modulo operation ... The result is: ". ($a% $b);
}
}
Class China implements js{
Public $varl =null;//here directly: public $varl = new Nothingx (); Will go wrong.
function __construct () {
$this->varl = new Nothingx ();
}
Function ys ($a, $b) {
return $this->varl->say ();
}
}
/* can also be implemented in an inherited way:
Class China extends NOTHINGX implements js{
Function ys ($a, $b) {
return Parent::say ();
}
}
*/
Class nothingx{
function say () {
Return "I don't do any arithmetic ... Just to achieve' coupling design mode '... I came out to soy sauce ... ";
}
}
Class test{
Private $one;
Private $two;
Public function __construct ($x, $y) {
$this
$this->two= $y;
echo "Object-oriented programming--InterfaceClass Test initialization:
Property \$ property \ $two = ". $this->two.";
}
function display (JS $a) {
Return "the operation implemented with PHP interface technology--begins the operation:". $a->ys ($this->one, $this->two)." ";
}
}
$t =new Test (103,2);
$t 1=new JF;
$t 2=new China;
echo $t->display ($t 1);
echo $t->display ($t 2);
?>

The above describes the JavaScript design pattern PHP interface-oriented programming coupled design pattern simple example, including the JavaScript design patterns of content, I hope to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.