In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use the design pattern of generating objects in PHP. It is very detailed and has a certain reference value. Friends who are interested must read it!
one。 Singleton mode (Singleton)
If the application contains only one object at a time, then this object is a singleton. Used to replace global variables.
The copy code is as follows:
two。 Problems to be solved by the Factory method pattern (factory method):
1 > the object type to be generated is known only when the code is running; 2 > the object type may expand the new product type; 3 > each product type can be customized for specific functions; the factory method pattern separates the creator class from the product class to be produced. The creator is a factory class that defines the class methods used to generate product objects. If no default implementation is provided, the instantiation is performed by a subclass of the creator class. In general, a subclass of each creator class instantiates a corresponding product subclass. The advantage of the factory pattern is in creating objects. Its task is to encapsulate the object creation process and return a new class as needed. To change the structure of the object and the way it is created, you only need to select the object factory and change the code only once. The factory pattern is so powerful that it is at the bottom of the application, so it keeps appearing in many other complex patterns and applications.) Different processing objects are automatically distributed internally, but for users, there is only one method, which is simple and convenient to use interfaces to practice factory mode:
The copy 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 the factory must be able to distinguish all the products to be produced. If there is a new product, the factory must be modified and the corresponding business logic or judgment must be added. One of the hallmarks of the simple factory model is the static method to achieve factory production functions. Factory method pattern: the factory method is an abstract class or interface, and the concrete factory implements this method (interface), which is called by the user to create a concrete product object (each product has a corresponding concrete factory) the following is the rewritten hello
The copy code is as follows:
/ / Abstract Factory
Interface Speaker {
Function assignSpeaker ()
}
/ / specific factory 1
Class EnglishSpeaker implements Speaker {
Public function assignSpeaker () {
Return new English ()
}
}
/ / specific factory 2
Class ChineseSpeaker implements Speaker {
Public function assignSpeaker () {
Return new Chinese ()
}
}
/ / Abstract product
Interface Hello {
Function say_hello ()
}
/ / specific product 1
Class English implements Hello {
Public function say_hello () {
Echo "Hello!"
}
}
/ / specific products 2
Class Chinese implements Hello {
Public function say_hello () {
Echo "Hello"
}
}
Use:
The copy 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 ()
}
three。 Abstract factory pattern (Abstract Factory) product family; each entity factory is responsible for one product family (1mem2.) , and each product family is divided into several different categories (AMagneB.) From a physical factory alone, it is actually a factory method model.
If the hello example above, there are more expressions, normal and singing expressions (2 product families)
The copy 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
}
}
}
/ / specific 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!"
}
}
/ / specific product B1
Class NormalChinese implements Hello {
Public function say_hello () {
Echo "Hello!"
}
}
/ / specific product A2
Class SingEnglish implements Hello {
Public function say_hello () {
Echo "Oh, jingle bells, jingle bells, Hello!"
}
}
/ / specific product B2
Class SingChinese implements Hello {
Public function say_hello () {
Echo "Tinker Bell, Hello!"
}
}
Use:
The copy code is as follows:
/ / determine the specific factory according to the business logic of the program
Switch ($_ GET ['language']) {
Case 1: $temp=new EnglishSpeaker ()
Break
Case 2: $temp=new ChineseSpeaker ()
Break
}
/ / determine specific products according to the business logic of the program, no need to care about which specific factory it is, and the maintainability is improved.
$man=$temp- > assignSpeaker ($_ GET ['style'])
/ / using the product, there is no need to care about which specific product
$man- > say_hello ()
four。 Prototype pattern (Prototype)
Use clone to copy existing concrete products, and then the specific product classes themselves become the basis for their own generation.
These are all the contents of the article "how to use the Design pattern of generated objects in PHP". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.