原文连接:learnku.com/laravel/t/4…
讨论请前往专业的 Laravel 开发者论坛:learnku.com/Laravelphp
设计模式对每一个开发人员都很重要。它解决了您构建的每一个项目中很是常见的问题。laravel
它能够帮助您在一个对象上添加额外的行为,而又不影响同一类中的其余对象。数据库
装饰器模式是一种设计模式,它容许动态地将行为添加到单个对象,而不会影响同一类中其余对象的行为设计模式
假设咱们有一个Post模型缓存
class Post extends Model
{
public function scopePublished($query) {
return $query->where('published_at', '<=', 'NOW()');
}
}
复制代码
在咱们的PostsController中,咱们有以下的index方法bash
class PostsController extends Controller
{
public function index() {
$posts = Post::published()->get();
return $posts;
}
}
复制代码
为了缓存帖子并避免每次咱们须要列出帖子时都查询数据库,咱们能够执行如下操做app
class PostsController extends Controller
{
public function index() {
$minutes = 1440; # 1 day
$posts = Cache::remember('posts', $minutes, function () {
return Post::published()->get();
});
return $posts;
}
}
复制代码
如今,咱们将帖子缓存1天。但看看代码,控制器了解了太多。它知道咱们缓存了多少天,它本身缓存了对象。ide
一样,假设您正在为HomePageController的Tag,Category,Archives实现相同的功能。阅读和维护的代码太多了。post
在大多数状况下,仓库模式是链接到装饰器模式。学习
首先,让咱们使用仓库模式分离获取帖子的方式,建立具备如下内容的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
中更改绑定便可。无需额外更改。
但愿您喜欢阅读本文。它向您展现了强大的设计模式,以及如何使您的项目易于维护和管理
原文连接:learnku.com/laravel/t/4…
讨论请前往专业的 Laravel 开发者论坛:learnku.com/Larav