Detailed description of the factory mode of the PHP design mode, and detailed description of the php design mode

Source: Internet
Author: User

Detailed description of the factory mode of the PHP design mode, and detailed description of the php design mode

When developing large systems, the following situations often occur:

I have A part of the basic data. The class classA is read from database A, and many other functions are operated based on this basic data. Now, I want to change the data from database A to another data source. At this time, it is troublesome to modify the data, and I need to modify the code of many other classes. This design is obviously not flexible enough. In other words, it is tightly coupled. So what is tightly coupled?Tight couplingIt refers to the behavior and structure of a function or class in a system that is heavily dependent on other functions or classes in the system.

At this time, the role of the factory model is reflected.

Factory Model

Is the design method to solve such situations.

The factory mode is a type. A factory is created to create objects as needed. This mode is important in multi-lingual programming and allows Dynamic Replacement of classes and Configuration modification.

/* Basic factory mode code */

<? Php/*** basic factory mode **/class User {private $ username; public function _ construct ($ username) {$ this-> username = $ username ;} public function getUser () {return $ this-> username;} class userFactory {static public function createUser () {return new User ('jack ');}} $ user = userFactory: createUser (); echo $ user-> getUser ();?>

Factory models include simple factory models, factory method models, and abstract factory models.

In simple factory mode, you can use static methods to create objects. It can be understood that it is only responsible for producing any product in the same level structure, but cannot add products.

<? Php/*** simple factory mode **/interface userProperties {function getUsername (); function getGender (); function getJob ();} class User implements userProperties {private $ username; private $ gender; private $ job; public function _ construct ($ username, $ gender, $ job) {$ this-> username = $ username; $ this-> gender = $ gender; $ this-> job = $ job;} public function getUsername () {return $ this-> username;} public fun Ction getGender () {return $ this-> gender;} public function getJob () {return $ this-> job ;}} class userFactory {static public function createUser ($ properties = []) {return new User ($ properties ['username'], $ properties ['gender'], $ properties ['job']) ;}$ employers = [['username' => 'jack', 'gender' => 'male ', 'job' => 'CODER'], ['username' => 'marry', 'gender' => 'female ', 'job' => 'designer'] ,]; $ User = userFactory: createUser ($ employers [0]); echo $ user-> getUsername ();?>

The factory method mode removes the static attributes of methods in the simple factory mode so that they can be integrated into the quilt class, define an interface for creating objects, and let the subclass decide which class to instantiate. It can be understood that it is used to produce fixed products in the same level structure, but supports adding products.

<? Php/*** factory method mode **/interface userProperties {function getUsername (); function getGender (); function getJob ();} interface createUser {function create ($ properties);} class User implements userProperties {private $ username; private $ gender; private $ job; public function _ construct ($ username, $ gender, $ job) {$ this-> username = $ username; $ this-> gender = $ gender; $ this-> job = $ job;} public functi On getUsername () {return $ this-> username;} public function getGender () {return $ this-> gender;} public function getJob () {return $ this-> job;} class userFactory {private $ user; public function _ construct ($ properties = []) {$ this-> user = new User ($ properties ['username'], $ properties ['gender'], $ properties ['job']);} public function getUser () {return $ this-> user;} class FactoryMan imple Ments createUser {function create ($ properties) {return new userFactory ($ properties) ;}} class FactoryWoman implements createUser {function create ($ properties) {return new userFactory ($ properties) ;}} class clientUser {static public function getClient ($ properties) {$ fac = new FactoryMan; $ man = $ fac-> create ($ properties); echo $ man-> getUser ()-> getUsername () ;}$ employers = [['username' => 'Jack', 'gender' => 'male', 'job' => 'CODER'], ['username' => 'marry ', 'Gender' => 'female', 'job' => 'designer '],]; $ user = clientUser: getClient ($ employers [0]);?>

Abstract Factory mode: provides an interface for creating a series of related or mutually dependent objects. It can be understood that the product is used to produce all the products that do not need any types, but new products cannot be added, and new types can be added.

<? Php/*** Abstract Factory mode **/interface userProperties {function getUsername (); function getGender (); function getJob ();} interface createUser {// abstracts the creation of an object into an interface function createOpen ($ properties); // creates a function createIntro ($ properties) internally ); // create externally} class User implements userProperties {private $ username; private $ gender; private $ job; public function _ construct ($ username, $ gender, $ job) {$ this-> username = $ u Sername; $ this-> gender = $ gender; $ this-> job = $ job;} public function getUsername () {return $ this-> username;} public function getGender () {return $ this-> gender;} public function getJob () {return $ this-> job;} class userFactory {private $ user; public function _ construct ($ properties = []) {$ this-> user = new User ($ properties ['username'], $ properties ['gender'], $ properties ['job']);} public Function getUser () {return $ this-> user;} class FactoryMan implements createUser {function createOpen ($ properties) {return new userFactory ($ properties );} function createIntro ($ properties) {return new userFactory ($ properties) ;}} class FactoryWoman implements createUser {function createOpen ($ properties) {return new userFactory ($ properties );} function createIntro ($ properties) {return ne W userFactory ($ properties) ;}} class clientUser {static public function getClient ($ properties) {ffac = new FactoryMan; $ man = $ fac-> createOpen ($ properties ); echo $ man-> getUser ()-> getUsername () ;}$ employers = [['username' => 'jack', 'gender' => 'male ', 'job' => 'CODER'], ['username' => 'marry', 'gender' => 'female ', 'job' => 'designer '],]; $ user = clientUser: getClient ($ employers [0]);?>

If any error occurs, please correct it.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.