Do you know PHP design Patterns _php Tutorial

Source: Internet
Author: User
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

 
 
  1. PHP
  2. Interface Iuser
  3. {
  4. function GetName ();
  5. }
  6. Class User implements Iuser
  7. {
  8. Public function __construct ($id) {}
  9. Public Function GetName ()
  10. {
  11. return "Jack";
  12. }
  13. }
  14. Class Userfactory
  15. {
  16. public static function Create ($id)
  17. {
  18. return new User ($id);
  19. }
  20. }
  21. $ UO = userfactory :: Create (1);
  22. Echo ($uo->getName (). " n ");
  23. ?>

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 ...

  • 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.