I'm having trouble extending an application. It is an attendance record system. Currently each employee records attendance by a card that has a QR code. Now they want to add fingerprint recognition, and there was no problem until they asked for two forms of identification must coexist in the system. Thus, the system must be able to sense QRs of the employee, but also his fingerprint.
The way to fix it in QrIdStrategy via .equalsTo(id) method is:
equalsTo(id){
if (id == isKindOf (QrIdStrategy))
if (this.getEmployeeId () == id.getEmployeeId ())
return true;
return false;
}
But I understand that asking about the class of an object is a bad practice and don't want to do that. How can I fix it?
I thought of the Visitor pattern, but I still have the same problem of comparing two classes that are not of the same type (because the system could scan any of the two types)
Maybe the first diagram already shows what I'm going to say, but this comes down to polymorphism.
Create an abstract class (e.g. IdentificationStrategy
with some method like equlasTo()
). Derive two classes: QRStrategy
and FingerPrintStrategy
from the IdentificationStrategy
and implement equlasTo()
method in each of them.
Now, there is a place in code where you know whether you are about to create an instance of QRStrategy
or FingerPrintStrategy
. Just assign an instance of that object to a variable of IdentificationStrategy
type (e.g. let's name it 'foo' ). When you call the equlasTo()
method on the 'foo', you don't care about object type any more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With