Copy codeThe Code is as follows: <? Php
/*
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 class objects, 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: <br> ";
}
Function read (){
Echo "start to read data... <br> ";
}
Function write (){
Echo "start to store data... }
}
Class yingpan implements readandwrite {
Function _ construct (){
Echo "I am a hard disk: <br> ";
}
Function read (){
Echo "start to read data... <br> ";
}
Function write (){
Echo "start to store data... }
}
Class disco implements readandwrite {
Function _ construct (){
Echo "I Am a CD: <br> ";
}
Function read (){
Echo "start to read data... <br> ";
}
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: <br> ";
}
Function read (){
Echo "start to read data... <br> ";
}
Function write (){
Echo "start to store data... }
}
Class yingpan extends readandwrite {
Function _ construct (){
Echo "I am a hard disk: <br> ";
}
Function read (){
Echo "start to read data... <br> ";
}
Function write (){
Echo "start to store data... }
}
Class disco extends readandwrite {
Function _ construct (){
Echo "I Am a CD: <br> ";
}
Function read (){
Echo "start to read data... <br> ";
}
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: <br> ";
}
Function read (){
Echo "start to read data... <br> ";
}
Function write (){
Echo "start to store data... }
}
Class yingpan extends readandwrite {
Function _ construct (){
Echo "I am a hard disk: <br> ";
}
Function read (){
Echo "start to read data... <br> ";
}
Function write (){
Echo "start to store data... }
}
Class disco extends readandwrite {
Function _ construct (){
Echo "I Am a CD: <br> ";
}
Function read (){
Echo "start to read data... <br> ";
}
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 "<font color = # ff0000> I am an alien storage device unknown to the earth. I have different access methods from the earth storage device: </font> <br> ";
}
Function Rdata (){
Echo "I'm reading now... <br> ";
}
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 "<font color = # ff0000> I am an alien storage device unknown to the earth. I have different access methods from the earth storage device: </font> <br> ";
}
Function Rdata (){
Echo "I'm reading now... <br> ";
}
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 "<font color = # ff0000> I am an alien storage device unknown to the earth. I have different access methods from the earth storage device: </font> <br> ";
}
Function Rdata (){
Echo "I'm reading now... <br> ";
}
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 "<strong> <font color = #990000 size = 20px> Object-Oriented Programming-interface </font> </strong> $ 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 ();
?>