The PHP design pattern has the following
Memo Mode (Memento mode)
Observer mode (Observer mode)
Template approach mode (Template method mode)
Command mode (commands mode)
Synthetic mode (composite mode)
Enjoy meta mode (Flyweight mode)
Policy mode (strategy mode)
Status mode (state mode)
Adapter mode (Adapter mode)
Factory approach Mode (factory method mode)
Prototype mode (prototype mode)
Facade mode (facade mode)
Single case mode (singleton mode)
Bridge mode (Bridges mode)
Adorner mode (decorator mode)
Abstract Factory mode (in abstract Factory mode)
Builder mode (builder mode)
Visitor Mode (visitor mode)
When you are constantly trying to find new features in your application, do you find that the solution you are proposing is similar to something you've already achieved before? If you're a programmer (even if you start a very short time), you may be able to answer "yes". It looks like you've used some of the old code to solve the new problems in the software development process. You may have realized that your solution is a basic principle, a method that is not only you but also widely used by all professional developers.
In fact, many procedural problems have been repeatedly encountered, and many of the basic methods (or design patterns) that are used to solve these problems have surfaced. Design patterns are a template that teaches you how to organize your code using a truly reliable design.
Design Pattern History
The term "design pattern" was originally designed to be used in the field of architecture. In his 1977 book "A Pattern Language:towns/building/construction", Christopher Alexander describes some common architectural design problems and explains how to use them, A well-known pattern set to start a new and effective design. Alexander's point of view is well translated into software development, and long-term use of existing components to construct new solutions.
All design patterns have some common features: an identifier (a name), a problem statement (a problem statement), and a solution (a solution).
1. The identification of a design pattern is important because it allows other programmers to immediately understand the purpose of your code (at least through this identification programmer will be familiar with this pattern) without having to study too deeply.
2, the problem description is used to illustrate the application of this model of the field.
3, the solution describes the implementation of this model. The discussion of a good design pattern should cover the advantages and disadvantages of using this model.
A pattern is an effective way to solve a specific problem. A design pattern is not a library (a code base that can be included and used directly in your project) but rather a template for organizing your code. In fact, a code base and a design pattern can be applied in a number of different ways.
For example, a shirt you buy from a store is a code base whose color, style and size are determined by the designer and manufacturer, but it meets your needs. However, if there are no clothes for you in the store, you can create your own shirt (design its shape, choose the fabric, then tailor it together). But if you are not a tailor, you may find it easy to find a suitable model and then follow the pattern to design your own shirt. Using a model, you can get a expertly designed shirt in less time.
Back to the discussion software, a data extraction layer or a CMS (Content management System) is a library that was previously designed and encoded, and it is a good choice if it can accurately meet your needs.
Factory mode
Factory mode (Factory) allows you to instantiate objects as code executes. It is called the factory model because it is responsible for the "production" object. The parameter of the factory method is the corresponding class name of the object you want to generate.
The code is as follows |
Copy Code |
Example #1 Call factory method (with parameters) <?php Class Example { The Parameterized factory method public static function factory ($type) { if (include_once ' drivers/'. $type. '. php ') { $classname = ' Driver_ '. $type; return new $classname; } else { throw new Exception (' Driver not found '); } } } ?> The drivers can be loaded dynamically by the way above. If the example class is a database abstract class, then you can generate MySQL and SQLite-driven objects like this: <?php Load a MySQL Driver $mysql = example::factory (' mysql '); Load a SQLite Driver $sqlite = example::factory (' SQLite '); ?> Single case The single case pattern (Singleton) is used to generate a unique object for a class. The most common place is the database connection. After an object is built using a single case pattern, the object can be used by many other objects. Example #2 Single case mode <?php Class Example { Save class instance in this property private static $instance;
Constructs a method declaration as private to prevent the object from being created directly Private Function __construct () { Echo ' I am constructed '; } Singleton method public static function Singleton () { if (!isset (self:: $instance)) { $c = __class__; Self:: $instance = new $c; } Return self:: $instance; }
Common methods in the example class Public Function bark () { Echo ' woof! '; } Prevent users from replicating object instances Public Function __clone () { Trigger_error (' Clone is not allowed. ', e_user_error); } } ?> So we can get a unique object for the example class. <?php This is an error, because the construction method is declared private $test = new Example; The following will get the example class's single Instance object $test = Example::singleton (); $test->bark (); Copying an object will result in a e_user_error. $test _clone = Clone $test; ?> |
One last thought: Like a tailor's model, a design is of no use in itself. After all, you can't wear a costume model, it's just a piece of paper that's thin. Similarly, a software design model is only a guide. It must be specifically designed to suit the characteristics and requirements of the programming language and your application.