Click here to Skip to main content
13,766,769 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

18.3K views
179 downloads
10 bookmarked
Posted 12 Feb 2015
Licenced CPOL

Strategy Design Pattern (C#)

, 12 Feb 2015
Rate this:
Please Sign up or sign in to vote.
A console application that calculates word occurrences found in a text.

Introduction

This tip demonstrates the usage of strategy design pattern. We display two different implementations of IDictionary<K,T> based on Strategy.

Background

Strategy is a behavioral design pattern that involves removing an algorithm from its host class and putting it in a separate class. There may be different algorithms (strategies) that are applicable for a given problem. If the algorithms are all kept in the host, messy code with lots of conditional statements will result. The Strategy pattern enables a client to choose which algorithm to use from a family of algorithms and gives it a simple way to access it.

Using the Code

Our family of algorithms, in our current example, is two different implementations of IDictionary<K,T>. First Algorithm will return a simple Dictionary<K,T>, whereas the second one will return a SortedDictionary<K,T>.

 public interface IDictionaryStrategy
 {
        IDictionary<string, int> GetProcessedText(string text);
 }

 public class DictionaryStrategy : IDictionaryStrategy
 {
        public IDictionary<string, int> GetProcessedText(string text)
        {
            string[] words = text.Split(' ', ';', ',', '.');

            var dictionary = new Dictionary<string, int>();
            int count = 1;

            foreach (var item in words.Where(x => x != string.Empty))
            {
                int value=0;
                if(!dictionary.TryGetValue(item,out value))
                {
                    count = 0;
                }
                dictionary[item] = ++count;
            }

            return dictionary;
        }
 }

public class SortedDictionaryStrategy : IDictionaryStrategy
{
        public IDictionary<string, int> GetProcessedText(string text)
        {
            string[] words = text.Split(' ', ';', ',', '.');
            
            var dictionary = new SortedDictionary<string, int>(new CaseInsensitiveComparer());

            int count = 1;

            foreach (var item in words.Where(x=>x!=string.Empty))
            {
                int value = 0;
                if (!dictionary.TryGetValue(item, out value))
                {
                    count = 0;
                }
                dictionary[item] = ++count;
            }

            return dictionary;
        }

        class CaseInsensitiveComparer : IComparer<string>
        {
            public int Compare(string s1, string s2)
            {
                return string.Compare(s1, s2, true);
            }
        }
 }

Points of Interest

Big fan of GoF!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Veronica S. Zotali
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralAvoid code duplication by using a base class Pin
htschirner18-Feb-15 13:09
memberhtschirner18-Feb-15 13:09 
GeneralRe: Avoid code duplication by using a base class Pin
Veronica S. Zotali19-Feb-15 10:06
memberVeronica S. Zotali19-Feb-15 10:06 
GeneralMy vote of 2 Pin
João Matos Silva14-Feb-15 12:58
professionalJoão Matos Silva14-Feb-15 12:58 
GeneralRe: My vote of 2 Pin
Veronica S. Zotali14-Feb-15 13:44
memberVeronica S. Zotali14-Feb-15 13:44 
Questionthanks Pin
Southmountain13-Feb-15 7:16
memberSouthmountain13-Feb-15 7:16 
GeneralDoesn't actually illustrate or discuss the strategy part of the pattern Pin
John Brett13-Feb-15 0:58
memberJohn Brett13-Feb-15 0:58 
GeneralRe: Doesn't actually illustrate or discuss the strategy part of the pattern Pin
Veronica S. Zotali13-Feb-15 1:19
memberVeronica S. Zotali13-Feb-15 1:19 
GeneralRe: Doesn't actually illustrate or discuss the strategy part of the pattern Pin
_GP18-Feb-15 1:11
member_GP18-Feb-15 1:11 
GeneralRe: Doesn't actually illustrate or discuss the strategy part of the pattern Pin
Veronica S. Zotali18-Feb-15 3:51
memberVeronica S. Zotali18-Feb-15 3:51 
QuestionLooks interesting Pin
GuyThiebaut13-Feb-15 0:10
professionalGuyThiebaut13-Feb-15 0:10 
AnswerRe: Looks interesting Pin
Veronica S. Zotali13-Feb-15 0:25
memberVeronica S. Zotali13-Feb-15 0:25 
GeneralRe: Looks interesting Pin
GuyThiebaut13-Feb-15 0:34
professionalGuyThiebaut13-Feb-15 0:34 
Questionthanks Pin
Member 1144952212-Feb-15 23:40
professionalMember 1144952212-Feb-15 23:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181114.1 | Last Updated 12 Feb 2015
Article Copyright 2015 by Veronica S. Zotali
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid