PHP uses interface, abstract class, common base class to implement "interface programming" and "coupling method" briefly _php skills

Source: Internet
Author: User
Copy Code code as follows:

<?php
/*
While learning to do, for the convenience of their own browsing and release, more get expert advice and release, welcome expert advice ...
"Prompt" This example passes the test correctly
"Situational Design"
Analog computer Board IDE interface, such as: motherboard can be accessed by the storage device is often the CD-ROM drive, hard disk, flash, etc.
For convenience, it is necessary to set the same interface for these different repositories.
This example also assumes that an unprecedented, access-different alien storage device is added to the motherboard for access,
Coupled design patterns are required.
"This example mainly tells the
1, through the interface, abstract class, general class inheritance three ways to achieve the so-called "interface" mode, to illustrate:
A, a subclass object can be used as a parent class object, because subclasses are special parent classes!
B, note that the three implementations of the interface, abstract class and general class inheritance of the base class writing!
2, the interface mode is really a paper contract!
3, the "Coupling design mode" of programming!
*/
//----------------------------------------------------------------------
/* "Mode One" interface implementation mode: * *
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 a flash:<br>";
}
function Read () {
echo "starts reading data ......<br>";
}
function Write () {
echo "begins to store data ......}
}
Class Yingpan implements readandwrite{
function __construct () {
echo "I am the hard drive:<br>";
}
function Read () {
echo "starts reading data ......<br>";
}
function Write () {
echo "begins to store data ......}
}
Class Disco implements readandwrite{
function __construct () {
echo "I am the disc:<br>";
}
function Read () {
echo "starts reading data ......<br>";
}
function Write () {
echo "begins to store data ......}
}
//----------------------------------------------------------------------
/* "mode two" 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 a flash:<br>";
}
function Read () {
echo "starts reading data ......<br>";
}
function Write () {
echo "begins to store data ......}
}
Class Yingpan extends readandwrite{
function __construct () {
echo "I am the hard drive:<br>";
}
function Read () {
echo "starts reading data ......<br>";
}
function Write () {
echo "begins to store data ......}
}
Class Disco extends readandwrite{
function __construct () {
echo "I am the disc:<br>";
}
function Read () {
echo "starts reading data ......<br>";
}
function Write () {
echo "begins to store data ......}
}
*/
//----------------------------------------------------------------------
"Mode three" general class inheritance implementation way:
/*
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 a flash:<br>";
}
function Read () {
echo "starts reading data ......<br>";
}
function Write () {
echo "begins to store data ......}
}
Class Yingpan extends readandwrite{
function __construct () {
echo "I am the hard drive:<br>";
}
function Read () {
echo "starts reading data ......<br>";
}
function Write () {
echo "begins to store data ......}
}
Class Disco extends readandwrite{
function __construct () {
echo "I am the disc:<br>";
}
function Read () {
echo "starts reading data ......<br>";
}
function Write () {
echo "begins to store data ......}
}
*/
//----------------------------------------------------------------------
/*
"Coupling mode"
The coupling pattern is the two classes of different standards (the interface of this example, the abstract class, the common base class and the alien memory have different access methods),
Through the intermediary converter, to achieve the same standard purposes, like the wiring--this example is the Unknow class of Rdata, Wdata method to
For the read, write method, which achieves the same access method as the interface, abstract class, and common base class of this example, the intermediate converter in this example is
Apdater class.
Because PHP can inherit only one class but can inherit multiple interfaces, three methods of coupling are generated:
Method One: Intermediate converter Apdater class inherits abstract class or normal base class, but because only one class can be inherited in PHP, the Apdater
Class to define an object that is unknow by an extraterrestrial memory class, and to use the method of overloading an inherited abstract class or common base class to turn
The method of exchanging access achieves the purpose of the same access method.
Method Two: Intermediate converter Apdater class inherits the Alien memory class Unknow, the interface, at this time can directly use Apdater class's Access method
(Parent::rdata () and Parent::wdata ()--php the way in which the subclass invokes the parent class method), and implements the interface-defined method,
To transform the access method to achieve the same access method.
Method Three: The same as the method, but only to inherit (implement) interface;
*/
//----------------------------------------------------------------------
/*
"Method One"
*/
/*
Class unknow{
function __construct () {
echo "<font color= #ff0000 > I am an alien reservoir unknown to the earth, and I have a different way of accessing the Earth's storage:</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 Two"
Class unknow{
function __construct () {
echo "<font color= #ff0000 > I am an alien reservoir unknown to the earth, and I have a different way of accessing the Earth's storage:</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 Three"
*/
Class unknow{
function __construct () {
echo "<font color= #ff0000 > I am an alien reservoir unknown to the earth, and I have a different way of accessing the Earth's storage:</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 Principal call"
echo "<strong><font color= #990000 size=20px> object-oriented Programming-Interface </font></strong>$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 ();
?>

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.