Brief introduction to "interface-oriented programming" and "coupling method" using interfaces, abstract classes, and common base classes in PHP _ PHP Tutorial

Source: Internet
Author: User
In PHP, "interface-oriented programming" and "coupling method" are implemented using interfaces, abstract classes, and common base classes. Copy the code as follows :? Php * is released to facilitate reading and publishing. you are welcomed to give guidance to others. [note] This example has passed the test. The code is as follows:


/*
What you learn and do is published to facilitate your own reading and publishing. You are welcome to give your expert guidance ......
Tip: The test is successful in this example.
Scenario Design]
Simulate the computer motherboard IDE interface, such as: the memory that can be accessed by the motherboard often has optical drive, hard drive, flash memory, etc,
For convenience, it is necessary to set the same interface for these different storages.
In this example, we assume that an external storage device with an unprecedented access method needs to be added to the motherboard for access,
Therefore, a coupled design mode is required.
[This example describes]
1. the so-called "interface" mode can be achieved through interfaces, abstract classes, and general class inheritance, which indicates:
A. subclass objects can be used as parent classes, because subclasses are special parent classes!
B. Note that interfaces, abstract classes, and basic classes inherited by general classes are written in these three methods!
2. the interface mode is really a contract!
3. "coupling design mode" in programming "!
*/
//----------------------------------------------------------------------
/* [Method 1] interface implementation method :*/
Interface readandwrite {
Function read ();
Function write ();
}
Class motherboard {
Private $ storage;
Function _ construct (readandwrite $ obj ){
$ This-> storage = $ obj;
}
Function read (){
$ This-> storage-> read ();
}
Function write (){
$ This-> storage-> write ();
}
}
Class flash implements readandwrite {
Function _ construct (){
Echo "I am Flash memory:
";
}
Function read (){
Echo "starts reading data ......
";
}
Function write (){
Echo "start to store data ......";
}
}
Class yingpan implements readandwrite {
Function _ construct (){
Echo "I am a hard disk:
";
}
Function read (){
Echo "starts reading data ......
";
}
Function write (){
Echo "start to store data ......";
}
}
Class disco implements readandwrite {
Function _ construct (){
Echo "I am a CD:
";
}
Function read (){
Echo "starts reading data ......
";
}
Function write (){
Echo "start to store data ......";
}
}
//----------------------------------------------------------------------
/* [Method 2] abstract class implementation method:
Abstract class readandwrite {
Abstract function read ();
Abstract function write ();
}
Class motherboard {
Private $ storage;
Function _ construct (readandwrite $ obj ){
$ This-> storage = $ obj;
}
Function read (){
$ This-> storage-> read ();
}
Function write (){
$ This-> storage-> write ();
}
}
Class flash extends readandwrite {
Function _ construct (){
Echo "I am Flash memory:
";
}
Function read (){
Echo "starts reading data ......
";
}
Function write (){
Echo "start to store data ......";
}
}
Class yingpan extends readandwrite {
Function _ construct (){
Echo "I am a hard disk:
";
}
Function read (){
Echo "starts reading data ......
";
}
Function write (){
Echo "start to store data ......";
}
}
Class disco extends readandwrite {
Function _ construct (){
Echo "I am a CD:
";
}
Function read (){
Echo "starts reading data ......
";
}
Function write (){
Echo "start to store data ......";
}
}
*/
//----------------------------------------------------------------------
// [Method 3] General class inheritance implementation method:
/*
Class readandwrite {
Function read (){
Echo "reading ..............";
}
Function write (){
Echo "writing ..............";
}
}
Class motherboard {
Private $ storage;
Function _ construct (readandwrite $ obj ){
$ This-> storage = $ obj;
}
Function read (){
$ This-> storage-> read ();
}
Function write (){
$ This-> storage-> write ();
}
}
Class flash extends readandwrite {
Function _ construct (){
Echo "I am Flash memory:
";
}
Function read (){
Echo "starts reading data ......
";
}
Function write (){
Echo "start to store data ......";
}
}
Class yingpan extends readandwrite {
Function _ construct (){
Echo "I am a hard disk:
";
}
Function read (){
Echo "starts reading data ......
";
}
Function write (){
Echo "start to store data ......";
}
}
Class disco extends readandwrite {
Function _ construct (){
Echo "I am a CD:
";
}
Function read (){
Echo "starts reading data ......
";
}
Function write (){
Echo "start to store data ......";
}
}
*/
//----------------------------------------------------------------------
/*
[Coupling mode]
The coupling mode refers to the two classes of different standards (the interfaces, abstract classes, and common base classes in this example have different access methods from the external storage devices ),
The purpose of achieving the same standard through the intermediate converter is like the transformer connection-this example is to convert the Rdata and Wdata methods of the unknow class
Replace it with the read and write methods to achieve the same access method as the interface, abstract class, and basic class in this example. the intermediate converter in this example is
Apdater class.
Since php can only inherit one class but can inherit multiple interfaces, three coupling methods are generated:
Method 1: The Apdater class of the intermediate converter inherits the abstract class or common base class. However, since php can only inherit one class
Class defines an external storage class unknow object, and uses the method of reloading the inherited abstract class or ordinary base class access to convert
Change the access method to achieve the same access method.
Method 2: The Apdater class of the intermediate converter inherits the unknow class and interface of the alien storage class. in this case, the access method of the Apdater class can be directly used.
(Parent: Rdata () and parent: Wdata () -- the method in php to call the parent class method), and the method specified by the implementation interface,
To convert the access method to achieve the same access method.
Method 3: Similar to Method 1, it is changed to inherit (implement) interface;
*/
//----------------------------------------------------------------------
/*
Method 1]
*/
/*
Class unknow {
Function _ construct (){
Echo "I am an alien storage device unknown to the earth. I have access methods different from those of the earth storage device:
";
}
Function Rdata (){
Echo "I'm reading now ......
";
}
Function Wdata (){
Echo "I'm writing now ......";
}
}
Class Adpater extends readandwrite {
Private $ obj;
Function _ construct (unknow $ x ){
$ This-> obj = $ x;
}
Function read (){
$ This-> obj-> Rdata ();
}
Function write (){
$ This-> obj-> Wdata ();
}
}
*/
//----------------------------------------------------------------------
/*
Method 2]
Class unknow {
Function _ construct (){
Echo "I am an alien storage device unknown to the earth. I have access methods different from those of the earth storage device:
";
}
Function Rdata (){
Echo "I'm reading now ......
";
}
Function Wdata (){
Echo "I'm writing now ......";
}
}
Class Adpater extends unknow implements readandwrite {
Function read (){
Parent: Rdata ();
}
Function write (){
Parent: Wdata ();
}
}
*/
//------------------------------------------------------------------------
/*
Method 3]
*/
Class unknow {
Function _ construct (){
Echo "I am an alien storage device unknown to the earth. I have access methods different from those of the earth storage device:
";
}
Function Rdata (){
Echo "I'm reading now ......
";
}
Function Wdata (){
Echo "I'm writing now ......";
}
}
Class Adpater implements readandwrite {
Private $ obj;
Function _ construct (unknow $ x ){
$ This-> obj = $ x;
}
Function read (){
$ This-> obj-> Rdata ();
}
Function write (){
$ This-> obj-> Wdata ();
}
}
// [Program subject call]
Echo"Object-oriented programming-interface";
$ Storage1 = new flash ();
$ Computer = new motherboard ($ storage1 );
$ Computer-> read ();
$ Computer-> write ();
$ Storage2 = new yingpan ();
$ Computer = new motherboard ($ storage2 );
$ Computer-> read ();
$ Computer-> write ();
$ Storage3 = new disco ();
$ Computer = new motherboard ($ storage3 );
$ Computer-> read ();
$ Computer-> write ();
$ Un_storage = new unknow ();
$ Apdaterx = new Adpater ($ un_storage );
$ Computer = new motherboard ($ apdaterx );
$ Computer-> read ();
$ Computer-> write ();
?>

The http://www.bkjia.com/PHPjc/323037.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323037.htmlTechArticle code is as follows :? Php/* learn and do it, and publish it for your convenience. it is even more popular. You are welcome to give advice ...... [note] The test result in this example is correct...

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.