-
- Interface DataOperation
- {
- public function select($info);
- public function selectNum($info);
- }
-
Copy code The above interface definition reads Data interface, the select method will return the required articles.
The selectNum method returns the total number of articles, which is used for paging display.
$info is an array used to store query conditions.
- Interface DataSource
- {
- public static function getInstance();
- }
Copy code It is assumed here that the database is operated, and DataSource defines an interface, and all those that implement this interface The instance class will get a static object.
- Interface Controller
- {
- public function pop();
- public function push();
- public function execute();
- }
- Interface View
- {
- public function display();
- }
Copy the codeDefine a class to implement the DataSource interface, using the singleton 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;
- }
- }
Copy code Define an abstract class to implement DataOperation. We want to share a database connection, so initialize the database object in the abstract class, so that all subclasses can share this object.
- abstract class DataBaseOperation implements DataOperation
- {
- protected $db = null;
- public function __construct()
- {
- $this->db = DataBaseSource::getInstance();
- }
- public function select($info);
- }
Copy code
Let’s write a business subclass to implement the abstract class DataBaseOperation
-
- class Tech extends DataBaseOperation
- {
- public function select($info)
- {
- //Implement your code here
- }
- public function selectNum($info)
- {
- / /Implement your code here
- }
- }
-
Copy the code After implementing the business logic layer, open the control layer.
class ViewController implements Controller - {
- private $mod = array();
- public function push($key,$value);
- {
- //implement you code, register the class into $this->mod;
- }
- public function pop($key)
- {
- //Implement your code, set the value of $this->mod[$key] to null;
- }
- public function execute($key)
- {
- //Implement your code here and generate an example. Pay attention to using the new features of php5, exception handling
- }
- }
- //Presentation layer, Interface View will be implemented here
- abstract ArticleView implements View
- {
- protected $smarty = null;
- public function __construct()
- {
- $this->smarty = new Smarty();
- /// Below you can define some attribute values of smarty
- }
- }
- Specific pages, such as the display page of technology articles
- class TechArticleView extends ArticleView
- {
- public function display()
- {
- //Implement your code, call the Tech class and more DataBaseOperation subclasses
- }
- }< ;/p>
//General entrance index.php
- try
- {
- $viewController = new ViewController();
- $viewController->push("tech",TechArticleView);
- //Continuous increase
- $mod = $_GET["mod"]:$_GET["mod"]:$_POST["mod"];
- //Finally
- $viewController->execute($key);
- }
- catch(Exception $e)
- {
- //Handling exceptions
- }
- ?>
-
Copy code
|