PHP Object-Oriented Learning Note 2: Design pattern for generating objects _ PHP Tutorial

Source: Internet
Author: User
PHP Object-Oriented Learning Note 2: The design mode for generating objects. 1. Singleton: if an application contains only one object at a time, this object is a Singleton. it is used to replace global variables. The Copy code is as follows :? Phpr 1. Singleton)
If an application contains only one object at a time, this object is a singleton to replace global variables.

The code is as follows:


Require_once ("DB. php ");
Class DatabaseConnection {
Public static function get (){
Static $ db = null;
If ($ db = null)
$ Db = new DatabaseConnection ();
Return $ db;
}
Private $ _ handle = null;
Private function _ construct (){
$ Dsn = 'MySQL: // root: password @ localhost/photos ';
$ This-> _ handle = & DB: Connect ($ dsn, array ());
}
Public function handle ()
{
Return $ this-> _ handle;
}
}
Print ("Handle =". DatabaseConnection: get ()-> handle (). "\ n ");
Print ("Handle =". DatabaseConnection: get ()-> handle (). "\ n ");
?>



II. problems to be solved by the factory method:

1> the object type to be generated is known only when the code is running; 2> the object type may need to be expanded to the new product type; 3> Specific features can be customized for each product type; the factory method mode separates the creator class from the product class to be produced. the creator is a factory class, which defines the class methods used to generate product objects. if the default implementation is not provided, the subclass of the creator class is used for instantiation. generally, a product subclass is instantiated for each creator class. the advantage of the factory mode lies in the creation of objects. Its task is to encapsulate the object creation process and return a new class. To change the object structure and object creation method, you only need to select the object factory and change the code only once. (The factory model is so powerful that it is at the bottom of the application, so it will not stop appearing in many other complex models and applications .) Different processing objects are automatically distributed internally, but for users, there is only one method, which is simple and convenient to use the interface to practice the factory mode. example:

The code is as follows:


Interface Hello {
Function say_hello ();
}
Class English implements Hello {
Public function say_hello (){
Echo "Hello! ";
}
}
Class Chinese implements Hello {
Public function say_hello (){
Echo "Hello ";
}
}
Class speak {
Public static function factory ($ type ){
If ($ type = 1) $ temp = new English ();
Else if ($ type = 2) $ temp = new Chinese ();
Else {
Die ("Not supported! ");
}
Return $ temp;
}
}
$ Test = Speak: factory (1 );
$ Test-> say_hello ();


In <深入浅出设计模式> The above is called the simple factory model, because this factory must be able to distinguish all the products to be produced. if a new product exists, the factory must be modified accordingly to add the corresponding business logic or judgment. A sign of the simple factory model is the static method to implement the factory production function. (not simple) factory method mode: The Factory method is an abstract class or interface, the specific factory implements this method (interface ), let the user call to create a specific product object (each product has a specific factory). below is the rewritten hello

The code is as follows:


// Abstract factory
Interface Speaker {
Function assignSpeaker ();
}
// Specific factory 1
Class EnglishSpeaker implements Speaker {
Public function assignSpeaker (){
Return new English ();
}
}
// Factory 2
Class ChineseSpeaker implements Speaker {
Public function assignSpeaker (){
Return new Chinese ();
}
}
// Abstract product
Interface Hello {
Function say_hello ();
}
// Product 1
Class English implements Hello {
Public function say_hello (){
Echo "Hello! ";
}
}
// Product 2
Class Chinese implements Hello {
Public function say_hello (){
Echo "Hello ";
}
}


Usage:

The code is as follows:


If (! Empty ($ _ GET ['t']) {
Switch ($ _ GET ['t']) {
Case 1: $ temp = new EnglishSpeaker ();
Break;
Case 2: $ temp = new ChineseSpeaker ();
Break;
}
$ Man = $ temp-> assignSpeaker ();
$ Man-> say_hello ();
}


III. abstract Factory product family; each physical Factory is responsible for one product family (1, 2 ...) product, and each product family is divided into several different categories (A, B ...) from the perspective of a real factory, it is actually a Factory method model.

If the above hello example shows more expressions, normal expressions and singing expressions (two product families)

The code is as follows:


// Abstract factory
Abstract class Speaker {
Const NORMAL = 1;
Const SING = 2;
Abstract function assignSpeaker ($ flag_int );
}
// Specific factory 1
Class EnglishSpeaker extends Speaker {
Public function assignSpeaker ($ flag_int ){
Switch ($ flag_int ){
Case self: NORMAL:
Return new NormalEnglish ();
Break;
Case self: SING:
Return new SingEnglish ();
Break;
}
}
}
// Factory 2
Class ChineseSpeaker extends Speaker {
Public function assignSpeaker ($ flag_int ){
Switch ($ flag_int ){
Case self: NORMAL:
Return new NormalChinese ();
Break;
Case self: SING:
Return new SingChinese ();
Break;
}
}
}
// Abstract product
Interface Hello {
Function say_hello ();
}
// Specific product A1
Class NormalEnglish implements Hello {
Public function say_hello (){
Echo "Hello! ";
}
}
// Product B1
Class NormalChinese implements Hello {
Public function say_hello (){
Echo "Hello! ";
}
}
// Product A2
Class SingEnglish implements Hello {
Public function say_hello (){
Echo "Oh, jingle bells, jingle bells, Hello! Hello! Hello! ";
}
}
// Product B2
Class SingChinese implements Hello {
Public function say_hello (){
Echo "dingting, Dingding, Hello! Hello! Hello! ";
}
}


Usage:

The code is as follows:


// Determine the specific factory based on the business logic of the program
Switch ($ _ GET ['language']) {
Case 1: $ temp = new EnglishSpeaker ();
Break;
Case 2: $ temp = new ChineseSpeaker ();
Break;
}
// Determine the specific product based on the business logic of the program. you do not need to worry about the specific factory. The maintainability is improved.
$ Man = $ temp-> assignSpeaker ($ _ GET ['style']);
// You do not need to care about the specific product.
$ Man-> say_hello ();


4. Prototype)
Clone is used to copy existing products, and then the specific product class itself becomes the basis for their own generation.

Examples. Singleton: if an application contains only one object at a time, this object is a Singleton. this is used to replace global variables. the code is as follows :? Php r...

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.