weixin_39844590 2020-11-22 01:42
浏览 0

Implement Reacter & Reactant Facades

Since package API is strict typed its might be harder to use and couple your application with the package subsystems. Developers will pass to the reactTo method reactable instance and string value of reaction type name: ->reactTo($comment, 'Like')

Facade design pattern to the rescue. Not the same as Laravel Facades (which act as "static proxies").

These facades will replace experimental global Cog\Laravel\Love\Facades\Love facade.

Facade concept

Facade shields developers from the complex details of the system and provides them with a simplified view of it which is easy to use. It also decouples the code that uses the system from the details of the subsystems, making it easier to modify the system later. It decreases maintenance cost, but adds additional layer of code & decreases runtime performance.

Reacter Facade

User via Reacterable trait will have additional method which will provide simpler access to Reacter model methods. This method returns Facades\Reacter class which hide internal complexity behind a single interface that appears simple from the outside. Method will have via (by way of) prefix.

php
trait Reacterable
{
    public function viaLoveReacter(): ReacterFacade
    {
        return new ReacterFacade($this->getLoveReacter());
    }
}

Usage

Instead default strict typed Reacter model calls:

php
$reactant = $comment->getLoveReactant();
$reactionType = ReactionType::fromName('Like');

$user->getLoveReacter()->reactTo($reactant, $reactionType);

Facades\Reacter class has simpler interface:

php
$user->viaLoveReacter()->reactTo($comment, 'Like');

Under the hood it will resolve Reactant model from $comment & ReactionType from the string name and call strict typed method.

Reactant Facade

Comment via Reactable trait will have additional method which will provide simpler access to Reactant model methods. This method returns Facades\Reactant class which hide internal complexity behind a single interface that appears simple from the outside. Method will have via (by way of) prefix.

php
trait Reactable
{
    public function viaLoveReactant(): ReactantFacade
    {
        return new ReactantFacade($this->getLoveReactant());
    }
}

Usage

Instead default strict typed Reactant model calls:

php
$reacter = $user->getLoveReacter();
$reactionType = ReactionType::fromName('Like');

$comment->getLoveReactant()->isReactedByWithType($reacter, $reactionType);

Facades\Reactant class has simpler interface:

php
$comment->viaLoveReactant()->isReactedBy($user, 'Like');

Under the hood it will resolve Reacter model from $user & ReactionType from the string name and call strict typed method.

Naming

Facades\Reacter & Facades\Reactant instantiation methods name candidate variants described in comments below.

该提问来源于开源项目:cybercog/laravel-love

  • 写回答

5条回答 默认 最新

  • weixin_39844590 2020-11-22 01:42
    关注

    Name Variant №1 actAs prefix

    php
    $user->actAsLoveReacter()->reactTo($article, 'Like');
    

    Pros

    • Very informative

    Cons

    • act prefix is redundant because not all the methods will be actions. For example: $user->actAsLoveReacter()->isNull()

    Decision

    Don't use it.

    评论

报告相同问题?