iterator ModeTraversing an internal element of an aggregate object without needing to know the internals, compared to the traditional programming model,
iterator ModeYou can hide all actions that traverse an element
An iterator pattern is a very high-frequency design pattern that allows the traversal of data to be detached from a clustered object by introducing iterators, which are only responsible for storing data, and traversing the data is done
Symfony2 Many design pattern ideas, the following information will be a bit helpful: http://www.open-open.com/lib/view/open1414996676559.htmlRead Catalogue
Creational
Structural
Behavioral
More
Here are some sample code that implements the famous design pattern in PHP. Each pattern has examples (most of which come from the Zend Framework
Method Static Public functionGetinstanceentry () {//public static method, the entry of an external instance object if(!self::$objInstanceof self) {//instanceof Determining whether an object is instantiatedSelf::$obj=NewSelf ();//or the new Danli () } returnSelf::$obj; } Public functionGetName ($nn){ return $nn; }}$t= Danli::getinstanceentry ();Echo $t-GetName (' ABC '));?>Factory mode: Is a class, in this class is some way to create objects, so that w
Note that iterators are a better abstraction than object sets, because we can let infiniteiterators,norewinditerators and so on, without being consistent with normal array arrays, so iterator lacks functions such as the count () function.
A complete list of SPL iterators can be found in the official PHP manual. Thanks to strong support for PHP, much of the work using the iterator pattern is included in the
(); return static::$_instance; } Clone functions and constructors must be declared private, //Prevent external programs from creating a new class, and losing the meaning of singleton patterns. Private Function __clone () { } Private Function __construct () { } Public Function Open () { echo "init times:".) Static:: $token. ' Comparison of a singleton with the static class: 1, Singleton can be very
This article introduces three kinds of commonly used PHP design patterns: Singleton mode, Factory mode, observer mode, has a good reference value, followed by small series to see it
First, the singleton mode
The so-called Singleton pattern is that only one instance of this class exists in the application.
Typically, singleton mode is used in instances where only
PHP design patterns-six principles
Code that follows the following six principles is generally considered to be easily scalable and reusable:
These six principles should be followed by any object-oriented language. If you want to make your code easy to expand and take high, try to satisfy these six principles. It is not necessarily based on a certain
Interface in PHP can only play a role in the definition of constraint class, although not as intuitive as C#/java, but based on the requirements of the package of OOP, the use of interfaces can improve the scalability of the program, such as the implementation of proxy design patterns.
Copy CodeThe code is as follows:
Human interface
Interface Ihuman
{
function
PHP 3 Basic Design Patterns combined use
1.1 工厂模式 , the factory method or class generates the object instead of the code directly in the new
class Factory{ static function getDatabase(){ return new Mysql($host, $user, $pass); } } #使用 Factory::getDatabase();
1.2 单例模式 , making an object of a class run only to create a
PHP design mode single-instance modeA singleton pattern is a creation pattern that restricts an application to create only a single instance of a particular class type. For example, a Web site would require a database connection object, but there should be only one, so we implemented this limitation by using a singleton pattern.We can use a static property to ensure that there is only one single instance fo
Single-instance learning for php design patterns
Class {
Private function _ construct (){}
}
$ A = new ();
?>
Program running result: Fatal error: Call to private A :__ construct () from invalid context in E: \ PHPProjects \ test. php on line 6We have disabled t
Design Patterns in PHP--façade mode
' ;} }//(2) PC Machine class Pcmachine {public Function turnOn () {} public Function turnoff () {echo ' Turn off pcmathion' ;}} (3) Power off class Power {public Function turnOn () {} public Function turnoff () {echo ' Turn off power' ; }}//shutdown of the façade role class Pcfacade implements Facade{private $PcLight; priva
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:
[email protected]
and provide relevant evidence. A staff member will contact you within 5 working days.