A brief analysis of PHP design pattern

Source: Internet
Author: User
Tags sqlite

Factory mode

The most mentioned, the use is also the most extensive. Simply put: Define an interface for user-created objects.

Simple Factory mode:
① abstract base class: Some methods of defining abstractions in classes to implement in subclasses
② inherits from the subclass of the abstract base class: Implementing an abstract method in a base class
③ Factory class: Used to instantiate objects

As long as the different class instances can be generated according to different parameters, then it conforms to the design idea of the Factory mode.

This makes it easy to expand. Instead of using code new Mysql ($host, $username, $password, $dbname) in the direct link database, we can dynamically generate an instance of a connected database. It can be MySQL, or it can be connected to Oracle. Interface DB {    function connect ();} Interface Factory {    function createdb ();} Class MySQLdb implements DB {    public  function  Connect () {        echo ' connected to MySQL ';    }} Class SQLite implements DB {    public  function connect () {        echo ' connected sqllist ';    }} Class Createmysql implements Factory {public    function createdb () {        return new mysqldb ();    }} Class Crearelite implements Factory {public    function createdb () {        return new SQLite ()    }} $fact = new creat Emysql (); Var_dump ($fact->createdb ()->connect ());

  

This one's good.

http://blog.csdn.net/hguisu/article/details/7505909

A brief analysis of PHP design pattern

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.