Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What design pattern does laravel use?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces what design pattern laravel uses, which can be used for reference by interested friends. I hope you can learn a lot after reading this article. Let's take a look at it with you.

Design patterns used in laravel: factory pattern, singleton pattern, registration tree pattern, adapter pattern, policy pattern, data object mapping pattern, observer pattern, prototype pattern, decorator pattern, iterator pattern, proxy pattern and so on.

The operating environment of this tutorial: windows7 system, Laravel6 version, Dell G3 computer.

This article introduces you to some common design patterns in Laravel, which you use all the time, maybe you don't know.

1: factory mode

For example: Auth::user ()

Here the Auth class is the method in the factory, and Auth is the alias in the registration tree.

Benefits:

Similar to the encapsulation of functions, objects have a unified generation (instantiation) entry. When the class name of the class corresponding to our object changes, we only need to change the instantiation method in the factory class.

2: singleton mode

Benefits:

Objects cannot be instantiated externally and can only be instantiated once, saving resources.

Implementation method:

Private static $ins = null / / set the private property private function _ _ construct () {} / / so that the external cannot new this class public static function getIns () {/ / expose to the external calling method if ( Self::$ins instanceof self) {return self::$ins } else {self::$ins = new self (); return self::$ins;}}

Declare a private or protected static variable of a class, declare the constructor private (external new operations are not allowed), instantiate it if it does not exist, then return, or directly return if it exists.

3: registration tree mode

Use:

The aliases array in config/app is a registration tree.

Benefits:

The registration tree mode uses the array structure to access the object. The factory method only needs to be called once (it can be placed in a place such as the initialization of the system environment), and it can be taken out directly from the registration tree when the object is called later. There is no need to call factory methods and singleton patterns.

Implementation method:

Class Register {protected static $objects function set ($alias,$object) {/ / Map objects to the global tree self::$objects [$alias] = $object;} static function get ($name) {/ / get object return self::$objects [$name] } function _ unset ($alias) {/ / remove object unset from the global tree (self::$onjects [$alias]);}}

$alias means alias. Set it yourself.

Add in factory mode

Register::set ('db1',$db)

To call anywhere else, just call the registry to read it.

Register::$objects ['db1']

4: adapter mode

Different function interfaces of different tools are encapsulated into a unified API, which is convenient to call. Such as: mysql,mysqli,PDO.

Implementation: declare a unified method body in the interface class, let different classes implement the interface, and override its abstract methods.

Interface Database {function connect ($host,$user,$password,$dbname); function query ($sql); function close ();}

Then use different tool functions to implement the same interface.

5: policy mode

Benefits:

Encapsulate a group of specific behaviors and algorithms into classes to adapt to some specific context, separate logical judgment from concrete implementation, realize hard coding to decoupling, and realize IOC, dependency inversion and inversion control.

Achieve:

1. Define a policy interface file (UserStrategy.php), define the policy interface, declare the policy

two。 Define concrete classes (FemaleUserStrategy.php,MaleUserStrategy.php), implement policy interfaces, and override policy methods

Class Page {protected $strategy; function index () {if ($request- > get ('female')) {$strategy=new FemaleUserStrategy ();} else {$strategy=new MaleUserStrategy ();} $this- > strategy- > method () } public function _ construct (UserStrategy $strategy) {$this- > strategy=$strategy;}}

6: data object mapping mode

Benefit: mapping objects to datastores, operations on an object are mapped to operations on datastores, which is also the implementation mechanism of ORM.

Class Model {public $id; public $name; public $email;. Function _ _ construct ($id) {/ / constructor, which is automatically executed when class is called and is used for initialization. / / query logic} function _ _ destruct () {/ / destructor, which is automatically executed when the class call is completed, and is used to destroy the instance and release resources. / / add, delete and modify logic}}

7: observer mode

Use:

Trigger class Event

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report