I am currently working hard to improve my code quality & apply design patterns. Today was dedicated to the Proxy Pattern.
My goal was to let a AuthService class act as a manager for any authentification service I may use in the future.
The user should be able to instantiate any service's class as a parameter to AuthService, so that there'd be no hassle when the need to change the service arises: all you'd need is to update the parameter.
For that purpose, I created an AuthInterface interface in Typescript which would be implemented by the AuthService manager componement & the actual services modules. I tried very hard to respect the SOLID principles by using dependency injection instead of using external code.
This being said, I am unsure if this design is an actual implementation of the Proxy pattern, or if I turned it into an anti-pattern by adding too much complexity.
Please find the code below:
// The interface for AuthService & any auth module
interface AuthInterface<ServiceType, UserType> {
init(initCallback?: () => ServiceType): ServiceType;
signIn(
authProvider?: ServiceType,
signIncallback?: (authProvider?: ServiceType) => boolean | never | object
): boolean;
getID(
authProvider?: ServiceType,
getIDCallback?: (authProvider?: ServiceType) => UserType
): UserType;
}
// The AuthService component which takes any authentification module. Is this the Proxy pattern?
class AuthService<ServiceType, UserType>
implements AuthInterface<ServiceType, UserType>
{
SomeService;
constructor(SomeService: AuthInterface<ServiceType, UserType>) {
this.SomeService = SomeService;
}
init(initCallback: () => ServiceType) {
return this.SomeService.init(initCallback);
}
signIn(
authProvider: ServiceType,
signIncallback: (authProvider?: ServiceType) => boolean | never
) {
return this.SomeService.signIn(authProvider, signIncallback);
}
getID(
authProvider: ServiceType,
getIDCallback: (authProvider?: ServiceType) => UserType
) {
return this.SomeService.getID(authProvider, getIDCallback);
}
}
// An authentification module, here Firebase, to add as a parameter to AuthService
class FirebaseAuth<ServiceType, UserType>
implements AuthInterface<ServiceType, UserType>
{
init(initCallback: () => ServiceType) {
return initCallback();
}
signIn(
authProvider: ServiceType,
signIncallback: (authProvider?: ServiceType) => boolean | never
) {
return signIncallback(authProvider);
}
getID(
authProvider: ServiceType,
getIDCallback: (authProvider?: ServiceType) => UserType
) {
return getIDCallback(authProvider);
}
}
I am new to Typescript, so please do not hesitate to tell me where the code can be improved.
I would be very grateful for any clarification. Thank you!
Aucun commentaire:
Enregistrer un commentaire