PHP interface-Oriented Programming Coupling Design Mode-PHP source code

Source: Internet
Author: User
Ec (2); php tutorial for Interface Programming Coupling Design Patterns for interface understanding. The deeper understanding of interfaces should be the separation of definitions (Norms, constraints) and implementations (the principle of separation of real names. Generally, when implementing a system, we usually integrate the definition and implementation without separation. In my opinion, the most understandable system design specification should be the separation of all definitions and implementations, although this may be a little complicated for some situations in the system. The interface itself reflects the abstract understanding of the system by the system designer. There are two types of interfaces: script ec (2) and script

Php tutorial interface-Oriented Programming Coupling Design Mode

Understanding of interfaces. The deeper understanding of interfaces should be the separation of definitions (Norms, constraints) and implementations (the principle of separation of real names. Generally, when implementing a system, we usually integrate the definition and implementation without separation. In my opinion, the most understandable system design specification should be the separation of all definitions and implementations, although this may be a little complicated for some situations in the system. The interface itself reflects the abstract understanding of the system by the system designer. There are two types of interfaces: the first class is the abstraction of an individual, which can correspond to an abstract class. The second class is the abstraction of a certain aspect of an individual, it forms an abstract surface (interface). An individual may have multiple abstract surfaces. There is a difference between an abstract body and an abstract surface. 2. Another factor that cannot be ignored when designing an interface is the context (environment) of the interface. the viewpoint of system theory is that the environment is the sum of the space and external influencing factors of the system elements. Any interface is generated in a certain environment. Therefore, the impact of environment definition and environment changes on interfaces cannot be ignored. From the original environment, all interfaces will lose their original meaning. 3. According to the component development model (3c), the three components complement each other, and each other is integrated. Object-oriented means that when we consider the problem, we take the object as the unit, and consider its attributes and Methods. process-oriented means that when we consider the problem, we take a specific process (transaction process) considering that its implementation interface design and non-interface design are for reuse technology, it is not a problem with object-oriented (process ).

I want to modify the code as little as possible to meet his needs. I can abstract the following interfaces:

Reference content is as follows:
Package org. bromon. test;
Public interface db
{
Java. SQL. connection opendb (string url, string user, string password );
Void close ();
}


This interface only defines two methods without any actual code. The specific code is provided by the class implementing this interface, such as mysql tutorial. java:

Reference content is as follows:

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 of the database connection tutorial
}

Public void close ()
{
// Close the database
}
}

See the instance below

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... the result is:". ($ a-$ B );
}
}
Class aaf implements js {

Function ys ($ a, $ B ){
Return "Multiplication... the result is:". ($ a * $ B );
}
}
Class jjf implements js {

Function ys ($ a, $ B ){
Return "Division calculation... 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; // public $ varl = new nothingx (); an error occurs.
Function _ construct (){
$ This-> varl = new nothingx ();
}
Function ys ($ a, $ B ){
Return $ this-> varl-> say ();
}
}
/* It can also be implemented using inheritance:
Class china extends nothingx implements js {
Function ys ($ a, $ B ){
Return parent: say ();
}
}
*/
Class nothingx {
Function say (){
Return "I do not perform any operations...'Coupling design Mode'... I made soy sauce ......";
}
}


Class test {
Private $ one;
Private $ two;
Public function _ construct ($ x, $ y ){
$ This-> one = $ x;
$ This-> two = $ y;
Echo"Object-Oriented Programming-InterfaceClass test initialization:
Attribute $ one = ". $ this-> one." attribute $ two = ". $ this-> two ."";
}
Function display (js $ ){
Return"Operations implemented using the php interface technology -- start operations:". $ A-> ys ($ this-> one, $ this-> two )."";
}
}
$ T = new test (103,2 );
$ T1 = new jf;
$ T2 = new china;
Echo $ t-> display ($ t1 );
Echo $ t-> display ($ t2 );
?>

Benefits

In small projects, you cannot experience it. Programming Using Interfaces makes it easy to expand programs and reduces program coupling.
For example, you can use the public viod doxx (arraylist) method. When calling a method, the parameter must be an arraylist object. One day, the Code must be changed when the parameter needs to be changed to another collection class. If an interface is used, the code is no longer required. Change the method to public viod doxx (list ), when the method is called, the parameters are flexible and can be passed into arraylist, sorted list, and so on, so as to reduce 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.