Factory model of php design model

Source: Internet
Author: User

Factory model of php design model
Factory model:

 

The biggest advantage of the factory mode is that the process of creating an object is encapsulated so that a new object can be generated at any time.

 

Reduce the number of code copies and paste the post, and the coupling relationship is heavy.

In layman's terms, new is used to create an object. Now, this process is encapsulated.

If the factory mode is not used: in many cases, Class a is called, and the Code creates an instance like this: new a (). Assume that the name of Class a needs to be modified one day, this means that a lot of called code must be modified.

Create a factory (a function or a class method) to create a new object. Its task is to encapsulate the object creation process,

Creating an object is not in the new format. Instead, define a method to create an object instance.

 

Factory method:

For example, if your factory is called a cigarette factory, there may be a seven-wolf factory, a Chinese factory, etc. However, this factory only produces one product: cigarettes;

 

Abstract Factory: it cannot describe what product it actually produces. It produces many types of products (so abstract factories generate sub-factories ).

 

Your factory is a comprehensive product that produces "a series" of products instead of "one", such as "cigarettes" and "beer. Then it can also have derived specific factories, but these factories all produce this series of products, just because the regions are different, in order to adapt to the taste of the local, the taste is not the same.

 

Factory model: a factory that generates only one product. For example, cigarettes.

 

Factory method: A product production line of a factory. For example, the keyboard generation process.

 

<Php

/**

* Factory method mode

*

* Defines an interface for creating objects so that the subclass decides which class to instantiate and delays the instantiation of a class to its subclass.

*/

 

/*

Class DBFactory

{

Public static function create ($ type)

{

Swtich ($ type)

{

Case "Mysql ":

Return new MysqlDB (); break;

Case "Postgre ":

Return new analytic dB (); break;

Case "Mssql ":

Return new MssqlDB (); break;

}

}

}

*/

Class DBFactory

{

Publicstaticfunction create ($ type)

{

$ Class = $ type. "DB ";

Returnnew $ class;

}

}

 

Interface DB

{

Publicfunction connect ();

Publicfunctionexec ();

}

 

Class MysqlDB implements DB

{

Publicfunction _ construct (){

Echo "mysql db
";

}

 

Publicfunction connect (){

}

 

Publicfunctionexec (){

}

}

 

Class analytic DB implements DB

{

Publicfunction _ construct (){

Echo "Postgre db
";

}

 

Publicfunction connect (){

}

 

Publicfunctionexec (){

}

}

 

Class MssqlDB implements DB

{

Publicfunction _ construct (){

Echo "mssql db
";

}

 

Publicfunction connect (){

}

Publicfunctionexec (){

}

}

 

 

$ OMysql = DBFactory: create ("Mysql ");

$ OPostgre = DBFactory: create ("Postgre ");

$ OMssql = DBFactory: create ("Mssql ");


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.