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);
}
}
}