1 Factory mode the factory mode is a type. it has some methods for creating objects for you. you can use the factory class to create objects instead of using new directly, if you want to change the created object
1. factory model
Factory mode is a type. it has some methods for creating objects for you. you can use the factory class to create objects instead of using new directly, if you want to change the type of the created object, you only need to change the factory, and all the code using the factory will be changed automatically.
The main function is to reduce coupling. the code is as follows:
- Abstract class Operation {
- Abstract public function getValue ($ num1, $ num2 );
- Public function getAttr (){
- Return 1;
- }
- }
- Class Add extends Operation {
- Public function getValue ($ num1, $ num2 ){
- Return $ num1 + $ num2;
- }
- }
- Class Sub extends Operation {
- Public function getValue ($ num1, $ num2 ){
- Return $ num1-$ num2;
- }
- }
- Class Factory {
- Public static function CreateObj ($ operation ){
- Switch ($ operation ){
- Case '+': return new Add ();
- Case '-': return new Sub ();
- }
- }
- }
- $ Op = Factory: CreateObj ('-');
- Echo $ Op-> getValue (3, 6 );
Generally used in real development as a database selection class.
Example 2 shows an example using the factory method. the code is as follows:
-
- Interface IUser
- {
- Function getName ();
- }
- Class User implements IUser
- {
- Public static function Load ($ id)
- {
- Return new User ($ id );
- }
- Public static function Create ()
- {
- Return new User (null );
- }
- Public function _ construct ($ id ){}
- Public function getName ()
- {
- Return "Jack ";
- }
- }
- $ Uo = User: Load (1 );
- Echo ($ uo-> getName (). "n ");
- ?>
This code is much simpler. it only has one IUser interface and one User class that implements this interface. the User class has two static methods for creating objects. PHP improves efficiency through the factory mode.
The following code creates the following four files:
- // Index. php
-
- Include_once ("f. inc. php ");
- $ F = new factory;
- $ T1 = & $ f-> create ('T1 ');
- Echo $ t1-> getName ();
- Echo $ config;
- ?>
- // F. inc. php
-
- Class factory
- {
- Function factory ()
- {
- $ This-> mClasses = array ('T1' => 't1. inc. php', 'T2' => 'T2. inc. php ');
- }
- Function & create ($ class)
- {
- If (! Class_exists ($ class ))
- {
- Require_once ($ this-> mClasses [$ class]);
- }
- Return new $ class;
- }
- }
- ?>
- // T1.inc. php
-
- Global $ config;
- $ Config = 'surfchen ';
- Class T1
- {
- Var $ mName = 'name: T1 ';
- Function getName ()
- {
- Return $ this-> mName;
- }
- }
- ?>
- // T2.inc. php
-
- Class T2
- {
- Function T2 ()
- {
- Echo 'T2 is OK ';
- }
- }
- ?>
In index. php, we use a factory class to create other class instances.
In the factory, an array $ this-> mClasses is saved in the format of array ("Class name" => "class file path ").
When we create a class instance through factory: create (), in create (), we first check whether the class exists. if it does not exist, the class file corresponding to the class is included according to $ this-> mClasses. Create and return an instance of this class.
In this way, we only need to include the factory class file into the execution script (such as index. php). you may have noticed that t1.inc. the code in php is as follows: global $ config; $ config = 'surfchen ';
2. Singleton mode
Simply put, the PHP Singleton mode is a function implemented by a class, and only one instance of this class exists in the entire application;
The factory model emphasizes object-oriented Polymorphism. it can encapsulate functional differences to implement common functions externally, such as database links, we may need to write different methods to implement database links, but as an application, it does not need to know whether the database uses MySQL or Oracle, and it only needs to use query () this method can be used to execute the corresponding operations on the database, thus shielding the differences and making the class more robust! The code is as follows:
- Class Mysql {
- Public static $ conn;
- Public static function getInstance (){
- If (! Self: $ conn ){
- New self ();
- Return self: $ conn;
- } Else {
- Return self: $ conn;
- }
- }
- Private function _ construct (){
- Self: $ conn = "mysql_connect:"; // mysql_connect ('','','')
- }
- Public function _ clone ()
- {
- Trigger_error ("Only one connection ");
- }
- }
- Echo Mysql: getInstance ();
- Echo Mysql: getInstance ();
In practice, it is used together as a database connection class and a factory mode. calling the Singleton mode based on parameters can improve resource usage efficiency.