国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

PHP設計模式篇:選擇適用于當前項目類型和規模的最佳模式

瀏覽:2日期:2022-09-16 11:00:23

在代碼和項目中使用常見模式是有好處的,可以讓代碼更易于管理,同時也便于其他開發者理解你的項目。

如果你的項目使用了框架,那么在代碼和項目結構上,都會遵循框架的約束,自然也就繼承了框架中的各種模式, 這時你所需要考慮的是讓上層代碼也能夠遵循最合適的模式。反之,如果沒有使用框架,那么就需要你自己選擇 適用于當前項目類型和規模的最佳模式了。

Architectural pattern on WikipediaSoftware design pattern on WikipediaFactory

One of the most commonly used design patterns is the factory pattern. In this pattern, a class simply creates the object you want to use. Consider the following example of the factory pattern:

<?phpclass Automobile{ private $vehicle_make; private $vehicle_model; public function __construct($make, $model) {$this->vehicle_make = $make;$this->vehicle_model = $model; } public function get_make_and_model() {return $this->vehicle_make . ’ ’ . $this->vehicle_model; }}class AutomobileFactory{ public static function create($make, $model) {return new Automobile($make, $model); }}// have the factory create the Automobile object$veyron = AutomobileFactory::create(’Bugatti’, ’Veyron’);print_r($veyron->get_make_and_model()); // outputs 'Bugatti Veyron'

This code uses a factory to create the Automobile object. There are two possible benefits to building your code this way, the first is that if you need to change, rename, or replace the Automobile class later on you can do so and you will only have to modify the code in the factory, instead of every place in your project that uses the Automobile class. The second possible benefit is that if creating the object is a complicated job you can do all of the work in the factory, instead of repeating it every time you want to create a new instance.

Using the factory pattern isn’t always necessary (or wise). The example code used here is so simple that a factory would simply be adding unneeded complexity. However if you are making a fairly large or complex project you may save yourself a lot of trouble down the road by using factories.

Factory pattern on WikipediaSingleton

When designing web applications, it often makes sense conceptually and architecturally to allow access to one and only one instance of a particular class. The singleton pattern enables us to do this.

<?php class Singleton{ static $instance; private function __construct() { } public static function getInstance() {if (!isset(self::$instance)) { self::$instance = new self();}return self::$instance; }}$instance1 = Singleton::getInstance();$instance2 = Singleton::getInstance();echo $instance1 === $instance2; // outputs 1

The code above implements the singleton pattern using a statically scoped variable and the?getInstance()?method. Note that the constructor is declared as private to prevent instantiation outside of the class via?new?keyword.

The singleton pattern is useful when we need to make sure we only have a single instance of a class for the entire request lifecycle in a web application. This typically occurs when we have global objects (such as a Configuration class) or a shared resource (such as an event queue).

You should be wary when using the singleton pattern, as by its very nature it introduces global state into your application, reducing testability. In most cases, dependency injection can (and should) be used in place of a singleton class. Using dependency injection means that we do not introduce unnecessary coupling into the design of our application, as the object using the shared or global resource requires no knowledge of a concretely defined class.

Singleton pattern on WikipediaFront Controller

The front controller pattern is where you have a single entrance point for you web application (e.g. index.php) that handles all of the requests. This code is responsible for loading all of the dependencies, processing the request and sending the response to the browser. The front controller pattern can be beneficial because it encourages modular code and gives you a central place to hook in code that should be run for every request (such as input sanitization).

Front Controller pattern on WikipediaModel-View-Controller

The model-view-controller (MVC) pattern and its relatives HMVC and MVVM let you break up code into logical objects that serve very specific purposes. Models serve as a data access layer where data it fetched and returned in formats usable throughout your application. Controllers handle the request, process the data returned from models and load views to send in the response. And views are display templates (markup, xml, etc) that are sent in the response to the web browser.

MVC is the most common architectural pattern used in the popular?PHP frameworks.

Learn more about MVC and its relatives:

MVCHMVCMVVM
標簽: PHP
相關文章:
主站蜘蛛池模板: 国产福利不卡一区二区三区 | 国内视频一区二区 | 国产精品免费久久 | 毛片手机在线视频免费观看 | 亚洲三级网站 | 91小视频在线观看免费版高清 | 亚洲精品第一区二区在线 | 日本不卡在线一区二区三区视频 | 国产精品一区二区久久精品涩爱 | 欧美精品在线视频观看 | 大量真实偷拍情侣视频野战 | 99精品视频在线观看免费 | 国产在线视频一区 | 久久一区二区三区不卡 | 国产精品成人aaaaa网站 | 波多野吉衣 免费一区 | 一级毛片在线免费视频 | 边接电话边做国语高清对白 | 91年精品国产福利线观看久久 | 成年人网站在线观看免费 | 成 人 a v黄 色 | 一级国产交换配乱淫 | 2022国内精品免费福利视频 | 成人午夜天 | 99国产精品视频久久久久 | 91国内精品久久久久影院优播 | 国产精品性视频免费播放 | 男子操女子 | 国内自拍小视频 | 日本精品高清一区二区2021 | 美女视频很黄很a免费国产 美女视频黄.免费网址 | a毛片免费在线观看 | 精品亚洲视频在线观看 | 99久久99久久久99精品齐 | 一级片免费观看视频 | 中文字幕毛片 | 男人女人真曰批视频播放 | 欧美久久精品 | 久久国产亚洲欧美日韩精品 | 亚洲久久视频 | 一级中国乱子伦视频 |