In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
In the basic MVC framework model, the database and template files are referenced. But different developers will choose different databases and template engines. Because in the actual development process, it is possible to change the database or template engine, if for each database and template engine to write an interface to call, it will be very cumbersome. So the emergence of factory class solves this problem very well.
Here I define a database factory class and a template engine factory class.
1. First, let's look at the database factory class:
I assume that I am using a mysql database, and the database factory class encapsulates the method of instantiating the database (which database is used) and the related methods of database operations (additions, deletions, changes, queries, etc.).
When using the database factory class, first determine what database to use. Here I need to write an initialization method in the database factory class, as shown below. $dbtype is the specified database type, and $config is the configuration information passed in to the database.
Public static function init ($dbtype, $config) {self::$db = new $dbtype;self::$db- > connect ($config);}
After initializing the database, you can call the operation methods of each database, and the methods we define in the factory class can be used by any kind of database. For example, the insertion method:
Public static function insert ($table,$arr) {return self::$db- > insert ($table,$arr);}
Two parameters are defined here, one table name and one inserted data. Then the insert method of the mysql database is called to implement the specific insert operation.
Summary: using the database factory class, that is, a standard is defined for the required database-related functions, and the instantiated objects of the database can be obtained no matter what database is passed in. We just need to define the operation methods in the factory class in order to call the actual database operation methods.
2. Template engine factory class
Similar to the database factory class, the template engine factory class is not targeted at any kind of template file. The method of initializing the template engine is also defined to determine which template engine to use.
Here, we assume that the template engine we use is Smarty template, and the initialization method is as follows:
Public static function init ($viewtype,$config) {self::$view = new $viewtype;foreach ($config as $key= > $value) {self::$view-> $key= $value;}
$viewtype is the type of view template, and $config is the configuration information (in this case, the array file).
After determining the view template engine, like the database factory class, you can define the methods of related functions to call the actual template engine operation methods.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.