First, let me give you an example:
A simple article display system
During the simple period, we assume that the article system is read-only, which means that this example will not involve the release of the article, and it is now starting.
Since only the reading of the database is involved, I have defined two interface
Interface dataoperation
{
Public Function Select ($info);
Public Function Selectnum ($info);
}
The above interface defines the interface for reading data, and the Select method returns the desired article. The Selectnum method returns the total number of articles, which are used when paging is displayed. $info is an array that is used to store query conditions
Interface DataSource
{
public static function getinstance ();
}
Here we assume we're working with a database, DataSource defines an interface, and all instance classes that implement that interface get a static object
Interface Controller
{
Public function pop ();
Public function push ();
Public function execute ();
}
Interface View
{
Public function display ();
}
All right, here we go.
The following defines a class to implement the DataSource interface, which uses a single instance pattern
Class Databasesource implements DataSource
{
public static $instance = null;
public static function getinstance ()
{
if (self:: $instance = = null)
{
Self:: $instance = = new PDO ("Mysql:host=localhost;dbname=article", "root", "123456");
}
Return self:: $instance;
}
}
To define an abstract class to implement Dataoperation, we want to share a database connection, so I initialize the database object in an abstract class so that all subclasses can share the object
Abstract class Databaseoperation implements Dataoperation
{
protected $db = null;
Public Function __construct ()
{
$this->db = Databasesource::getinstance ();
}
Public Function Select ($info);
Public Function Select ($info);
}
Let me write a business subclass to implement the abstract class Databaseoperation
Class Tech extends Databaseoperation
{
Public Function Select ($info)
{
To implement your code here
}
Public Function Selectnum ($info)
{
To implement your code here
}
}
Business Logic Layer We have implemented, the following is the control layer
Class Viewcontroller implements Controller
{
Private $mod = Array ();
Public function push ($key, $value);
{
Implement your code, register the class into the $this->mod;
}
Public function pop ($key)
{
Implementation of your code, the $this->mod[$key] value is null;
}
Public function execute ($key)
{
Here to implement your code, generate an instance. Note the use of PHP5 new features, exception handling
}
}
Okay, here's the presentation layer, which will implement Interface View
Abstract Articleview implements View
{
protected $smarty = null;
Public Function __construct ()
{
$this->smarty = new Smarty ();
Below you can define some of the attribute values of Smarty
}
}
Specific pages, such as the display page for science and technology articles
Class Techarticleview extends Articleview
{
Public Function display ()
{
Implement your code, call the tech class and more databaseoperation subclasses
}
}
Okay, here's the main entrance index.php.
Try
{
$viewController = new Viewcontroller ();
$viewController->push ("Tech", Techarticleview);
continued to increase
$mod = $_get["mod"]:$_get["mod"]:$_post["mod"];
At last
$viewController->execute ($key);
}
catch (Exception $e)
{
How to handle the anomaly is your business.
}
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.