C# 策略模式(Strategy)
理解:当一件事件(基类),在不同的时间或者不同的地点需要不同的策略时,考虑用该模式,好处就是修改或者添加某个子策略时不会影响其它子策略,也不用去修改客户端
代码:
using System.Windows.Forms;
namespace DesignMode.Strategy
{
//4S手机抽象类
public abstract class IPhone4S
{
public abstract void GetPrice(string color);
}
//港版4S
public class HK_IPhone4S : IPhone4S
{
public override void GetPrice(string color)
{
if (color.Equals("白"))
{
MessageBox.Show("港版白色价格为:4199");
}
else if (color.Equals("黑"))
{
MessageBox.Show("港版黑色价格为:3999");
}
else
{
MessageBox.Show("港版" + color + "色无货");
}
}
}
//台版4S
public class TW_IPhone4S : IPhone4S
{
public override void GetPrice(string color)
{
if (color.Equals("白"))
{
MessageBox.Show("台版白色价格为:5199");
}
else if (color.Equals("红"))
{
MessageBox.Show("台版红色价格为:4999");
}
else
{
MessageBox.Show("台版" + color + "色无货");
}
}
}
//大陆版4S
public class CN_IPhone4S : IPhone4S
{
public override void GetPrice(string color)
{
if (color.Equals("白"))
{
MessageBox.Show("大陆版白色价格为:5999");
}
else
{
MessageBox.Show("大陆版" + color + "色无货");
}
}
}
//手机销售店
public class MobileShop
{
IPhone4S _i4s;
public MobileShop(string type)
{
//简单工厂模式
switch (type)
{
case "港版":
_i4s = new HK_IPhone4S();
break;
case "台版":
_i4s = new TW_IPhone4S();
break;
case "大陆版":
_i4s = new CN_IPhone4S();
break;
}
}
public void DisplayPrice(string color)
{
_i4s.GetPrice(color);
}
}
}
namespace DesignMode.Strategy
{
//4S手机抽象类
public abstract class IPhone4S
{
public abstract void GetPrice(string color);
}
//港版4S
public class HK_IPhone4S : IPhone4S
{
public override void GetPrice(string color)
{
if (color.Equals("白"))
{
MessageBox.Show("港版白色价格为:4199");
}
else if (color.Equals("黑"))
{
MessageBox.Show("港版黑色价格为:3999");
}
else
{
MessageBox.Show("港版" + color + "色无货");
}
}
}
//台版4S
public class TW_IPhone4S : IPhone4S
{
public override void GetPrice(string color)
{
if (color.Equals("白"))
{
MessageBox.Show("台版白色价格为:5199");
}
else if (color.Equals("红"))
{
MessageBox.Show("台版红色价格为:4999");
}
else
{
MessageBox.Show("台版" + color + "色无货");
}
}
}
//大陆版4S
public class CN_IPhone4S : IPhone4S
{
public override void GetPrice(string color)
{
if (color.Equals("白"))
{
MessageBox.Show("大陆版白色价格为:5999");
}
else
{
MessageBox.Show("大陆版" + color + "色无货");
}
}
}
//手机销售店
public class MobileShop
{
IPhone4S _i4s;
public MobileShop(string type)
{
//简单工厂模式
switch (type)
{
case "港版":
_i4s = new HK_IPhone4S();
break;
case "台版":
_i4s = new TW_IPhone4S();
break;
case "大陆版":
_i4s = new CN_IPhone4S();
break;
}
}
public void DisplayPrice(string color)
{
_i4s.GetPrice(color);
}
}
}
客户端代码:
private void btn_Strategy_Click(object sender, EventArgs e)
{
MobileShop shopTW = new MobileShop("台版");
shopTW.DisplayPrice("黑");
shopTW.DisplayPrice("白");
shopTW.DisplayPrice("红");
MobileShop shopHK = new MobileShop("港版");
shopHK.DisplayPrice("黑");
shopHK.DisplayPrice("白");
shopHK.DisplayPrice("红");
MobileShop shopCN = new MobileShop("大陆版");
shopCN.DisplayPrice("黑");
shopCN.DisplayPrice("白");
shopCN.DisplayPrice("红");
}
{
MobileShop shopTW = new MobileShop("台版");
shopTW.DisplayPrice("黑");
shopTW.DisplayPrice("白");
shopTW.DisplayPrice("红");
MobileShop shopHK = new MobileShop("港版");
shopHK.DisplayPrice("黑");
shopHK.DisplayPrice("白");
shopHK.DisplayPrice("红");
MobileShop shopCN = new MobileShop("大陆版");
shopCN.DisplayPrice("黑");
shopCN.DisplayPrice("白");
shopCN.DisplayPrice("红");
}