Strategy Design Pattern

Anwendung des Patterns sind Klassen, die sich je nach Anwendungsfall anders verhalten sollen.

 

 

Das Strategy Design Pattern


Damit der Code frei von Abfragen bleicht entscheidet der Aufbau durch das Strategy Pattern welche Daten zurückgegeben werden. Als Beispiel habe ich eine kleine Kundenklasse genommen, in der je nach Anwendungsfall der Anzeigename des Kunden mit Vor/Nachname bzw. Nach/Vorname zurückgegeben werden soll.
Die Kundenklasse

using System;

namespace StrategyPattern
{
    public class Customer
    { 
        public enum CustomerSex
        {
            Male ,
            Female,
            Unknown
        }
        public enum DisplayMode
        {
            NameSurname,
            SurnameName
        }

        public string Surname { get; set; }
        public string Name { get; set; }
        public CustomerSex Sex { get; set; }
        public string Birthday { get; set; }
        public string DisplayName { get; set; }

        public Customer(int custId, DisplayMode displayStrategy )
        {
            switch (custId)
            {
                case 1:
                    Name = "Gerrit";
                    Surname = "Gruenthal";
                    Sex = CustomerSex.Male;
                    Birthday = "Unknown";
                    break;
                case 2:
                    Name = "Max";
                    Surname = "Musterman";
                    Sex = CustomerSex.Male;
                    Birthday = new DateTime(1981,5,3).ToShortDateString();
                    break;
                case 3:
                    Name = "Andrea";
                    Surname = "Morelli";
                    Sex = CustomerSex.Unknown;
                    Birthday = new DateTime(1989, 11, 9).ToShortDateString();
                    break;

            }

            switch (displayStrategy)
            {
               case DisplayMode.NameSurname:
                    DisplayName = Name + ' ' + Surname;
                    break;
               case DisplayMode.SurnameName:
                    DisplayName = Surname + ' ' + Name;
                    break;
            }
        }
    }
}

Das Strategy Pattern

namespace StrategyPattern
{
    public abstract class CustomerStatergy
    {
        public abstract Customer Customer(int customerId);
    }

    public class CustomerNameSurname : CustomerStatergy
    {
        public override Customer Customer(int customerId)
        {
            return new Customer(customerId, StrategyPattern.Customer.DisplayMode.NameSurname);
        }
    }

    public class CustomerSurnameName : CustomerStatergy
    {
        public override Customer Customer(int customerId)
        {
            return new Customer(customerId, StrategyPattern.Customer.DisplayMode.SurnameName);
        }
    }

    public class CustomerDisplayName
    {
        private CustomerStatergy _objStatergy;
        public void SetStatergy(CustomerStatergy obj)
        {
            _objStatergy = obj;
        }

        public Customer Show(int customerId)
        {
            return _objStatergy.Customer(customerId);
        }

    }
}

Der Programmaufruf

using System;

namespace StrategyPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            ShowCustomer(1);
            ShowCustomer(2);
            ShowCustomer(3);

            Console.ReadLine();
        }

        static void ShowCustomer(int customerId)
        {
            CustomerDisplayName obj = new CustomerDisplayName();
            Console.WriteLine("Anzeige Vorname Nachname");
            obj.SetStatergy(new CustomerNameSurname());
            ShowDetails(obj.Show(customerId));

            Console.WriteLine("Anzeige Nachname Vorname ");
            obj.SetStatergy(new CustomerSurnameName());
            ShowDetails(obj.Show(customerId));

        }
        static void ShowDetails(Customer inCust)
        {
            Console.WriteLine("Anzeigename\t:{0}", inCust.DisplayName);
            Console.WriteLine("Vorname\t\t:{0}", inCust.Name);
            Console.WriteLine("Nachname\t:{0}", inCust.Surname);
            Console.WriteLine("Geschlecht\t:{0}", inCust.Sex);
            Console.WriteLine("Geburtta:\t:{0}", inCust.Birthday);
            Console.WriteLine("-------------------------");

        }
    }
}

 

Und so sieht es aus:

Kommentare sind geschlossen