Alibabacloud.com offers a wide variety of articles about php objects patterns and practice, easily find your php objects patterns and practice information here online.
have code to execute.
Design Patterns
The singleton pattern solves the problem of how to create a unique object instance throughout the project, and the factory pattern solves the method of not building the instance object through new.
Single-Case mode
$_instance must be declared as a static private variable
Constructors and destructors must be declared private to prevent the external program new class from losing the meaning of the sing
Three basic design patterns (three of the most common in all object-oriented design patterns):① Factory mode : Use factory methods or classes to generate objects instead of directly new in codeCreate a new factory.php under the Common directory:phpnamespace Common; class factory{ staticfunction createdatabase () { //$db = new \common\database; $
, so that the other code cannot instantiate the object of that class by calling its constructor. Only the static method provided by the class is used to obtain a unique instance of the class. ---wikipedia
The main points of the Singleton pattern are: A class can have only one instance; It must create its own instance itself; It must provide this instance to the entire system on its own. Singleton mode is an object-creation pattern.
Role:
Singleton: Single Case class
UML class Diagrams:
Example c
$observer) {$observer->update ();}}}Define an Observer interfaceobserver.phpRequire_once ' loader.php ';Interface observer{function update ();//This is the logic to be executed after the event has occurred}A class that implements the Eventgenerator abstract class for defining an event that occursRealize:Require ' loader.php ';Class Event extends eventgenerator{function Triger () {echo "Event}}Class Observer1 implements observer{function Update () {echo "Logic 1}}Class Observer2 implements obser
: This article mainly introduces the third part of API development: the perfect Singleton mode of PHP design patterns. if you are interested in PHP tutorials, refer to it. Let's talk about the Singleton mode today.
I used to develop java. when I used the Singleton mode, I first thought of using the hungry Chinese style. then I found that
The importance of design patterns in object-oriented objects.
More in-depth understanding of object-oriented thinking, is conducive to the development of highly scalable programs. There is a "closing principle" in the PHP object-oriented: "Software entities should be open to extensions and closed to modifications." Popular saying: Add new class/interface, etc.
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
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
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
1. There is a private static object variab
the factory identified institutions to buy, not everyone to the factory to buy)
2. The next time you want to use an object, do not need to use the factory, nor need to use a singleton mode, directly on the registrar to get it
class Register () { protected Static $objects; function set($alias, $object){ Self::$objects[$alias] =$obj
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
In-depth PHP: Object-oriented, model, and practice (version 2nd)
Basic Information
Author: (US) Matt zandstraTranslator: Chen Hao; Wu sunbin; Hu DanSeries name: Turing programming SeriesPress: People's post and telecommunications PressISBN: 9787115225788Mounting time:Published on: February 1, May 2010Start: 16Other Details View: http://www.china-pub.com/196636
Introduction
This book thoroughly analyzes the
First understand the concept of the Observer pattern: an object is made observable by adding a method that allows another object, the observer to register itself. When an observable object changes, it sends the message to the registered observer. These observers use this information to perform operations that are independent of observable objects. The result is that objects can talk to each other without kn
Overview: As an object's creation pattern, a singleton ensures that only one instance exists in an intrinsic system and that it cannot create a copy. The Clone function (__clone) and the constructor (__construct) must be declared private, preventing the external program from creating a new class and losing the meaning of the singleton pattern. The PHP language is an interpreted language, and when the page is executed, all related resources
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 the database access object is allowed, there
Notes on three common design patterns in php
Class SingetonBasic {
Private static $ instance;
// Other vars ..
Private function _ construct (){
// Do construct ..
}
Private function _ clone (){}
Public static function getInstance (){
If (! (Self: $ instance instanceof self )){
Self: $ instance = new self ();
}
Ret
is conceived out, so oriented to abstract design is in line with human thinking. In addition, this principle will be well supported by the OCP, and the abstraction-oriented design allows us to not rely too much on implementation, so that the extension becomes possible, and this principle is the cornerstone of another article, design by contract.4) Interface segregation Principle (ISP), interface isolation principle, "breaking large interfaces into multiple small interfaces", the benefits are ob
the Observer. The coupling degree is quite low.
How do I implement an observer registration? It's easy to give us ideas through the previous registrant model and add these objects to a registered tree. How to notify? This is even simpler, by traversing the registration tree, allowing each object to implement the operations provided by its interface.
The so-called pattern, more is an idea, there is no need to rigidly adhere to the code details. The O
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.