Home >Backend Development >PHP Tutorial >Do you understand PHP design patterns_PHP tutorial

Do you understand PHP design patterns_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:28:16920browse

Design patterns have brought me a lot of benefits. There are more than 20 design patterns in JAVA, and there are five common design patterns in PHP. Let’s take a detailed look at the PHP design patterns. Factory mode. The book PHP Design Patterns introduced design patterns to the software community. The authors of the book are Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Design (commonly known as the "Gang of Four"). The core concepts behind the design patterns presented are very simple.

After years of software development practice, Gamma et al. discovered certain patterns with fixed designs, just like architects design houses and buildings, which can Develop templates for where a bathroom should be or how a kitchen should be constructed. Using these templates, or design patterns, means designing better buildings faster. The same concept applies to software. Design patterns not only represent a useful way to develop robust software faster, but they also provide a way to encapsulate large ideas in friendly terms. For example, you might say that you are writing a messaging system that provides loose coupling, or you might say that you are writing a pattern named Observer.

Demonstrating the value of patterns with smaller examples is very difficult. This often feels like overkill, since patterns actually work in large code bases. This article does not demonstrate a large application, so you need to think about ways to apply the principles of the example in your own large application—not the code itself demonstrated in this article. This is not to say that you shouldn't use patterns in small applications. Many good applications start out as small applications and progress to large applications, so there's no reason not to build on these types of solid coding practices.

Now that you know about PHP design patterns and how useful they are, let’s take a look at the five commonly used patterns in PHP V5.

Factory Pattern

Originally in the book Design Patterns, many design patterns encourage the use of loose coupling. To understand this concept, it's best to talk about the arduous journey that many developers go through working on large systems. When you change one piece of code, problems can occur, and cascading breaks can occur in other parts of the system—parts you once thought were completely unrelated.

The problem is tight coupling. Functions and classes in one part of the system are heavily dependent on the behavior and structure of functions and classes in other parts of the system. You want a set of patterns that allow these classes to communicate with each other, but you don't want to tie them tightly together to avoid interlocking.

In large systems, a lot of code depends on a few key classes. Difficulties may arise when these classes need to be changed. For example, suppose you have a User class that reads from a file. You want to change it to a different class that reads from the database, however, all your code references the original class that reads from the file. At this time, it will be very convenient to use factory mode.

Factory pattern is a class that has certain methods that create objects for you. You can use a factory class to create objects without using new directly. This way, if you want to change the type of object created, you only need to change the factory. All code using this factory is automatically changed.

The manifest displays an instance of the 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 list of feedback, and get articles related to a specific feedback.

List.Factory1.php

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN>interface IUser  </SPAN><LI class=alt><SPAN>{  </SPAN><LI class=""><SPAN>function getName();  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>class User implements IUser  </SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>public function __construct( $id ) { }  </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>public function getName()  </SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>return "Jack";  </SPAN><LI class=""><SPAN>}  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>class UserFactory  </SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>public static function Create( $id )  </SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>return new User( $id );  </SPAN><LI class=""><SPAN>}  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>uo</SPAN><SPAN> = </SPAN><SPAN class=attribute-value>UserFactory</SPAN><SPAN>::Create( 1 );  </SPAN></SPAN><LI class=""><SPAN>echo( $uo-</SPAN><SPAN class=tag>></span><span>getName()."n" );  </span></span></li>
<li class="alt">
<span></span><span class="tag">?></span><span> </span>
</li>
</ol>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446468.htmlTechArticleDesign patterns have brought me a lot of benefits. There are more than 20 design patterns in JAVA, and in PHP There are also five common design patterns. Let’s take a detailed look at the factory in PHP design patterns...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn