Strategy Pattern in .NET: Listing 5

Person class after strategy pattern

Listing 5: Person.cs

using System;
using System.Collections.Generic;
 
namespace VSMStrategyPatternDemo
{
    public class Person
    {
        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; }
 
        private static Dictionary<PersonType, IPersonValidationStrategy> _validationStategies;
 
        public Person()
        {
            _validationStategies = new Dictionary<PersonType, IPersonValidationStrategy>();
            _validationStategies.Add(PersonType.Employer, new EmployerValidationStrategy());
            _validationStategies.Add(PersonType.Employee, new EmployeeValidationStrategy());
            _validationStategies.Add(PersonType.Customer, new EmployeeValidationStrategy());
        }
 
        public bool IsValid()
        {
            return _validationStategies[Type].IsValid(this);
        }

        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