I have to generate a code that will send through SMS or Email to implement the One Time Password (OTP) requirement of our client.
I just finished creating the design using strategy pattern, .
.
This it the first time I've used strategy pattern in real life projects so I would like to ask if I'm doing it right.
Below is just a sample code that will be used by client.
//OTPGeneratorStrategy generatorStrategy = new EmailGeneratorStrategy("test@gmail.com"); for sending to Email for example
OTPGeneratorStrategy generatorStrategy = new SmsGeneratorStrategy(993454454545);
OTPGeneratorContextgeneratorContext = new OTPGeneratorContext(generatorStrategy);
context.generate("1-12345-0");
OTPValidatorStrategy validatorStrategy = new SmsValidatorStrategy();
OTPValidatorContext validatorContext = new OTPValidatorContext (validatorStrategy );
try {
validatorContext.validate("1-12345-0","e7241f");
} catch (InvalidVerificationKeyException ivk) {
System.out.println("Verification key is invalid!");
} catch(VerificationKeyExpiredException vke) {
System.out.println("Verification key is already expired, Please regenerate");
}
Updated:
Thank you for the feedback, actually I was not able to update my latest UML design and already separated the generation and validation.
And the reason why I've used Exception rather than enums because the project was built in java 1.4.