Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 构造函数中多次使用同一接口_Oop_Design Patterns - Fatal编程技术网

Oop 构造函数中多次使用同一接口

Oop 构造函数中多次使用同一接口,oop,design-patterns,Oop,Design Patterns,如图所示,我创建了接口IValidator和几个实现该接口的类。我有一个类UserAccountManager,它管理用户帐户-创建新帐户、更改配置文件详细信息、更改密码等。。。问题是,我需要在某些情况下验证用户输入(更改密码、注册…),并且需要多个验证程序。我不认为这种设计是好的,因为构造函数中有多个验证器有更好的方法吗? 验证器通常作为一个类实现,但有多个可动态配置的规则。PHP示例: Validator::make($data, [ 'email' => ['req

如图所示,我创建了接口IValidator和几个实现该接口的类。我有一个类UserAccountManager,它管理用户帐户-创建新帐户、更改配置文件详细信息、更改密码等。。。问题是,我需要在某些情况下验证用户输入(更改密码、注册…),并且需要多个验证程序。我不认为这种设计是好的,因为构造函数中有多个验证器有更好的方法吗?


验证器通常作为一个类实现,但有多个可动态配置的规则。PHP示例:

 Validator::make($data, [
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);

从您的示例中,我可以看到所有三个规则(电子邮件、名称、密码)都与某种用户输入身份验证相关。因此,您只能使用一个验证器,负责验证用户登录。

验证器通常作为单个类实现,但具有可动态配置的多个规则。PHP示例:

 Validator::make($data, [
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);

从您的示例中,我可以看到所有三个规则(电子邮件、名称、密码)都与某种用户输入身份验证相关。因此,您只能使用一个验证器,负责验证用户登录。

您可以将验证器封装在单个对象或集合(列表或数组)中,以便构造函数接收单个参数

.............................
..+---------------------+
..|....<<class>>........|
..|....ValidatorGroup...|
..+---------------------+
..|..[+]..void.RegisterValidator..|
..|.......(IValidator..NewValidator)...|
..|..[+]..void.Validate(..)...|
..+---------------------+
.............................
..+--------------------------+
..|.........<<class>>........|
..|....UserAccountManager....|
..+--------------------------+
..|..[+]..UserAccountManager..<<constructor>>..|
..|.......(ValidatorGroup..NewGroup)...|
..+--------------------------+
..|..[+]..void..ChangePassword.(..)..|
..|..[+]..void..RegisterNewUser.(..)..|
..+--------------------------+
.................................
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
..+---------------------+
..|............|
..|..验证组|
..+---------------------+
..|..[+]..无效。注册验证器|
.........................................新验证器..........新验证器|
..|..[+]..无效。验证(..)|
..+---------------------+
.............................
..+--------------------------+
..|.................|
.................用户帐户管理器|
..+--------------------------+
..|..[+]..用户帐户管理器|
..|…(验证组..新组)|
..+--------------------------+
..|..[+]..无效..更改密码。(..)|
..|..[+]无效..注册用户|
..+--------------------------+
.................................

您还可以避免在构造函数中分配验证器,并将其作为单个属性添加。

您可以将验证器封装在单个对象或集合(列表或数组)中,以便构造函数接收单个参数

.............................
..+---------------------+
..|....<<class>>........|
..|....ValidatorGroup...|
..+---------------------+
..|..[+]..void.RegisterValidator..|
..|.......(IValidator..NewValidator)...|
..|..[+]..void.Validate(..)...|
..+---------------------+
.............................
..+--------------------------+
..|.........<<class>>........|
..|....UserAccountManager....|
..+--------------------------+
..|..[+]..UserAccountManager..<<constructor>>..|
..|.......(ValidatorGroup..NewGroup)...|
..+--------------------------+
..|..[+]..void..ChangePassword.(..)..|
..|..[+]..void..RegisterNewUser.(..)..|
..+--------------------------+
.................................
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
..+---------------------+
..|............|
..|..验证组|
..+---------------------+
..|..[+]..无效。注册验证器|
.........................................新验证器..........新验证器|
..|..[+]..无效。验证(..)|
..+---------------------+
.............................
..+--------------------------+
..|.................|
.................用户帐户管理器|
..+--------------------------+
..|..[+]..用户帐户管理器|
..|…(验证组..新组)|
..+--------------------------+
..|..[+]..无效..更改密码。(..)|
..|..[+]无效..注册用户|
..+--------------------------+
.................................

您还可以避免在构造函数中分配验证器,并将它们作为单个属性添加。

最后我使用了Facade模式-最后我使用了Facade模式-