The coupling design pattern of PHP interface programming

Source: Internet
Author: User
Tags abstract mysql tutorial php tutorial

PHP Tutorial interface-oriented programming coupled design pattern

Understanding of interfaces.   Interface from a deeper level of understanding, should be defined (norms, constraints) and implementation (the principle of separation of the principles).   When we implement a system in general, the definition and implementation are usually integrated, without separation, I think the most understood system design specification should be the separation of all definitions and implementations, although this may be a bit annoying in some cases of the system.   The interface itself reflects the system designer's abstract understanding of the system.   Interfaces should have two classes: the first is an abstraction of a body, which corresponds to an abstraction (abstract class); The second class is an abstraction of one aspect of a body, which forms an abstract plane (interface), and a body may have multiple abstract faces.   There is a difference between an abstract body and an abstract surface. 2. Another important factor in designing interfaces is the environment in which interfaces are (context,environment), and the view of the system theory: the environment is the sum of the space and the external influence factors of the system elements. Any interface is generated in a certain environment.   Therefore, the definition of the environment and the impact of the environment on the interface can not be ignored, from the original environment, all the interface will lose its original meaning.   3. According to the module development model (3c), the three of them complement each other, each division, seamless, indispensable. Object-oriented means that when we consider a problem, the object is the unit, considering its properties and methods oriented process, we consider the problem in a specific process (transaction process) as a unit, consider its implementation of interface design and non-interface design is for the reuse of technology, and object-oriented (process) is not a problem

What I want is to change the code as little as possible to meet his needs. I can abstract the following interface:

The following are the referenced contents:
Package org.bromon.test;
public interface db
{
Java.sql.connection opendb (string url,string user,string password);
void Close ();
}


This interface defines only two methods, without any meaningful code, and the specific code is given by the class that implemented the interface, such as the MySQL tutorial. Java:

The following are the referenced contents:

Package org.bromon.test;
Import java.sql.*;
public class MySQL implements DB
{
private string Url= "Jdbc:mysql:localhost:3306/test";
private string user= "root";
private string Password= "";
Private Connection conn;
Public connection opendb (Url,user,password)
{
Code to connect to the database tutorial
}

public void Close ()
{
Close Database
}
}

Look at the example below

<?php
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 implements js{
     public $varl =null;//here directly: public $varl = new Nothingx (); There will be an error.
     function __construct () {
     $this->varl = new Nothingx ();
 }
     function ys ($a, $b) {
        return $ This->varl->say ();
    }
}
/* can also be implemented in an inherited way Yo:
Class extends Nothingx implements js{
     function ys ($a, $b {
     return Parent::say ();
    }
}
*/
Class nothingx{
 function Say () {
    return "I don't do anything ... Just to realize <font color= #990000 ><b> ' coupling design mode ' </b></font> .... I am out of soy sauce ... ";
 }
}


Class test{
Private $one;
Private $two;
Public function __construct ($x, $y) {
$this->one= $x;
$this->two= $y;
echo "<font size=20px><strong> Object-oriented programming-interface </font></strong>}
function display (JS $a) {
Return "<font color= #990000 ><b> operations implemented using the PHP interface technology--started:</b></font>}
}
$t =new Test (103,2);
$t 1=new JF;
$t 2=new;
echo $t->display ($t 1);
echo $t->display ($t 2);
?>

Benefits

The

is not experienced in a small project. Using interface programming, it is easy to expand programs and reduce the coupling of programs.
  For a common example, say, you have a method: public Viod Doxx (ArrayList ArrayList), the parameter must be a ArrayList object when the method is invoked. One day, when a parameter needs to be replaced with another collection class, the code must be changed; If you use an interface, you don't have to; change the method to public viod Doxx (List list), the method calls, the parameters are flexible, you can pass in ArrayList, LinkedList and so on, so that the drop point coupling.

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.