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
        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

comments powered by Disqus


  • What's New in Visual Studio 2019 v16.5 Preview 2

    The second preview of Visual Studio 2019 v16.5 has arrived with improvements across the flagship IDE, including the core experience and different development areas such as C++, Python, web, mobile and so on.

  • C# Shows Strong in Tech Skills Reports

    Microsoft's C# programming language continues to show strong in tech industry skills reports, with the most recent examples coming from a skills testing company and a training company.

  • Color Shards

    Sharing Data and Splitting Components in Blazor

    ASP.NET Core Version 3.1 has at least two major changes that you'll want to take advantage of. Well, Peter thinks you will. Depending on your background, your response to one of them may be a resounding “meh.”

  • Architecture Small Graphic

    Microsoft Ships Preview SDK, Guidance for New Dual-Screen Mobile Era

    Microsoft announced a new SDK and developer guidance for dealing with the new dual-screen mobile era, ushered in by the advent of ultra-portable devices such as the Surface Duo.

  • How to Create a Machine Learning Decision Tree Classifier Using C#

    After earlier explaining how to compute disorder and split data in his exploration of machine learning decision tree classifiers, resident data scientist Dr. James McCaffrey of Microsoft Research now shows how to use the splitting and disorder code to create a working decision tree classifier.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events