No nonsense C # design pattern of the XV: strategy

Source: Internet
Author: User

Intention

Define a series of algorithms, encapsulate them one by one, and make them interchangeable with each other. This mode allows the algorithm to change independently of its customers.

Scene

When developing a program, we often take different algorithms to deal with the object according to the different environment. For example, in a news listing page you need to display all the news, while in a news search page you need to display matching news based on the search keyword. If there is a ShowData method inside the news class, then we may pass in a Searchword parameter and in the method, if the argument is empty, show all the news, and search if the argument is not empty. If there is a paging requirement, then it is possible to determine in the method whether to page pagination and so on.

There are a few bad places to do this, one is that the ShowData method is too complex, the code is not easy to maintain and may degrade performance, the second is if there are other requirements will need to modify the ShowData method, three is not conducive to reusing some of the common parts of the algorithm. By introducing the strategy pattern, the algorithm is encapsulated so that it can be flexibly extended and replaced.

Sample code

using System;





using System.Collections.Generic;





using System.Text;





using System.Collections;





namespace Strategyexample





{





Class Program





    {





static void Main (string[] args)





        {





Data data = new data ();





data. ADD ("AAA");





data. ADD ("BBB");





data. ADD ("CCC");





data. ADD ("abc");





data. Show ();





data. Setshowdatastrategy (New Searchdata ("a"));





data. Show ();





data. Setshowdatastrategy (New Showpageddata (2,1));





data. Show ();





        }





    }





Abstract class Showdatastrategy





    {





abstract public void ShowData (IList data);





    }





class Showalldata:showdatastrategy





    {





public override void ShowData (IList data)





        {





for (int i = 0; i < data. Count; i++)





            {





Console.WriteLine (Data[i]);





            }





        }





    }





class Showpageddata:showdatastrategy





    {





private int pageSize;





private int pageIndex;





public showpageddata (int pageSize, int pageIndex)





        {





this.pagesize = pageSize;





this.pageindex = PageIndex;





        }





public override void ShowData (IList data)





        {





for (int i = pageSize * PAGEINDEX; i < PageSize * (PageIndex + 1); i++)





            {





Console.WriteLine (Data[i]);





            }





        }





    }





class Searchdata:showdatastrategy





    {





private String Searchword;





public string Searchword





        {





get {return searchword;}





set {Searchword = value;}





        }





public Searchdata (string searchword)





        {





This.searchword = Searchword;





        }





public override void ShowData (IList data)





        {





for (int i = 0; i < data. Count; i++)





            {





if (Data[i]. ToString (). Contains (Searchword))





Console.WriteLine (Data[i]);





            }





        }





    }





class Data





    {





private showdatastrategy strategy;





private IList data = new ArrayList ();





public void Setshowdatastrategy (Showdatastrategy strategy)





        {





this.strategy = strategy;





        }





public void Show ()





        {





if (strategy = null)





Strategy = new ShowAllData ();





Console.WriteLine (strategy. GetType (). ToString ());





strategy. ShowData (data);





        }





public void Add (string name)





        {





data. ADD (name);





        }





    }





}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.