Learn design patterns with me (2), learn design patterns _ PHP tutorials

Source: Internet
Author: User
Learn design patterns with me (2). learn design patterns. Learn design patterns with me (II). The Design patterns factory patterns are very important in the design of polymorphism. if appropriate, they can make the application more portable, learning the design pattern between classes with me (2) learning the design pattern

The factory model is very important in the design of polymorphism. if used properly, it can make the application more portable, the dependency between classes more loose, and thus improve flexibility. If the Singleton mode is a class responsibility, then the factory mode is a class polymorphism.

So what is a factory?

Concept: A factory class refers to a class that contains a method specifically used to create other objects.

Application scenario: The Factory mode is usually used to return different classes that conform to similar interfaces. That is to say, the factory class allows us to decide which class to instantiate based on the configuration or application logic.

The following is a simple factory code:

interface IDatabase {  //...public function query($sql);}class DBFactory {    public static function create($type){        $db = null;        switch ($type) {            case 'mysql':                $db = new Mysql(/**arguments*/);                break;            case 'sqlite':                $db = new Sqlite(/**arguments*/);                break;            case 'pgsql':                $db = new PGsql(/**arguments*/);                break;            default:                # code...                break;        }        return $db;    }}class Mysql implements IDatabase {   //...    public function query($sql){    }}/**other class ...*/

Factory type:

$db = DBFactory::create('mysql');$db->query('show database');

Each database here inherits the specified interface, so that all database objects are consistent externally. External classes can safely use the methods stated in interfaces, that is, what we often say in software engineering is transparent to users. Assume that one day, we need to replace the data center with another type of database. we only need to implement the relevant database classes according to the interface, and the business code does not need to be changed. This reflects the flexibility and polymorphism of the factory class.

From another point of view, we put all the changes at the entrance. There is no need to repeat if-else for these changes internally.

Well, there is only so much theoretical content. more experiences need to be used in projects and more benefits.

Workshop (II), design patterns factory patterns are very important in the design of polymorphism. if the application is appropriate, it can make the application portability better, and the inter-class dependency...

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.