PHP object-oriented programming and design Patterns (3) _php tutorial

Source: Internet
Author: User
PHP Advanced Programming Learning Note 2014.06.11

Design patterns are a set of reusable, most known, categorized purposes, code design experience Summary. Design patterns are used in order to reuse code, make code easier for others to understand, and ensure code reliability. There is no doubt that design patterns in others in the system are multi-win; Design patterns make code production truly engineering; Design patterns are the cornerstone of software engineering, like the structure of a building.

Single-Case mode

Singleton mode is useful when you need to ensure that an object can have only one instance. It delegates control of the created object to a single point, and at any time the application will only have one instance to exist. A singleton class should not be able to instantiate outside of a class A singleton class should have the following elements.

You must have a private access-level constructor that effectively prevents classes from being instantiated arbitrarily.

You must have a static variable that holds an instance of the class.

You must have a public static method that accesses this instance, which is usually named GetInstance ().

You must have a private, empty __clone method that prevents the instance from being cloned.

The following is a simple example of a singleton class to illustrate

classclassname{ Public Static $_instance; Private function__construct () {#code ...    }    Private function__clone () {#Empty    }     Public Static functiongetinstance () {if(! (Self::$_instanceinstanceof Self)) { Self::$_instance=NewSelf (); }        returnSelf::$_instance; }     Public functionSayhi () {Echo"Hi boy!"; }}$App= ClassName::getinstance ();$App-Sayhi ();/** * * Output * * Hi boy! * */

Simple Factory mode

When you have a large number of classes that implement the same interface, instantiating the appropriate classes at the right time, if you spread these new to all corners of the project, not only makes the business logic confusing and makes the project difficult to maintain. If we introduce the concept of factory model, we can deal with this problem very well. We can also allow the factory class to return the appropriate instance to us through the application configuration or by providing parameters.

The factory class, which puts the process of instantiating classes into the various factory classes, dedicated to creating objects of other classes. Factory patterns are often used in conjunction with interfaces so that applications do not have to know the specifics of these instantiated classes, as long as it is easy to know that the factory returns a class that supports an interface. Below is a brief example of the use of the factory class.

Interfaceproductinterface{ Public functionshowproductinfo ();}classProductAImplementsproductinterface{functionShowproductinfo () {Echo' This is product A. '; }}classProductBImplementsproductinterface{functionShowproductinfo () {Echo' This is product B. '; }}classproductfactory{ Public Static functionFactory$ProductType)    {                $ProductType= ' Product '.Strtoupper($ProductType); if(class_exists($ProductType))        {            return New $ProductType(); }        Else        {            Throw New Exception("Error processing Request", 1); }}}//need an object of product model a$x= Productfactory::factory (' A ');$x-Showproductinfo ();
We need an object with a product model B.$o= Productfactory::factory (' B ');$o-Showproductinfo ();

You can call the Showproductinfo method because both implement the interface Productinterface.?>

Summary

The pattern is like the cornerstone of software engineering. Like the design drawings of the building, there are two modes of contact: single-case mode and engineering mode. There is a static variable in the Singleton class that holds an instance of itself and provides a static method to get the static variable. The Singleton class should also mark the constructor and the clone function as private to prevent the uniqueness of the broken instance. Factory mode creates different types of instances based on the parameters passed in or the configuration of the program, the factory class returns objects, and the factory class is critical in the practice of polymorphic programming.

http://www.bkjia.com/PHPjc/781929.html www.bkjia.com true http://www.bkjia.com/PHPjc/781929.html techarticle PHP Advanced Programming Learning Note 2014.06.11 design pattern is a set of iterative, most known, categorized purposes, code design experience Summary. To make a.

  • 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.