Information Technology Reference
In-Depth Information
Va l i d a t o r, b u t n o w y o u n e e d t o u s e a n i n t e r n a t i o n a l v e r s i o n i n o n e i n s t a l -
lation. Rather than stick the extra functionality in this one class, you're
better off reducing the coupling between the different items. You create an
interface to validate any phone number:
public interface
IPhoneValidator
{
bool
ValidateNumber(
PhoneNumber
ph);
}
Next, change the existing phone validator to implement that interface, and
make it an internal class:
internal class
USPhoneValidator
:
IPhoneValidator
{
public bool
ValidateNumber(
PhoneNumber
ph)
{
// perform validation.
// Check for valid area code, exchange.
return true
;
}
}
Finally, you can create a class for international phone validators:
internal class
InternationalPhoneValidator
:
IPhoneValidator
{
public bool
ValidateNumber(
PhoneNumber
ph)
{
// perform validation.
// Check international code.
// Check specific phone number rules.
return true
;
}
}
To fi n i s h t h i s i m p l e m e n t a t i o n , y o u n e e d t o c r e a t e t h e p r o p e r c l a s s b a s e d o n
the type of the phone number. You can use the factory pattern for this pur-
pose. Outside the assembly, only the interface is visible. The classes, which
are specific for different regions in the world, are visible only inside the
assembly. You can add different validation classes for different regions
without disturbing any other assemblies in the system. By limiting the