Strategy Pattern in .NET: Listing 1

Person class before strategy implementation

Listing 1: PersonV1.cs

using System;
 
namespace VSMStrategyPatternDemo
{
    public class PersonV1
    {
        public enum PersonType
        {
            Employer,
            Employee,
            Customer
        }
 
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime? DateOfBirth { get; set; }
        public int SSN { get; set; }
        public int EIN { get; set; }
 
        public PersonType Type { get; set; }
 
        public bool IsValid()
        {
            bool valid = false;
 
            switch (Type)
            {
                case PersonType.Employer:
                    if (FirstName != null && LastName != null && EIN > 0
                        && (DateOfBirth != null && DateOfBirth <= DateTime.Now.AddYears(-18)))
                        valid = true;
                    break;
                case PersonType.Employee:
                    if (FirstName != null && LastName != null && SSN > 0 && DateOfBirth != null 
                        && DateOfBirth <= DateTime.Now.AddYears(-18))
                        valid = true;
                    break;
                case PersonType.Customer:
                    if (FirstName != null && LastName != null && DateOfBirth != null 
                        && DateOfBirth.Value <= DateTime.Now.AddYears(-13))
                        valid = true;
                    break;
                default:
                    break;
            }
 
            return valid;
        }

       public override string ToString()
        {
            return String.Format("{0} {1} {2:M/d/yyyy}", FirstName, LastName, DateOfBirth);
        }
    }
}

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at [email protected].

comments powered by Disqus

Featured

Subscribe on YouTube