Simple analysis of _php example of PHP single example mode

Source: Internet
Author: User

This series of articles summarizes the application of design patterns in PHP, which is the first single case pattern of the creation pattern.

Introduction of Design pattern
First, let's get to know what design patterns are:
Design patterns are a summary of the proven code design experience that is reused and easily understood by others.
Design pattern is not a Java patent, we use object-oriented method in PHP can also be very good use of 23 design patterns.
So what do we often say about architecture, frameworks, and design patterns?
Architecture is a set of architecture, is the overall solution of the project, the framework is reusable semi-finished software, is the specific program code. Architecture generally involves what frameworks are used to speed up and optimize the resolution of a part of the problem, and many design patterns are reasonably used in good framework code.

Second, refining the design pattern of several principles:

Opening and closing principle: The module should be extended open, and the modification closed.
The Richter substitution principle: If the parent class is invoked, then it is entirely possible to change to a subclass.
Dependency reversal principle: Abstract does not depend on the details, interface programming, passing parameters as far as possible to refer to high-level classes.
Interface Isolation principle: each interface is responsible for only one role.
Synthesis/aggregation Reuse principle: To use synthesis/aggregation as far as possible, do not abuse inheritance.

Third, the function of design pattern?

Design patterns can solve
Replace the messy code to form a good code style
The code is easy to read and engineers can easily understand
Add new features without modifying the interface, scalability is strong
Good stability, generally do not appear unknown problems
Design pattern cannot be resolved:
Design patterns are used to organize the templates of your code, rather than directly calling libraries;
Design patterns are not the most efficient, but the readability and maintainability of the code is more important;
Do not blindly pursue and apply the design pattern, refactoring more consideration;

Iv. Classification of design patterns
1. Create mode:
Single case mode, Factory mode (Simple factory, factory method, abstract factory), creator mode, prototype mode.
2, the structure of the model:
Adapter mode, bridging mode, decoration mode, combination mode, appearance mode, enjoy meta mode, Agent mode.
3. Behavioral pattern:
Template method mode, command mode, iterator mode, observer mode, Mediator mode, Memo mode, interpreter mode, state mode, policy pattern, responsibility chain mode, visitor pattern.
V. Creating Design Patterns
1. Single Case mode
Purpose: To ensure that a class has only one instance and provides a global access point to access it.
Scenario: Database connection, cache operation, distributed storage.

Copy Code code as follows:

/**
* Single case mode
*/
Class Dbconn
{
private static $_instance = null;
protected static $_counter = 0;
protected $_db;
Privatisation constructor, do not allow external instance creation
Private Function __construct ()
{
Self::$_counter + 1;
}
Public Function getinstance ()
{
if (self::$_instance = null)
{
Self::$_instance = new Dbconn ();
}
return self::$_instance;
}
Public Function Connect ()
{
echo "Connected:". (Self::$_counter). " n ";
return $this->_db;
}
}
/*
* When you do not use a single case mode, you delete the constructor's private and then test it, and the second time the constructor is invoked, the _counter becomes 2
*/
$conn = new Dbconn ();
$conn->connect ();
$conn = new Dbconn ();
$conn->connect ();
You cannot direct the new object after using the single case mode, you must call getinstance to get
$conn = Dbconn::getinstance ();
$db = $conn->connect ();
The second call is the same instance, _counter or 1
$conn = Dbconn::getinstance ();
$db = $conn->connect ();

Special Note: Here getinstance have if to judge and then generate objects, in a multithreaded language there will be concurrent problems. For example, there are two Java solutions, either to the method plus synchronized keyword synchronization, or to the initialization of the _instanc in advance to the class member variable definition, but these 2 kinds of ways PHP does not support. However, because PHP does not support multithreading, there is no need to consider this issue.

Do the small partners know anything about the PHP design pattern's single example model? Next article we will introduce the next factory model.

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.