Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to ask about the object class, but it's a bad practice. Alternatives for this case?

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.

I have the following classes:

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)

enter image description here

like image 424
gal007 Avatar asked Apr 25 '13 15:04

gal007


1 Answers

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.

like image 140
user1764961 Avatar answered Nov 10 '22 02:11

user1764961