You may discover sometimes that some of your classes have method you would like to use without creating new instance of that class. In this case we have to use expose static method refactoring to make this method static.
Let’s say you have class called MailValidator that validates your e-mail objects so your system sends out only messages that follow the set of custom business rules. One of the methods is called IsValidAddress. The code of IsValidAddress method is borrowed from The Code Project article Effective Email Address Validation.
public class MailValidator
{
private Mail _mail;
public MailValidator(Mail mail)
{
_mail = mail;
}
// Block of other validation rules
private bool IsValidAddress()
{
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex re = new Regex(strRegex);
return re.IsMatch(_mail.To);
}
}
IsValidAddress is the method we are interested in. All the other validation rules in this class are very specific to those e-mails and we are not interested in those rules at more common level. But we want to use IsValidAddress method on some other parts of our system to validate e-mail addresses. Let’s suppose we have to validate e-mail addresses that users insert when they are joining with our site. In this case we don’t have e-mail object – we just want to make sure that user inserted correct e-mail address.
What we have to do is to refactor IsValidAddress method. We have to follow these steps:
- make IsValidAddress method publicly visible,
- make IsValidAddress static,
- add new parameter emailAddress to its signature,
- resolve the dependence of e-mail object.
As a result our MailValidator looks like this.
public class MailValidator
{
private Mail _mail;
public MailValidator(Mail mail)
{
_mail = mail;
}
// Block of other validation rules
public static bool IsValidAddress(string emailAddress)
{
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex re = new Regex(strRegex);
return re.IsMatch(emailAddress);
}
}
Now we can use IsValidAddress method without instantiating the new instance of MailValidator class. Let’s say we have form called join.aspx and we have textbox emailTextBox defined on it. Also let there be button called joinButton. When user presses joinButton we want to validate form fields and one of these fields is e-mail address.
protected void joinButton_Click(object sender, EventArgs e)
{
// ...
if(!MailValidator.IsValidAddress(emailTextBox.Text))
{
SetError("email", "E-mail address is not valid!");
return;
}
// ...
}
Now we can use MailValidator class to validate e-mail addresses everywhere we need in our system. And if there are more common methods besides IsValidAddress we can make those methods visible same way.
As a last thing be warned that using static methods heavily is not a good idea because you will loose many of the object-oriented programming advantages this way. You can read more about static methods from developer Fusion article A Twisted Look at Object Oriented Programming in C#.