Php object-oriented development-Abstract factory model

Source: Internet
Author: User
Abstract Factory mode is an abstraction of the factory mode. In general, it is to separate the structure of the factory mode into an individual that can run independently. Abstract factory modelYes Factory modelIn general, it is to separate the structure of the factory model into an individual that can run independently.

Let's take the example in the factory model for illustration:

Now there is a car factory that produces cars and buses. cars and buses are composed of engines, bodies, and wheels.

In the factory model, we use cars and bus as two types of automobile families. production engines, bodies, and wheels are fixed structures for producing cars, as shown in:

The actual deployment is:

// Standard interface engineNorms of the production engine {function engine ();} class carEngine implements engineNorms {public function engine () {return 'vehicle engine ';}} class busEngine implements engineNorms {public function engine () {return 'bus engine '; }}// standard interface bodyNorms {function body ();} class carBody implements bodyNorms {public function body () {return 'vehicle body';} class busBody implements bodyNorms {public function body () {return 'bus body ';}} // standard interface whellNorms {function whell ();} class carWhell implements whellNorms {public function whell () {return 'vehicle wheel ';}} class busWhell implements whellNorms {public function whell () {return 'bus wheel ';}}

Continue to abstract the factory, abstract the automobile factory and bus factory, and associate each factory with each component,

The actual deployment is:

The actual deployment is:

// Standard interface engineNorms of the production engine {function engine ();} class carEngine implements engineNorms {public function engine () {return 'vehicle engine ';}} class busEngine implements engineNorms {public function engine () {return 'bus engine '; }}// standard interface bodyNorms {function body ();} class carBody implements bodyNorms {public function body () {return 'vehicle body';} class busBody implements bodyNorms {public function body () {return 'bus body ';}} // standard interface whellNorms {function whell ();} class carWhell implements whellNorms {public function whell () {return 'vehicle wheel ';}} class busWhell implements whellNorms {public function whell () {return 'bus wheel ';}}

// Factory standard
Interface factory {
Static public function getInstance ($ type );
}

// Automobile factory
Class carFactory implements factory {

Static public function getInstance ($ type ){
$ Instance = '';
Switch ($ type ){
Case 'engine ':
$ Instance = new carEngine ();
Break;
Case 'body ':
$ Instance = new carBody ();
Break;
Case 'whell ':
$ Instance = new carWhell ();
Break;
Default:
Throw new Exception ('the car factory cannot produce this product ');
}
Return $ instance;
}

}

// Bus Factory
Class busFactory implements factory {

Static public function getInstance ($ type ){
$ Instance = '';
Switch ($ type ){
Case 'engine ':
$ Instance = new busEngine ();
Break;
Case 'body ':
$ Instance = new busBody ();
Break;
Case 'whell ':
$ Instance = new busWhell ();
Break;
Default:
Throw new Exception ('The bus factory cannot produce this product ');
}
Return $ instance;
}

}

$ Car ['engine'] = carFactory: getInstance ('engine')-> engine ();
$ Car ['body'] = carFactory: getInstance ('body')-> body ();
$ Car ['whell '] = carFactory: getInstance ('whell')-> whell ();
Print_r ($ car );

$ Bus ['engine'] = busFactory: getInstance ('engine')-> engine ();
$ Bus ['body'] = busFactory: getInstance ('body')-> body ();
$ Bus ['whell '] = busFactory: getInstance ('whell')-> whell ();
Print_r ($ bus );

The abstract factory model abstracts the factory model to make the new structure more flexible. For example, if the production body requires a spray paint action, in the factory mode, we need to change the overall structure, and in the abstract factory, we only need to change the production body.

The abstract factory model also has the disadvantages that the factory model has high requirements on the structure. the expansion or streamlining of the overall structure will become more complicated. Therefore, when the abstract factory model is used, the classification of the hierarchical structure is very important.

The above is the content of php object-oriented development-the abstract factory model. For more information, see PHP Chinese network (www.php1.cn )!

Related articles:

Detailed description of code for implementing the abstract Factory mode in Java

Abstract Factory pattern of JAVA design pattern

Comparison of PHP simple factory mode, Factory method mode, and abstract Factory mode

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.