Showing posts with label Design pattern. Show all posts
Showing posts with label Design pattern. Show all posts

Remember Old School OO Principle While Using RegularExpressionValidator

I'm sure all of you are frequently using RegularExpressionValidator in your project. But are sure you are not repeating same expression in multiple pages? Is your code impervious to Change? If you are a bit careful while writing code, you can avoid this. And your friend will be 'Inheritance'.

Suppose you are using a RegularExpressionValidator to validate phone number. You may possibly going to write following code block

<asp:RegularExpressionValidator ID="revPhoneNumber" Display="Dynamic" ErrorMessage="Valid phone no required." ControlToValidate="tbPhoneNumber" ValidationExpression="^\d{8,}$" runat="server">*</asp:RegularExpressionValidator>

If you need this Validator in more than one page, then writing the same code block in every page obviously produces code duplication. Moreover if your project manager asks you to allow '+' at the beginning of the phone number then you have to change the regular expression in all pages. That will be surely a painful task for you. You can make your life easy by following approach.

Create your own validator by inheriting RegularExpressionValidator. Assign a regular expression to the ValidationExpression property

namespace TestValidator.Validators
{
    public class PhoneNumberValidator : RegularExpressionValidator
    {
        public PhoneNumberValidator()
        {
            ValidationExpression = "^\\d{8,}$";
        }
    }
}

To use your own PhoneNumberValidator first add this in the Registry
<%@ Register Assembly="TestValidator(Project Name)" Namespace="TestValidator.Validators" TagPrefix="myvalidators" %>

Then use this in the following way

<myvalidators:PhoneNumberValidator ID="revPhoneNumber" Display="Dynamic" ErrorMessage="Valid phone no required." ControlToValidate="tbPhoneNumber" runat="server">*</myvalidators:PhoneNumberValidator>

Now it is a matter of seconds to accomplish what your project manager wants.

Implementing Strategy pattern to support different fingerprint device login system

In our ERP project one of the features of Human resource module is managing daily attendance of the employee using biometric authentication. Our client wants to use NAC 3000 as a fingerprint verification device.

The task is simple. The software of that device writes data in an access database and in a log file whenever an employee place finger on it. We just have to pull the data from the access file or the log file after certain interval. The problem is that as our ERP is generalized and can be customizable for any type of organization so we have to think about other devices that serve the same purpose.

We analyze that all the devices vary in one point and that is the format of the log file or the database file. That is only the file parsing algorithm varies from device to device. This motivates me to use Strategy pattern here as the Strategy pattern defines a family o algorithms, encapsulate each one and makes them interchangeable.

Class diagram


Details
LogInDevice is an abstract class which has an instance fileParser as the interface type not a concrete class implementation type. The LogInDevice objects will set the variable polymorphically to reference the specific file parser.

The FileParser is an interface which has four mehthods. The NAC300Parser, NAC2500Parser implements this interface. When we need to integrate a new device we just have to create a new class that will implement this interface. The code of parsing the logfile will be written in the parseFile() method.

Implementation of the doParsing() method of LogInDevice class

  
public void doParsing()
{
this.fileParser.parseFile();
userId = this.fileParser.getUserId();
purpose = this.fileParser.getPurpose();
time = this.fileParser.getTime();
//Code block for saving the data to database goes here.
}


In this part of the code we don't care about what kind of file parser it is.

Are you thinking of setting the fileParser instance variable? Let’s take a look at the NAC3000 class


public class NAC3000 extends LogInDevice
{
public NAC3000()
{
this.fileParser = new NAC3000Parser();
}
}



The NAC3000 uses the NAC3000Parser to handle its file parsing. So when the doParsing() is called the responsibility for the parsing is delegated to the NAC3000Parser object and we got our desired data.

Change the FileParser dynamically

To change the parsing algorithm dynamically you just need a setter method in LogInDevice class


public void setFileParser(FileParser fileParser)
{
this.fileParser = fileParser;
}



We can call this method anytime we want to change the file parsing algorithm on the fly.

With this design we can integrate a new Fingerprint or any other authentication device very easily in our system and obviously without modifying the existing code.

Total Pageviews

Tags

Twitter Updates
    follow me on Twitter

    Followers