Design patterns give me a lot of benefits, there are more than 20 kinds of design patterns in Java, and there are five common design patterns in PHP, let's take a closer look at the factory pattern in PHP design mode. PHP design mode introduces design patterns to the software community, which is written by Erich Gamma, Richard Helm, Ralph Johnson, and John vlissides designs (commonly known as "Gang of Four"). The core concepts behind the design patterns presented are very simple.
After years of software development practice, Gamma and others have discovered patterns of fixed design, like architects designing houses and buildings, to develop templates for the location of a bathroom or the way the kitchen is constructed. Using these templates, or design patterns, means that you can design better buildings faster. The same concept applies to software. Design patterns represent not only a useful way to develop robust software faster, but also a way to encapsulate large ideas in friendly terms. For example, you can say that you are writing a message delivery system that provides loosely coupled, or that you are writing a pattern named Observer.
It is very difficult to demonstrate the value of a pattern with a smaller example. This is often a bit of an overkill, because the pattern actually works in a large code base. This article does not show large applications, so what you need to think about is how to apply the principle of the example in your own large application-not the code that this article demonstrates. This is not to say that you should not use patterns in small applications. Many good applications start with small applications and evolve to large applications, so there is no reason not to build on such solid coding practices.
Now that you understand PHP design patterns and their usefulness, let's look at five common patterns of PHP V5.
Factory mode
In the first book of design patterns, many design patterns encourage the use of loose coupling. To understand this concept, let's talk about the arduous journey of many developers working on large systems. A problem occurs when you change a code fragment, and other parts of the system--the ones you thought were completely unrelated--might also have cascading corruption.
The problem is tight coupling. Functions and classes in a 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 patterns that enable these classes to communicate with each other, but do not want to tightly bind them together to avoid interlocking.
In large systems, many of the code relies on a few key classes. There may be difficulties when you need to change these classes. For example, suppose you have a User class that reads from a file. You want to change it to another class read from the database, but all code references the original class that was read from the file. At this point, it is convenient to use the Factory mode.
A factory pattern is a class that has some methods for creating objects for you. You can use the factory class to create objects without using new directly. This way, if you want to change the type of object you are creating, you can simply change the factory. All code that uses the factory is automatically changed.
The manifest shows an example of a factory class. The server side of the equation consists of two parts: a database and a set of PHP pages that allow you to add feedback, request a feedback list, and get articles related to specific feedback.
Listing. factory1.php
- PHP
- 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 (). " n ");
- ?>
http://www.bkjia.com/PHPjc/446468.html www.bkjia.com true http://www.bkjia.com/PHPjc/446468.html techarticle Design Patterns give me a lot of benefits, there are more than 20 design patterns in Java, and there are five common design patterns in PHP, below we will look at the details of the PHP design mode of the factory ...