An in-depth analysis of appearance patterns in the Laravel framework
laravelThe facade pattern (Facade Pattern) in the framework means that external communication with a subsystem must be carried out through a unified facade object to provide a consistent interface for a set of interfaces in the subsystem. The facade pattern defines a high-level interface that makes this subsystem easier to use. Appearance mode is also called facade mode, which is an object structure mode.
The Facades Route
, Redis
, and Auth
that we commonly use in Laravel are the specific implementations of the appearance pattern. Many of them are designed in Laravel. Each appearance class inherits from a unified abstract appearance class. The abstract appearance class provides basic methods for accessing the subsystem behind it through the appearance class.
For new business needs, do not modify the original appearance class, but add a new specific appearance class. The new specific appearance class is associated with the new subsystem object, and at the same time, it is achieved by modifying the configuration file. The purpose is not to modify the source code and replace the appearance class.
The following is an example of a simple appearance pattern, which does not introduce an abstract appearance class. In the article introducing Laravel Facade, we will see that Laravel provides an abstract appearance class so that we can easily customize it according to our needs. Add the appearance class of the new subsystem, and enable the appearance class to correctly proxy to its corresponding subsystem (or service).
Mode structure
The appearance mode contains the following roles:
Facade appearance role
-
SubSystem Subsystem Role
#Code Example1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
class
Client
{
public
function
main()
{
(
new
Facade)->operation();
}
}
class
Facade
{
private
$systemA
;
private
$systemB
;
public
function
__construct()
{
$this
->systemA =
new
SystemA;
$this
->systemB =
new
SystemB;
}
public
function
operation()
{
$this
->systemA->operationA();
$this
->systemB->operationB();
}
}
class
SystemA
{
public
function
operationA()
{
//
}
}
class
SystemB
{
public
function
operationB()
{
//
}
}
Copy after login
Pattern AnalysisAccording to the "single responsibility principle", dividing a system into several subsystems in software is beneficial to reducing the complexity of the entire system. A common design goal is to enable communication and interdependence between subsystems Achieve minimum, and one way to achieve this goal is to introduce a facade object, which provides a simple and single entry point for access to the subsystem. -Appearance mode is also a manifestation of "Demeter's Law". By introducing a new appearance class, the complexity of the original system can be reduced, and the coupling between the client class and the subsystem class can be reduced. - The appearance pattern requires that communication between the outside of a subsystem and its interior is carried out through a unified appearance object. The appearance class separates the client from the internal complexity of the subsystem, so that the client only needs to deal with the appearance object without Work with many objects within the subsystem. -The purpose of appearance mode is to reduce the complexity of the system. -The appearance mode greatly improves the convenience of client use, so that the client does not need to care about the working details of the subsystem and can call related functions through the appearance role. DisadvantagesDisadvantages of the appearance mode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
- It cannot restrict customers' use of subsystem classes very well. If you do too much for customers to access subsystem classes Limitations reduce variability and flexibility.
- Without introducing an abstract appearance class, adding a new subsystem may require modifying the source code of the appearance class or client, which violates the "opening and closing principle".
- A system has multiple appearance classesIn appearance mode, usually only one appearance class is needed , and this appearance class has only one instance, in other words it is a singleton class. In many cases, in order to save system resources, appearance classes are generally designed as singleton classes. Of course, this does not mean that there can only be one appearance class in the entire system. Multiple appearance classes can be designed in a system. Each appearance class is responsible for interacting with some specific subsystems and providing corresponding business functions to users.
- Do not try to add new behaviors to the subsystem through appearance classes. Do not add new behaviors to the subsystem by inheriting an appearance class. This approach is wrong. . The purpose of the appearance pattern is to provide a centralized and simplified communication channel for subsystems, rather than adding new behaviors to the subsystem. The addition of new behaviors should be achieved by modifying the original subsystem class or adding a new subsystem class. , cannot be implemented through appearance classes.
- Introduction of abstract appearance classesThe biggest disadvantage of appearance mode is that it violates the "opening and closing principle", which is required when adding new subsystems or removing subsystems Modifying the appearance class can solve this problem to a certain extent by introducing abstract appearance classes, and the client programs for the abstract appearance classes. For new business needs, the original appearance class is not modified, but a new specific appearance class is added. The new specific appearance class is associated with the new subsystem object. At the same time, the configuration file is modified to achieve the purpose of not modifying the source code and replacing it. Appearance class purpose.
Summary
In appearance mode, external communication with a subsystem must be carried out through a unified appearance object, which is a group of subsystems. The interface provides a consistent interface, and the facade pattern defines a high-level interface that makes this subsystem easier to use. Appearance mode is also called facade mode, which is an object structure mode.
The appearance mode contains two roles: the appearance role is a role that is directly called on the client. In the appearance role, you can know the functions and responsibilities of the relevant (one or more) subsystems. , it delegates all requests from the client to the corresponding subsystem and passes them to the corresponding subsystem object for processing; there can be one or more subsystem roles in the software system at the same time, and each subsystem may not be a separate A class, but a collection of classes that implements the functions of a subsystem.
The appearance pattern requires that communication between the outside of a subsystem and its interior is carried out through a unified appearance object. The appearance class separates the client from the internal complexity of the subsystem, making the client The end only needs to deal with appearance objects, and does not need to deal with many objects inside the subsystem.
The main advantage of appearance mode is to shield subsystem components from customers, reduce the number of objects processed by customers and make the subsystem easier to use. It realizes the communication between subsystem and customers. It loosely couples the relationship, reduces compilation dependencies in large software systems, and simplifies the transplantation process of systems between different platforms; its disadvantage is that it cannot well restrict customers' use of subsystem classes, and it does not introduce abstract appearance classes. In some cases, adding a new subsystem may require modifying the appearance class or client source code, violating the "open-close principle".
Applicable situations of the appearance pattern include: providing a simple interface for a complex subsystem; there is a large dependency between the client program and multiple subsystems; in a hierarchical structure, The entrance to each layer in the system needs to be defined so that there is no direct connection between layers.
The above is the entire content of this article, please pay attention to laravel framework introductory tutorial for more information.
Recommended related articles:
Parsing of middleware source code based on laravel5.2
Laravel local environment construction: Homestead Deployment of development environment
Related course recommendations:
The latest five Laravel video tutorial recommendations in 2017
The above is the detailed content of An in-depth analysis of appearance patterns in the Laravel framework. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.
