PHP的装饰器模式定义、使用是什么
Admin 2022-12-06 群英技术资讯 951 次浏览
今天这篇我们来学习和了解“PHP的装饰器模式定义、使用是什么”,下文的讲解详细,步骤过程清晰,对大家进一步学习和理解“PHP的装饰器模式定义、使用是什么”有一定的帮助。有这方面学习需要的朋友就继续往下看吧!它可以帮助您在一个对象上添加额外的行为,而又不影响同一类中的其他对象。
装饰器模式是一种设计模式,它允许动态地将行为添加到单个对象,而不会影响同一类中其他对象的行为
假设我们有一个Post模型
class Post extends Model
{
public function scopePublished($query) {
return $query->where('published_at', '<=', 'NOW()');
}
} 登录后复制 在我们的PostsController中,我们有如下的index方法
class PostsController extends Controller
{
public function index() {
$posts = Post::published()->get();
return $posts;
}
} 登录后复制 为了缓存帖子并避免每次我们需要列出帖子时都查询数据库,我们可以执行以下操作
class PostsController extends Controller
{
public function index() {
$minutes = 1440; # 1 day
$posts = Cache::remember('posts', $minutes, function () {
return Post::published()->get();
});
return $posts;
}
} 登录后复制 现在,我们将帖子缓存1天。但看看代码,控制器了解了太多。它知道我们缓存了多少天,它自己缓存了对象。
同样,假设您正在为HomePageController的Tag,Category,Archives实现相同的功能。阅读和维护的代码太多了。
在大多数情况下,仓库模式是连接到装饰器模式。
首先,让我们使用仓库模式分离获取帖子的方式,创建具有以下内容的app/Repositories/Posts/PostsRepositoryInterface.php
namespace App\Repositories\Posts;
interface PostsRepositoryInterface
{
public function get();
public function find(int $id);
} 登录后复制 在同个目录下创建具有下面内容的 PostsRepository
namespace App\Repositories\Posts;
use App\Post;
class PostsRepository implements PostsRepositoryInterface
{
protected $model;
public function __construct(Post $model) {
$this->model = $model;
}
public function get() {
return $this->model->published()->get();
}
public function find(int $id) {
return $this->model->published()->find($id);
}
} 登录后复制 回到PostsController并将更改应用为
namespace App\Http\Controllers;
use App\Repositories\Posts\PostsRepositoryInterface;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function index(PostsRepositoryInterface $postsRepo) {
return $postsRepo->get();
}
} 登录后复制 控制器变得健康,知道足够的细节来完成工作。
在这里,我们依靠 Laravel 的 IOC 注入 Posts 接口的具体对象来获取我们的帖子
我们需要做的就是告诉Laravel的IOC使用接口时要创建哪个类。
在你的 app/Providers/AppServiceProvider.php 添加绑定方法
namespace App\Providers;
use App\Repositories\Posts\PostsRepositoryInterface;
use App\Repositories\Posts\PostsRepository;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(PostsRepositoryInterface::class,PostsRepository::class);
}
} 登录后复制 现在无论何时我们注入PostsRepositoryInterface Laravel 都会创建 PostsRepository 的实例并将其返回。
我们在一开始就说过,装饰器模式允许将行为添加到单个对象,而不会影响同一类中的其他对象。
在这里缓存是行为,对象/类是 PostsRepository。
让我们在 app/Repositories/Posts/PostsCacheRepository.php 中创建具有以下内容的PostsCacheRepository
namespace App\Repositories\Posts;
use App\Post;
use Illuminate\Cache\CacheManager;
class PostsCacheRepository implements PostsRepositoryInterface
{
protected $repo;
protected $cache;
const TTL = 1440; # 1 day
public function __construct(CacheManager $cache, PostsRepository $repo) {
$this->repo = $repo;
$this->cache = $cache;
}
public function get() {
return $this->cache->remember('posts', self::TTL, function () {
return $this->repo->get();
});
}
public function find(int $id) {
return $this->cache->remember('posts.'.$id, self::TTL, function () {
return $this->repo->find($id);
});
}
} 登录后复制 在这个类中,我们接受 Caching 对象和 PostsRepository 对象,然后使用类(装饰器)将缓存行为添加到 PostsReposiory 实例。
我们可以使用相同的示例将HTTP请求发送到某些服务,然后在失败的情况下返回模型。我相信您会从该模式以及它是如何轻松添加行为中受益。
最后一件事是修改 AppServiceProvider 接口绑定以创建 PostsCacheRepository 实例而不是PostsRepository
namespace App\Providers;
use App\Repositories\Posts\PostsRepositoryInterface;
use App\Repositories\Posts\PostsCacheRepository;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(PostsRepositoryInterface::class,PostsCacheRepository::class);
}
} 登录后复制 现在再次检查文件,您会发现它非常易于阅读和维护。同样,它也是可测试的,如果您决定在某个时候删除缓存层。您只需在AppServiceProvider中更改绑定即可。无需额外更改。
希望您喜欢阅读本文。它向您展示了强大的设计模式,以及如何使您的项目易于维护和管理
原文地址:https://dev.to/ahmedash95/design-patterns-in-php-decorator-with-laravel-5hk6
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
PHP预定义接口――Iterator用法示例, 本文实例讲述了PHP预定义接口――Iterator用法。分享给大家供大家参考,具体如下:<br /> Iterator(迭代器)接口<br /> <br /> 可在内部迭代自己的外部迭代器或类的接口。<br /> <br /> 接口摘要
如图这有可能是因为文件的结构问题。很大可能是将.php文件放到了WEB-INF文件下了,因为该文件用户不可访问,所以自然也就接受不到数据。可能是Tomcat的问题。所以在配置php时不要写WEB-INF,如图 所以直接填phpbin就好了,然后在ROOT目录下建立phpbin文件夹,如图 然后重试,即可解决问题。
在学习PHP过程中,大家应该都有接触过时间戳,下面是小编整理的一些Laravel框架中时间戳的使用技巧,觉得挺实用的,因此给大家简单介绍一下,感兴趣的朋友就继续往下看吧。
Swoole Client类用于实现客户端功能,并增加异步非阻塞模式,让用户在客户端也能使用事件循环。作为客户端使用,Swoole Client可以在FPM环境下或 Apache中使用,但不允许使用Async异步模式,只能使用同步非阻塞模式
我们先添加热键 在Live Templates中添加名字叫user的模板,里面建一个比如说叫xxx的子类,然后再下面的Templates text写入;然后点击Edit variables ;然后点击Change
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008