PHP Object-Oriented learning note the design pattern of the second generation object _php tutorial

Source: Internet
Author: User
I. Singleton mode (Singleton)
If the application contains and contains only one object at a time, then the object is a single instance. Used to override global variables.
Copy CodeThe 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 ");
?>


two. Factory method Mode (factory methods) to solve the problem:

1> know the type of object to be generated when the code is running; 2> object types may want to expand the new product type; 3> each product type can be customized to specific functions; The factory method mode separates the creator class from the product class to be produced. The creator is a factory class that defines the class method that is used to build the Product object. If you do not provide a default implementation, the subclass of the Creator class performs the instantiation. In general, It is the subclass of each creator class that instantiates a corresponding product subclass. The advantage of the Factory mode is on the creation object. Its task is to encapsulate the creation of the object, and then return a new class that is needed. To change the structure of an object and the way it is built, simply select the object factory and change the code only once. (The functionality of Factory mode is so powerful that it is at the bottom of the application, so it keeps appearing in many of the remaining complex patterns and applications.) ) Different processing objects, internal automatic shunt processing, but for the user, there is only one method, simple and convenient to use the interface way to practice the Factory mode example:
Copy CodeThe code is as follows:
Interface hello{
function Say_hello ();
}
Class 中文版 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 中文版 ();
else if ($type = = 2) $temp = new Chinese ();
else{
Die ("Not supported!");
}
return $temp;
}
}
$test = speak::factory (1);
$test->say_hello ();

In <深入浅出设计模式> , the above is referred to as the Simple Factory mode, 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 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 pattern: The factory method is an abstract class or interface, the specific factory implements this method (interface), lets the user call to create the concrete product object (each product has corresponding specific factory) below is the rewritten hello
Copy CodeThe code is as follows:
Abstract Factory
Interface speaker{
function Assignspeaker ();
}
Specific Factory 1
Class Englishspeaker implements speaker{
Public Function Assignspeaker () {
return new 中文版 ();
}
}
Specific Factory 2
Class Chinesespeaker implements speaker{
Public Function Assignspeaker () {
return new Chinese ();
}
}
Abstract Products
Interface hello{
function Say_hello ();
}
Specific Product 1
Class 中文版 implements hello{
Public Function Say_hello () {
echo "hello!";
}
}
Specific Product 2
Class Chinese implements hello{
Public Function Say_hello () {
echo "Hello";
}
}

Use:
Copy CodeThe 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 product family; Each entity factory is responsible for a product family (...) Products, and each product family is divided into several different categories (A, b ...) From one entity factory, it's actually a factory method pattern.

If the above hello example, and more out of the expression, normal and singing expression (2 product families)
Copy CodeThe 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 Products
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! Hello! Hello! ";
}
}
Specific product B2
Class Singchinese implements hello{
Public Function Say_hello () {
echo "Jingle, jingle, Hello!" Hello, hello! ";
}
}

Use:
Copy CodeThe code is as follows:
Determine the specific plant based on the business logic of the program
Switch ($_get[' language ')} {
Case 1: $temp =new englishspeaker ();
Break
Case 2: $temp =new chinesespeaker ();
Break
}
According to the business logic of the program to determine the specific product, no need to care about which specific factory, maintenance improvement
$man = $temp->assignspeaker ($_get[' style ');
Use of the product, no need to care about which specific product
$man->say_hello ();

Four. Prototype mode (PROTOTYPE)
Using clone to replicate specific products that already exist, then the specific product classes themselves become the basis for their own generation.

http://www.bkjia.com/PHPjc/326063.html www.bkjia.com true http://www.bkjia.com/PHPjc/326063.html techarticle I. Singleton mode (SINGLETON) if the application contains and contains only one object at a time, the object is a single example. Used to override global variables. Copy the code code 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.