PHP开发PHP经验分享

编程中的设计模式之过滤器模式

2020-08-10  本文已影响0人  phpworkerman
定义

过滤器模式(Filter Pattern)允许使用不同标准过滤同一组对象,通过逻辑运算结耦的方式把标准对象结合起来,从而获得单一标准的结果,属于结构型模式。

代码实例
<?php
interface Validate
{
    public function rule($data);
}

class ActiveValidate implements Validate
{
    public function rule($data)
    {
        if($data->getActive() == 1) {
            return true;
        }

        return '账户未激活';
    }
}

class GenderValidate implements Validate
{
    public function rule($data)
    {
        if($data->getGender() == 2 || $data->getGender() == 1){
            return true;
        }

        return '惊现外星人';
    }
}

class EmailValidate implements Validate
{
    public function rule($data)
    {
        if(strpos($data->getEmail(),'@') !== false) {
            return true;
        }

        return 'Email 格式错误';
    }
}

class AndCondition
{
    protected $condition = [];

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

    public function applyRule($customer)
    {
        $msg = [];
        foreach($this->condition as $key => $item){
            $result = $item->rule($customer);
            if($result !== true){
                $msg[] = $result;
            }
        }

        return empty($msg) ? '验证成功' : $msg;
    }
}

class OrCondition
{
    protected $condition = [];

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

    public function applyRule($customer)
    {
        foreach($this->condition as $key => $item){
            $result =  $item->rule($customer);
            if($result === true){
                return get_class($item) . '规则应用成功,其他规则不生效';
            }
        }

        return false;
    }
}

class Customer
{
    protected $name;
    protected $active;
    protected $email;
    protected $gender;

    public function __construct($name, $active, $email, $gender)
    {
        $this->name = $name;
        $this->active = $active;
        $this->email = $email;
        $this->gender = $gender;
    }

    public function getActive()
    {
        return $this->active;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function getGender()
    {
        return $this->gender;
    }
}

class FilterDemo
{
    public function andApplyRule()
    {
        $customerList = new Customer('Sam',1,'sam98989@gmail.com',2);
        $andCondition = new AndCondition([new ActiveValidate(),new GenderValidate(),new EmailValidate()]);
        return $andCondition->applyRule($customerList);
    }

    public function orApplyRule()
    {
        $customerList = new Customer('Lisa',1,'lisa098909',1);
        $andCondition = new OrCondition([new ActiveValidate(),new GenderValidate(),new EmailValidate()]);
        return $andCondition->applyRule($customerList);
    }
}

$filterDemo = new FilterDemo();
$msg1 = $filterDemo->andApplyRule();
$msg2 = $filterDemo->orApplyRule();
总结

如果用过框架,对其中的验证器应该是熟悉的,根据提前设定好的验证规则,客户端只需要传入需要验证的数据,就可以获取相应的返回值。

上一篇下一篇

猜你喜欢

热点阅读