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).
The appearance mode contains the following roles:
Facade appearance role
SubSystem Subsystem Role
<?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() { // } }
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!