Better construction of five PHP design modes of development templates

Source: Internet
Author: User

The design pattern is only prepared for the Java architect-at least you may always think so. In fact, the design pattern is very useful to everyone. If these tools are not the patents of the "architecture astronauts", what are they? Why are they very useful in php (as the mainstream development language) applications? This article explains these issues.

The 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 "four-person gang "). 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 design patterns and their usefulness, let's take a look at the five common patterns of php (as the mainstream development language) 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.

Listing 1 shows an indication column of the factory class. The server side of the equation includes two parts: a database and a set of php (as the mainstream development language) pages, these pages allow you to add feedback, request feedback lists, and obtain articles related to specific feedback.

Listing 1. Factory1.php (as the mainstream development language)

<? Php (as the mainstream development language)
Interface IUser
{
Function getName ();
}

Class User implements IUser
{
Public function _ construct ($ id ){}

Public function getName ()
{
Return "Jack ";
}
}

Class UserFactory
{
Public static function Create ($ id)
{
Return new User ($ id );
}
}

$ Uo = UserFactory: Create (1 );
Echo ($ uo-> getName ()."");
?>

The IUser interface defines what operations a user object should perform. The implementation of IUser is called User, and the UserFactory class creates an IUser object. This relationship can be expressed in UML in Figure 1.


Figure 1. factory class and related IUser interface and user class

If you use php (as the mainstream development language) interpreter to run this code on the command line, you will get the following results:

% Php (as the mainstream development language) factory1.php (as the mainstream development language)
Jack
%

The test code will request the User object to the factory and output the result of the getName method.

There is a variation in the factory model that uses the factory method. Class.

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.