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 "architecture astronauts", what are they? Why?
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 "architecture astronauts", what are they? Why are they very useful in PHP Exploitation programs? This article describes these titles.
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 practice, Gamma and others have invented some fixed design patterns, just like architects designing houses and buildings, which can be considered as the status of the bathroom or the structural method of the kitchen development template. Using these templates or design patterns means faster building design. The same concept is also applied to software.
The design pattern not only represents a useful way to develop tough software faster, but also provides a method to encapsulate large concepts in friendly terms. For example, you can say that you are writing a loosely coupled message transmission system, or you are writing a pattern named observer.
It is very difficult to use a small example to show the value of the model. This is often a little practical, because the model is actually used in large code libraries. This article does not show large exploitation programs, so you need to consider how to use the example principle in your own large exploitation programs-not the code demonstrated in this article. This does not mean that you should not apply the pattern in a small application. Many good exploitation programs are developed from small exploitation programs to large exploitation programs, so there is no reason not to base on such solid coding practices. Now that you know the design patterns and their usefulness, let's take a look at the five common patterns of PHP V5.
Factory model
In the design patterns book, many design patterns encourage the use of loose coupling. To understand this concept, we 'd better talk about the difficult process of many developers engaged in large-scale systems. When you change a code snippet, a title is generated, and other parts of the system, which you once thought are completely irrelevant, may also render cascading corruption.
This title is tightly coupled. Functions and classes in a part of the system are heavily attached to the actions and structures 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 expect to closely bind them together to avoid rendering interlocking. In large systems, many codes are attached to a few key categories. It may be difficult to change these classes. For example, assume that you have a User class to read from a file. You may want to change it to other classes read from the database, but all the code references the original class read from the file. At this time, the application factory model will be very convenient.
A factory model is a type that provides methods for creating objects for you. You can create an object for the application factory class without directly applying new. In this way, if you want to change the created object type, you only need to change the factory. All codes applied to the factory are automatically changed.
Listing 1 shows an indication column of the factory class. The server side of the equation contains two parts: a database and a set of PHP pages, which promise you to add feedback, request feedback lists, and get articles related to specific feedback.
Listing 1. Factory1.php
<? Php Inte *** ce 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 (). "\ n "); ?> |
The IUser interface defines what control the user object should perform. The implementation of IUser is called User, and the UserFactory class creates the IUser object. This relationship can be expressed in UML in figure 1.
Figure 1. factory class and related IUser interface and user class |