requirements: Just log in to the file. (Log System First Edition
)
/** * Log System First Edition * @author ranping */class Logmodel {private $logId;
Private $operateUser;
Private $operateTime;
Private $logContent;
Public Function Getlogid () {return $this->logid;
The Public Function Getoperateuser () {return $this->operateuser;
The Public Function Getoperatetime () {return $this->operatetime;
The Public Function getlogcontent () {return $this->logcontent;
The Public Function Setlogid ($logId) {$this->logid = $logId;
The Public Function Setoperateuser ($operateUser) {$this->operateuser = $operateUser;
The Public Function Setoperatetime ($operateTime) {$this->operatetime = $operateTime;
The Public Function setlogcontent ($logContent) {$this->logcontent = $logContent;
The Public Function toString () {return ";
} interface Logfileoperateapi {Abstract public Function readlogfile ();
Abstract public Function writelogfile (); Class Logfileoperate implements LOGFILEOPERATEAPI {public Function __construct () {} public Function Readlogfile () {} public Function Writelogfile () {}} class Client {publ
IC static function main () {$logModel = new Logmodel ();
$list = Array ();
$list [] = $logModel;
$api = new Logfileoperate ();
$api->writelogfile ($list);
$readLog = $api->readlogfile (); }
}
Increase Demand:
increase the way the database logs. And you need to make the previous version work.
There is no problem if the log file is logged only in the form of a file. It's easy to see, though, but after a while, start thinking about upgrading the system and deciding to use the database to manage the logs.
Interface Logdboperateapi {
abstract public Function createlog ();
Abstract public Function Updatelog ();
Abstract public Function Removelog ();
Abstract public Function GetLog ();
}
Class Logdboperate implements LOGDBOPERATEAPI {public
function Createlog () {} public
function GetLog () {}
Public Function Removelog () {} public
function Updatelog () {}
}
This time, the problem encountered:1. The second edition of the new system log, the use of the old system is the first edition of the log. The new system does not have the same interface as the old system, so the new system cannot be used in the same way to directly use the first version of the log behavior. 2. The first solution to the idea was to rewrite the initial implementation of the write to file. However, other features may depend on the previous implementation, and direct changes can cause a failure. What if you don't see the source code at all?
Solution:
Adapter Mode
code for Adapter mode:
Interface Target {public
function request ();
}
Class Adaptee {public
function specificrequest () {
//todo
}
}
class Adapter implements Target { C12/>private $adaptee;
Public function __construct ($adaptee) {
$this->adaptee = $adaptee;
}
Public Function request () {
$this->adaptee->specificrequest ();
}
}
Class Client {public
static function main () {
$adaptee = new Adaptee ();
$taget = new Adapter ($adaptee);
$taget->request ();
}
implementing the log system with adapter mode:(second edition)
Class Adapter implements LOGDBOPERATEAPI {
private $adaptee = null;
Public function __construct ($adaptee) {
$this->adaptee = $adaptee;
}
Public Function Createlog () {
///$this->adaptee to implement functionality
} public Function
GetLog () {
//with $this-> Adaptee to implement functionality
} public Function
Removelog () {
//$this->adaptee to implement function
}
Public function Updatelog () {
///using $this->adaptee to implement the functionality
}
}
class Client {public
static function main () {
$logModel = new Logmodel ();
$list = Array ();
$list [] = $logModel;
$logFileApi = new Logfileoperate ();
$logDbApi = new Adapter ($LOGFILEAPI);
Todo $logDbApi implements the operation with the Logdboperateapi interface.
}
}
Adapter Mode Explanation:
function: Make a transformation match, the purpose is to reuse existing functions, not to implement the new interface with the industry.
convert incompatible interfaces to the customer's desired style.