不同的角色查询条件不同用啥设计模式

需求里面总会有根据不同的角色查询条件不是不同的,但是接口一般而言我都会设计同一个,这里其实会根据角色的不同会有不同的查询条件。
1 老师 学生 年级组长
2 老师查询整个班的作业
3 学生查询自己的作业
4 年级组长查询整个年纪的作业
这样有啥比较好的设计模式吗  优雅的写法
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

💡策略模式(Strategy)

现实生活中的例子:

考虑排序的例子,我们实现了冒泡排序,但随着数据的增长,冒泡排序开始变得非常缓慢。为了解决这个问题,我们实现了快速排序。但是现在虽然快速排序算法对大型数据集的效果更好,但对于较小的数据集来说速度非常慢。为了解决这个问题,我们实施了一个策略,对于小型数据集使用冒泡排序,更大规模的使用快速排序。

通俗解释:

策略模式允许您根据情况切换算法或策略。

维基百科:

在计算机编程中,策略模式(也称为政策模式)是一种行为软件设计模式,可以在运行时选择算法的行为。

程序化的例子

从上面翻译我们的例子。首先,我们有策略接口和不同的策略实现

interface SortStrategy
{
    public function sort(array $dataset): array;
}

class BubbleSortStrategy implements SortStrategy
{
    public function sort(array $dataset): array
    {
        echo "Sorting using bubble sort";

        // Do sorting
        return $dataset;
    }
}

class QuickSortStrategy implements SortStrategy
{
    public function sort(array $dataset): array
    {
        echo "Sorting using quick sort";

        // Do sorting
        return $dataset;
    }
}

然后我们的客户将使用任何策略

class Sorter
{
    protected $sorter;

    public function __construct(SortStrategy $sorter)
    {
        $this->sorter = $sorter;
    }

    public function sort(array $dataset): array
    {
        return $this->sorter->sort($dataset);
    }
}

它可以用作

And it can be used as

$dataset = [1, 5, 4, 3, 2, 8];

$sorter = new Sorter(new BubbleSortStrategy());
$sorter->sort($dataset); // Output : Sorting using bubble sort

$sorter = new Sorter(new QuickSortStrategy());
$sorter->sort($dataset); // Output : Sorting using quick sort

摘选自:github.com/guanguans/design-patter...

1年前 评论
周小云 (楼主) 1年前
忆往昔弹指间 (作者) 1年前
忆往昔弹指间 (作者) 1年前
周小云 (楼主) 1年前
周小云 (楼主) 1年前
讨论数量: 23

可以用策略模式,如果更简单得话就switch case 分别调用不同得方法啦

1年前 评论
周小云 (楼主) 1年前

💡策略模式(Strategy)

现实生活中的例子:

考虑排序的例子,我们实现了冒泡排序,但随着数据的增长,冒泡排序开始变得非常缓慢。为了解决这个问题,我们实现了快速排序。但是现在虽然快速排序算法对大型数据集的效果更好,但对于较小的数据集来说速度非常慢。为了解决这个问题,我们实施了一个策略,对于小型数据集使用冒泡排序,更大规模的使用快速排序。

通俗解释:

策略模式允许您根据情况切换算法或策略。

维基百科:

在计算机编程中,策略模式(也称为政策模式)是一种行为软件设计模式,可以在运行时选择算法的行为。

程序化的例子

从上面翻译我们的例子。首先,我们有策略接口和不同的策略实现

interface SortStrategy
{
    public function sort(array $dataset): array;
}

class BubbleSortStrategy implements SortStrategy
{
    public function sort(array $dataset): array
    {
        echo "Sorting using bubble sort";

        // Do sorting
        return $dataset;
    }
}

class QuickSortStrategy implements SortStrategy
{
    public function sort(array $dataset): array
    {
        echo "Sorting using quick sort";

        // Do sorting
        return $dataset;
    }
}

然后我们的客户将使用任何策略

class Sorter
{
    protected $sorter;

    public function __construct(SortStrategy $sorter)
    {
        $this->sorter = $sorter;
    }

    public function sort(array $dataset): array
    {
        return $this->sorter->sort($dataset);
    }
}

它可以用作

And it can be used as

$dataset = [1, 5, 4, 3, 2, 8];

$sorter = new Sorter(new BubbleSortStrategy());
$sorter->sort($dataset); // Output : Sorting using bubble sort

$sorter = new Sorter(new QuickSortStrategy());
$sorter->sort($dataset); // Output : Sorting using quick sort

摘选自:github.com/guanguans/design-patter...

1年前 评论
周小云 (楼主) 1年前
忆往昔弹指间 (作者) 1年前
忆往昔弹指间 (作者) 1年前
周小云 (楼主) 1年前
周小云 (楼主) 1年前

上面说的策略模式没错 但是没法解决大量if else的情况 完整的模式应该是——状态模式

1年前 评论
失色天空 (作者) 1年前
周小云 (楼主) 1年前
周小云 (楼主) 1年前
失色天空 (作者) 1年前
周小云 (楼主) 1年前
周小云 (楼主) 1年前

策略的话 可以做一个map然后就不需要switch case了

1年前 评论
周小云 (楼主) 1年前

作用域不行吗

1年前 评论
周小云 (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!