策略模式由一组相关的算法组成,这些算法被封装在称为Context的驱动类中。客户端程序可以选择这些不同的算法之一,或者由Context类来自动选择最合适的算法。该模式的目的是使得这些算法之间可以互换,并提供一种最合适算法的方式。该模式与状态模式的区别是,在策略模式中,用户一般会选中几个不同策略中的一个加以使用,统一时间内只能有一个i额策略被 实例化以及活动于Context的内部。然而在状态模式中,所有可能的状态都是实例化了的,并且同时活动与状态管理器类的内部。
程序中经常遇到这样的情况,希望以多种不同的方式来完成相同的事情。
策略模式把各种策略封装在一个模块中,并提供一个简单的接口来支持在这些策略之间做选择。每个策略都应该有相同的编程接口,虽然他们不需要都是相同的类继承层次的成员,但他们必须实现相同的编程接口。
我们考虑一个简单的绘图程序,根据数据,用户可以选择绘制柱状图还是线形图。
用户界面如图:
有两个按钮,点击不同的按钮会选择不同的绘制算法。
我们有几个绘图用的基础抽象类;
using System;
namespace Strategy
{
/// <summary>
/// Summary description for PlotStrategy.
/// </summary>
public abstract class PlotStrategy
{
public abstract void plot( float[] x, float[] y);
}
}
我们需要在派生类中把计算代码实现。
然后是设计我们的 Context, Context类相当于一个交通指挥者,由他来决定调用哪一种策略。在这里看来,这个交通指挥者更像简单工厂模式中的工厂,通过参数或者选择返回不同的算法类的实例。
using System;
using System.Collections ;
using CsharpPats;
namespace Strategy
{
/// <summary>
/// Selects which plot strategy to carry out
/// </summary>
public class Context {
float[] x, y;
PlotStrategy plts; //strategy selected goes here
//-----
public void plot() {
readFile(); //read in data
plts.plot (x, y);
}
//-----
//select bar plot
public void setBarPlot() {
plts = new BarPlotStrategy ();
}
//-----
//select line plot
public void setLinePlot() {
plts = new LinePlotStrategy();
}
//-----
public void readFile() {
ArrayList xc = new ArrayList();
ArrayList yc = new ArrayList();
//reads data in from data file
csFile fl = new csFile("data.txt");
fl.OpenForRead();
string sline = fl.readLine ();
while (sline != null) {
int i = sline.IndexOf(" ");
if (i > 0) {
float tmp = Convert.ToSingle (sline.Substring (0, i));
xc.Add(tmp);
tmp = Convert.ToSingle (sline.Substring(i + 1));
yc.Add(tmp);
}
sline = fl.readLine();
}
//copy into arrays from collections
float[] xp = new float[xc.Count];
float[] yp = new float[yc.Count];
for (int i = 0; i< xc.Count; i++) {
xp[i] = (float)xc[i];
yp[i] = (float)yc[i];
}
x = xp;
y = yp;
fl.close();
}
}
}
同时在程序的按钮部分还使用了命令模式,这样每个按钮的执行代码都包含在了自定义的按钮类中。
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace Strategy
{
/// <summary>
/// Summary description for LineButton.
/// </summary>
public class LineButton : System.Windows.Forms.Button, Command
{
private System.ComponentModel.Container components = null;
private Context contxt;
public LineButton() {
InitializeComponent();
this.Text = "Line plot";
}
public void setContext(Context ctx) {
contxt = ctx;
}
public void Execute() {
contxt.setLinePlot();
contxt.plot();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace Strategy
{
/// <summary>
/// Summary description for BarButton.
/// </summary>
public class BarButton : System.Windows.Forms.Button , Command
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Context contxt;
public BarButton()
{
InitializeComponent();
}
public void setContext(Context ctx) {
contxt = ctx;
}
public void Execute() {
contxt.setBarPlot();
contxt.plot();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}
这两个按钮中通过Context的实例调用算法。
接下来就是我们的具体的策略的实现:
using System;
namespace Strategy
{
/// <summary>
/// Summary description for BarPlotStrategy.
/// </summary>
public class BarPlotStrategy :PlotStrategy
{
public override void plot(float[] xp, float[] yp) {
BarPlot bplot = new BarPlot ();
bplot.Show ();
bplot.plot (xp, yp);
}
}
}
using System;
namespace Strategy
{
/// <summary>
/// Summary description for LinePlotStrategy.
/// </summary>
public class LinePlotStrategy : PlotStrategy {
public override void plot(float[] x, float[] y) {
LinePlot lplt = new LinePlot();
lplt.Show ();
lplt.plot (x, y);
}
}
}
这里把具体的绘制算法放在了LinePlot和BarPlot 这两个窗体中了,其中LinePlot继承了BarPlot。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Strategy
{
/// <summary>
/// Summary description for BarPlot.
/// </summary>
public class BarPlot : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pic;
protected float ymin, ymax, xfactor, yfactor;
protected float xpmin, xpmax, ypmin, ypmax, xp, yp;
private float xmin, xmax;
private int w, h;
protected float[] x, y;
private ArrayList colors;
protected Pen bPen;
protected bool hasData;
protected const float max = 1.0e38f;
private System.ComponentModel.Container components = null;
public BarPlot()
{
InitializeComponent();
}
public void setPenColor(Color c){
bPen = new Pen(c);
}
public void plot(float[] xp, float[] yp) {
x = xp;
y = yp;
setPlotBounds(); //compute scaling factors
hasData = true;
pic.Refresh();
}
public void findBounds() {
xmin = max;
xmax = -max;
ymin = max;
ymax = -max;
for (int i = 0; i< x.Length ; i++) {
if (x[i] > xmax) xmax = x[i];
if (x[i] < xmin) xmin = x[i];
if (y[i] > ymax) ymax = y[i];
if (y[i] < ymin) ymin = y[i];
}
}
public virtual void setPlotBounds() {
findBounds();
//compute scaling factors
h = pic.Height;
w = pic.Width;
xfactor = 0.8F * w / (xmax - xmin);
xpmin = 0.05F * w;
xpmax = w - xpmin;
yfactor = 0.9F * h / (ymax - ymin);
ypmin = 0.05F * h;
ypmax = h - ypmin;
//create array of colors for bars
colors = new ArrayList();
colors.Add(new SolidBrush(Color.Red));
colors.Add(new SolidBrush(Color.Green));
colors.Add(new SolidBrush(Color.Blue));
colors.Add(new SolidBrush(Color.Magenta));
colors.Add(new SolidBrush(Color.Yellow));
}
//-----
public int calcx(float xp) {
int ix = (int)((xp - xmin) * xfactor + xpmin);
return ix;
}
//-----
public int calcy(float yp) {
yp = ((yp - ymin) * yfactor);
int iy = h - (int)(ypmax - yp);
return iy;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pic = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// pic
//
this.pic.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pic.Location = new System.Drawing.Point(24, 24);
this.pic.Name = "pic";
this.pic.Size = new System.Drawing.Size(248, 208);
this.pic.TabIndex = 0;
this.pic.TabStop = false;
this.pic.Paint += new System.Windows.Forms.PaintEventHandler(this.pic_Paint);
//
// BarPlot
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.pic});
this.Name = "BarPlot";
this.Text = "BarPlot";
this.ResumeLayout(false);
}
#endregion
protected virtual void pic_Paint(object sender, PaintEventArgs e) {
Graphics g = e.Graphics;
if (hasData) {
for (int i = 0; i< x.Length; i++){
int ix = calcx(x[i]);
int iy = calcy(y[i]);
Brush br = (Brush)colors[i];
g.FillRectangle(br, ix, h - iy, 20, iy);
}
}
}
}
}
using System;
using System.Drawing ;
using System.Windows.Forms;
namespace Strategy {
/// <summary>
/// Summary description for LinePlot.
/// </summary>
public class LinePlot :BarPlot {
public LinePlot() {
init();
}
protected void init() {
bPen = new Pen(Color.White);
this.Text = "Line Plot";
}
protected override void pic_Paint(object sender, PaintEventArgs e) {
Graphics g= e.Graphics;
if (hasData) {
for (int i = 1; i< x.Length; i++) {
int ix = calcx(x[i - 1]);
int iy = calcy(y[i - 1]);
int ix1 = calcx(x[i]);
int iy1 = calcy(y[i]);
g.DrawLine(bPen, ix, iy, ix1, iy1);
}
}
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// LinePlot
//
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "LinePlot";
this.ResumeLayout(false);
}
//private void pic_Click(object sender, EventArgs e)
//{
//}
//private void pic_Click(object sender, EventArgs e)
//{
//}
}
}
策略模式允许动态的选择一个算法之一,因为这些算法可以是相关的,位于某个继承层次结构中,也可以是无关的,只要他们实现一个共同的几口就可以了。