Implementation of "interface-oriented programming" and "coupling method" with interface, abstract class and common base class in PHP _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
/*
Learning while doing, for the convenience of their own pages to publish, more get expert guidance and release, welcome expert guidance ...
"Tip" This example passes the test error
"Situational Design"
Analog computer motherboard IDE interface, such as: The motherboard can access the storage device often has optical drive, hard disk, flash memory, etc.
For convenience, it is necessary to set the same interface for these different storage.
This example also assumes that an unprecedented type of alien storage with a unique access method is added to the motherboard for access,
Therefore, a coupled design pattern needs to be adopted.
"In this case, the main story"
1, through the interface, abstract class, general class inheritance three ways to achieve the so-called "interface" mode, to illustrate:
A, subclass object can be used when the parent class object, because the subclass is a special parent class!
B, please note that the three implementations of the interface, abstract class and general class inheritance of the base class Method!
2, the interface mode is really a paper contract!
3, "Coupling design mode" of programming!
*/
//----------------------------------------------------------------------
/* "Mode One" 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:
";
}
function Read () {
echo "begins reading data ...
";
}
function Write () {
echo "Start storing data ...";
}
}
Class Yingpan implements readandwrite{
function __construct () {
echo "I am the hard drive:
";
}
function Read () {
echo "begins reading data ...
";
}
function Write () {
echo "Start storing data ...";
}
}
Class Disco implements readandwrite{
function __construct () {
echo "I am a disc:
";
}
function Read () {
echo "begins reading data ...
";
}
function Write () {
echo "Start storing data ...";
}
}
//----------------------------------------------------------------------
/* "Way two" abstract class implementation mode:
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:
";
}
function Read () {
echo "begins reading data ...
";
}
function Write () {
echo "Start storing data ...";
}
}
Class Yingpan extends readandwrite{
function __construct () {
echo "I am the hard drive:
";
}
function Read () {
echo "begins reading data ...
";
}
function Write () {
echo "Start storing data ...";
}
}
Class Disco extends readandwrite{
function __construct () {
echo "I am a disc:
";
}
function Read () {
echo "begins reading data ...
";
}
function Write () {
echo "Start storing data ...";
}
}
*/
//----------------------------------------------------------------------
"Way three" general class inheritance Implementation mode:
/*
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:
";
}
function Read () {
echo "begins reading data ...
";
}
function Write () {
echo "Start storing data ...";
}
}
Class Yingpan extends readandwrite{
function __construct () {
echo "I am the hard drive:
";
}
function Read () {
echo "begins reading data ...
";
}
function Write () {
echo "Start storing data ...";
}
}
Class Disco extends readandwrite{
function __construct () {
echo "I am a disc:
";
}
function Read () {
echo "begins reading data ...
";
}
function Write () {
echo "Start storing data ...";
}
}
*/
//----------------------------------------------------------------------
/*
"Coupling mode"
The coupling pattern is the different access methods for the two classes of different standards (the interface of this example, the abstract class, the common base class and the alien storage),
The purpose of achieving the same standard through an intermediate converter is like a wire transfer-this example is to turn the Rdata, Wdata method of the Unknow class
Instead of the read and write methods, the same access method as the interface, abstract class, and common base class of this example is reached, and the intermediate converter in this case is
Apdater class.
Because PHP can inherit only one class but inherits multiple interfaces, there are three kinds of coupling methods:
Method One: The intermediate converter Apdater class inherits the abstract class or the ordinary base class, but because PHP can only inherit one class, so in the Apdater
class defines an Unknow object of an alien memory class, and uses the method of overloading inherited abstract classes or ordinary base classes to
Access method, the same access method to achieve the purpose.
Method Two: The intermediate converter Apdater class inherits the outer star memory class Unknow, the interface, at this time can directly use the Apdater class the Access method
(Parent::rdata () and Parent::wdata ()--php The way the child class calls the parent class method), and implements the interface prescribed method,
To convert access methods to achieve the same access method.
Method Three: The same as the method, but instead of inheriting (implementing) the interface;
*/
//----------------------------------------------------------------------
/*
"Method One"
*/
/*
Class unknow{
function __construct () {
echo "I am an unknown alien storage device for the Earth, and I have a different way of accessing the Earth's storage:
";
}
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 Two"
Class unknow{
function __construct () {
echo "I am an unknown alien storage device for the Earth, and I have a different way of accessing the Earth's storage:
";
}
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 Three"
*/
Class unknow{
function __construct () {
echo "I am an unknown alien storage device for the Earth, and I have a different way of accessing the Earth's storage:
";
}
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 Principal call"
echo " Object-oriented programming--Interface";
$storage 1=new Flash ();
$computer =new motherboard ($storage 1);
$computer->read ();
$computer->write ();
$storage 2=new Yingpan ();
$computer =new motherboard ($storage 2);
$computer->read ();
$computer->write ();
$storage 3=new Disco ();
$computer =new Motherboard ($storage 3);
$computer->read ();
$computer->write ();
$un _storage=new unknow ();
$apdaterx =new adpater ($un _storage);
$computer =new motherboard ($apdaterx);
$computer->read ();
$computer->write ();
?>

http://www.bkjia.com/PHPjc/323037.html www.bkjia.com true http://www.bkjia.com/PHPjc/323037.html techarticle Copy the code as follows:? PHP/* Side learn to do, for the convenience of their own page to publish, more get expert guidance and release, welcome expert guidance ... "Prompt" This example pass test error ...

  • Related Article

    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.