在子类中更改参数和返回类型是否意味着违反SOLID设计原则?

问题描述

我正在构建一个使用API​​从第三方源检索数据的应用程序。 消耗的数据需要进行格式化,因此我决定创建一个ConvertTypeInterface,以强制子类实现export class Reset extends React.Component<IFormProps,IFormState> { private currentPasswordProvided: boolean; private passwordValidated: boolean; private completeValidation: boolean; private resetParams: ResetPassword; constructor(props: IFormProps) { super(props); const errors: IErrors = {}; const values: IValues = {}; this.state = { errors,values }; this.currentPasswordProvided=false; this.passwordValidated= false; this.completeValidation=false; this.resetParams = { adminID : '',currentPassword: '',newPassword: '',} } private setValues = (values: IValues)=>{ this.setState({values: {...this.state.values,...values}}) }; private validate = (fieldName: string,value: string): string => { let newError: string = ""; if(fieldName!=='retypePassword'){ if ( this.props.fields[fieldName] && this.props.fields[fieldName].validation ){ newError = this.props.fields[fieldName].validation!.rule( value,fieldName,this.props.fields[fieldName].validation!.args ); if(fieldName=='currentPassword' && newError === "") { this.currentPasswordProvided = true; } else if(fieldName=='newPassword' && newError === "") { this.passwordValidated = true; } } } else if(fieldName === 'retypePassword'){ if ( this.props.fields[fieldName] && this.props.fields[fieldName].ComparePasswordValidation ){ let passwordValues : CompareValue = { newPassword: this.state.values.newPassword } newError = this.props.fields[fieldName].ComparePasswordValidation!.rule( value,passwordValues,this.props.fields[fieldName].ComparePasswordValidation!.args ); if(this.currentPasswordProvided===true&&this.passwordValidated===true&&newError===""){ const params = useLocation(); //<------ unable to call this ------------ const adminID = params.pathname.substr(params.pathname.lastIndexOf('/')+1); this.resetParams = { adminID: adminID,currentPassword: this.state.values.currentPassword,newPassword: this.state.values.newPassword,} this.completeValidation = true; } else{ this.completeValidation = false; } } } this.state.errors[fieldName] = newError; this.setState({ errors: { ...this.state.errors,[fieldName]: newError} }); return newError; } private haveErrors(errors: IErrors) { let haveError: boolean = true; Object.keys(errors).map((key: string)=> { if(errors[key].length > 0){ haveError = true; } }); return haveError; } private async handleSubmit ( e: React.FormEvent<HTMLFormElement> ): Promise<void> { e.preventDefault(); try { console.log(`These are the values----> ${this.state.values}`) const resetParams : ResetPassword = { adminID: 'adminID',currentPassword : this.state.values.currentPassword,} console.log(`These are the params being sent to the API ----> ${JSON.stringify(resetParams)}`) /* API call code goes here */ } catch (e) { console.log(`Request failed: ${e}`); // setShowAlert(true); } } public render() { const { submitSuccess,errors } = this.state; const context: IFormContext = { ...this.state,setValues: this.setValues,validate: this.validate }; // console.log(`This is the state ----> ${JSON.stringify(this.state.values)}`) return ( <IonPage id="login-registration-page"> <IonHeader> <IonToolbar color="primary"> <IonTitle>Reset Password</IonTitle> </IonToolbar> </IonHeader> <IonContent> <FormContext.Provider value={context}> <form onSubmit={this.handleSubmit} className="login-registration-form ion-padding"> <div className="container"> {this.props.render()} <div className="form-group"> <IonButton type="submit" //onClick={this.handleSubmit} disabled={!this.completeValidation} expand="block" >Submit</IonButton> </div> {this.completeValidation === false && !this.haveErrors(errors) && ( <div className="alert alert-danger" role="alert"> Sorry,an unexpected error has occured </div> )} </div> </form> </FormContext.Provider> </IonContent> </IonPage> ) } }方法,该方法会将任何数据转换/格式化为任何其他格式。

ConvertTypeInterface

convert($data)

现在有这个问题,我认为我可能不通过在参数或返回类型上不强制使用更严格的类型来破坏Liskov的Substitution原理,但是在这种情况下,我不能对参数或返回类型使用类型提示,因为$ data参数可以是任何东西:字符串,整数,对象,数组等。与返回类型相同。

转换器示例

interface ConverterTypeInterface
{
    public function convert($data);
}

用法示例

class ConvertToInteger implements ConverterTypeInterface
{
    public function convert($data)
    {
        return (int) $data;
    }

}

问题

这是违反Liskov的替代原则还是另一种SOLID设计原则?有办法改善吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)