Do you know the PHP design mode?

Source: Internet
Author: User
The design pattern brings many benefits to me. there are more than 20 design patterns in JAVA, and there are also five common design patterns in PHP, let's take a detailed look at the factory model in the PHP design mode. The PHP Design Patterns book introduces the design patterns into the software community. The author of this book is Er

The design pattern brings many benefits to me. there are more than 20 design patterns in JAVA, and there are also five common design patterns in PHP, let's take a detailed look at the factory model in the PHP design mode. The PHP Design Patterns book introduces Design patterns to the software community by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Design (commonly known as the "Gang of Four "). The core concept behind the design pattern is very simple.

After years of software development practices, Gamma and others have discovered some patterns of fixed design. just like architects designing houses and buildings, they can develop templates for bathroom locations or kitchen structures. Using these templates or design patterns means faster building design. The same concept applies to software. The design pattern not only represents a useful way to develop robust software faster, but also provides a method to encapsulate large concepts in friendly terms. For example, you can say that you are writing a message transmission system that provides loose coupling, or you are writing a mode named observer.

 

It is very difficult to use small examples to demonstrate the value of the model. This is often a little practical, because the model actually plays a role in large code libraries. This article does not show large applications, so you need to think about how to apply the sample principles in your own large applications-rather than the code demonstrated in this article. This does not mean that you should not use the mode in a small application. Many good applications are developed from small applications to large applications, so there is no reason not to base on such solid coding practices.

Now that you have learned about the PHP design patterns and their usefulness, let's take a look at the five common patterns of PHP V5.

Factory model

Many design patterns encourage loose coupling in the design patterns book. To understand this concept, we 'd better talk about the hard work of many developers engaged in large-scale systems. A problem occurs when you change a code snippet. other parts of the system, which you once thought were completely unrelated, may also cause cascading damages.

This problem lies in tight coupling. Functions and classes in a certain part of the system depend heavily on the behavior and structure of functions and classes in other parts of the system. You need a set of modes to enable these classes to communicate with each other, but do not want to closely bind them together to avoid interlocking.

In large systems, many codes depend on a few key classes. It may be difficult to change these classes. For example, assume that you have a User class to read from a file. You want to change it to other classes read from the database, but all code references the original class read from the file. At this time, it is very convenient to use the factory model.

Factory mode is a type that provides methods for creating objects for you. You can use the factory class to create objects instead of using new directly. In this way, if you want to change the type of the created object, you only need to change the factory. All codes used in the factory are automatically changed.

The list shows an indication column of the factory class. The server side of the equation consists of a database and a set of PHP pages that allow you to add feedback, request feedback lists, and obtain articles related to specific feedback.

List. Factory1.php

 
 
  1.  
  2. interface IUser  
  3. {  
  4. function getName();  
  5. }  
  6.  
  7. class User implements IUser  
  8. {  
  9. public function __construct( $id ) { }  
  10.  
  11. public function getName()  
  12. {  
  13. return "Jack";  
  14. }  
  15. }  
  16.  
  17. class UserFactory  
  18. {  
  19. public static function Create( $id )  
  20. {  
  21. return new User( $id );  
  22. }  
  23. }  
  24.  
  25. $uo = UserFactory::Create( 1 );  
  26. echo( $uo->getName()."\n" );  
  27. ?> 

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.