<?php
/*
Learn and do it while you are here to make it easier for you to read it yourself, and to be published for the sake of guidance from experts. Experts are welcome to give advice...
【Tip】This example passes the test without error
【Scenario Design】
Simulate the IDE interface of the computer motherboard, for example: the storage that the motherboard can access is often optical drive, hard disk, flash memory, etc.
For convenience, it is necessary to set the same interface for these different storages.
This example also assumes that an unprecedented alien storage with unique access methods must also be added to the motherboard for access.
Therefore, a coupled design model is needed.
【The main story of this example】
1. To achieve the so-called "interface" mode through three methods: interface, abstract class, and general class inheritance, we can explain:
A. Subclass objects can be used as parent objects, because subclasses are special parent classes�
B. Please pay attention to the base class writing method of interface, abstract class and general class inheritance in these three implementation methods!
2. The interface mode is really a contract!
3. The "coupled design model" of 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:<br>";
}
function read(){
echo "Start reading data...<br>";
}
function write(){
echo "Start storing data...<hr>";
}
}
class yingpan implements readandwrite{
function __construct(){
echo "I'm a hard drive:<br>";
}
function read(){
echo "Start reading data...<br>";
}
function write(){
echo "Start storing data...<hr>";
}
}
class disco implements readandwrite{
function __construct(){
echo "I am CD:<br>";
}
function read(){
echo "Start reading data...<br>";
}
function write(){
echo "Start storing data...<hr>";
}
}
//----------------------------------------------------------------------
/*【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:<br>";
}
function read(){
echo "Start reading data...<br>";
}
function write(){
echo "Start storing data...<hr>";
}
}
class yingpan extends readandwrite{
function __construct(){
echo "I'm a hard drive:<br>";
}
function read(){
echo "Start reading data...<br>";
}
function write(){
echo "Start storing data...<hr>";
}
}
class disco extends readandwrite{
function __construct(){
echo "I am CD:<br>";
}
function read(){
echo "Start reading data...<br>";
}
function write(){
echo "Start storing data...<hr>";
}
}
*/
//----------------------------------------------------------------------
//【Method Three】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:<br>";
}
function read(){
echo "Start reading data...<br>";
}
function write(){
echo "Start storing data...<hr>";
}
}
class yingpan extends readandwrite{
function __construct(){
echo "I'm a hard drive:<br>";
}
function read(){
echo "Start reading data...<br>";
}
function write(){
echo "Start storing data...<hr>";
}
}
class disco extends readandwrite{
function __construct(){
echo "I am CD:<br>";
}
function read(){
echo "Start reading data...<br>";
}
function write(){
echo "Start storing data...<hr>";
}
}
*/
//----------------------------------------------------------------------
/*
【Coupled Mode】
The coupling mode is to use two classes of different standards (the interface, abstract class, ordinary base class in this example has different access methods and alien storage).
The purpose of achieving the same standard through an intermediate converter is like a transducer - this example is to convert the Rdata and Wdata methods of the unknown class
Change it to read and write methods to achieve the same access method as the interface, abstract class, and ordinary base class in this example. The intermediate converter in this example is
Adapter class.
Since php can only inherit one class but can inherit multiple interfaces, three coupling methods are generated:
Method 1: The intermediate converter Apdater class inherits abstract class or ordinary base class, but since only one class can be inherited in PHP, it is in Apdater
Define an object of an alien memory class unknown in the class and transfer it by overloading the access method of the inherited abstract class or ordinary base class.
Change the access method to achieve the purpose of the same access method.
Method 2: The intermediate converter Apdater class inherits the alien memory class unknown and interface. At this time, you can directly use the access method of Apdater class.
(parent::Rdata() and parent::Wdata() - the way in which subclasses in php call parent class methods), and implement interface specified methods,
to convert the access method to achieve the purpose of the same access method.
Method 3: Similar to method, it is just changed to inherit (implementation) interface;
*/
//----------------------------------------------------------------------
/*
【Method 1】
*/
/*
class unknow{
function __construct(){
echo "<font color=#ff0000>I am an alien storage unknown to the earthlings. I have a different way of accessing the earth storage:</font><br>";
}
function Rdata(){
echo "I'm reading now......<br>";
}
function Wdata(){
echo "I'm writing now......<hr>";
}
}
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 unknown to the earthlings. I have a different way of accessing the earth storage:</font><br>";
}
function Rdata(){
echo "I'm reading now......<br>";
}
function Wdata(){
echo "I'm writing now......<hr>";
}
}
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 unknown to the earthlings. I have a different way of accessing the earth storage:</font><br>";
}
function Rdata(){
echo "I'm reading now......<br>";
}
function Wdata(){
echo "I'm writing now......<hr>";
}
}
class Adpater implements readandwrite{
private $obj;
function __construct(unknow $x){
$this->obj=$x;
}
function read(){
$this->obj->Rdata();
}
function write(){
$this->obj->Wdata();
}
}
//【Program body call】
echo "<strong><font color=#990000 size=20px>Object-oriented programming—interface</font></strong><hr>";
$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();
?>