Context (scenarios):
1, requires the use of algorithms ConcreteStrategy provided.
2, internal maintenance instance of a Strategy.
3, is responsible for dynamically setting Strategy specific algorithm runs.
4, interaction and data transfer between the responsible with the Strategy.
Strategy (Strategy abstract class):
1, define a common interface, a variety of algorithms implemented in different ways this interface, Context this interface calls using different algorithms, using general or abstract class implements an interface.
ConcreteStrategy (specific policy class):
public abstract class Computer
{
public abstract string MainBoard();
public abstract string Cpu();
public abstract string PhenoType();
public abstract string Memory();
public abstract string HardDisk();
public abstract string Display();
}
public class lenovo:Computer
{
public override string MainBoard()
{
return "华硕880G系列";
}
public override string Cpu()
{
return "闪龙双核180(2.4GHz)";
}
public override string PhenoType()
{
return "集成高性能显卡";
}
public override string Memory()
{
return "1G DDRIII";
}
public override string HardDisk()
{
return "500G";
}
public override string Display()
{
return "19寸宽屏液晶显示器";
}
}
And then build HP.cs:
public class HP:Computer
{
public override string MainBoard()
{
return "ATI RS482";
}
public override string Cpu()
{
return "速龙 64位 x2 双核5000+";
}
public override string PhenoType()
{
return "NV G310 512M";
}
public override string Memory()
{
return "2G DDR2 667 ";
}
public override string HardDisk()
{
return "3200G";
}
public override string Display()
{
return "19寸宽屏液晶显示器";
}
}
And then buy a new computer class BuyComputer.cs:
public class BuyComputer
{
private Computer _computer;
public BuyComputer(Computer computer)
{
_computer = computer;
}
public string ShowComputerConfigure()
{
StringBuilder strCom = new StringBuilder();
strCom.AppendLine("你的电脑配置如下:");
strCom.AppendLine("主板是:" + _computer.MainBoard());
strCom.AppendLine("处理器是:" + _computer.Cpu());
strCom.AppendLine("显卡是:" + _computer.PhenoType());
strCom.AppendLine("内存是:" + _computer.Memory());
strCom.AppendLine("硬盘是:" + _computer.HardDisk());
strCom.AppendLine("显示器是:" + _computer.Display());
strCom.AppendLine("己组装完成");
return strCom.ToString();
}
}
Then call it:
public partial class Run : Form
{
public Run()
{
InitializeComponent();
}
private void btnRun_Click(object sender, EventArgs e)
{
BuyComputer myBuy = new BuyComputer(new lenovo());
rtbResult.AppendText(myBuy.ShowComputerConfigure());
myBuy = new BuyComputer(new HP());
rtbResult.AppendText(myBuy.ShowComputerConfigure());
}
}
Results are as follows:
Your computer configuration is as follows:
motherboard is: ASUS 880G series
processors: Sempron binuclear 180 (2.4GHz)
card is: integrated high-performance graphics
memory is: 1G DDRIII
hard is: 500G
display is: 19-inch widescreen LCD monitor
hexyl assembly complete
your computer configuration is as follows:
motherboard is: ATI RS482
processor: Athlon 64 5000 + x2 dual core
chip is: NV G310 512M
memory is: 2G DDR2 667
drives are: 3200G
display is: 19-inch widescreen LCD monitor
hexyl assembly carry out
Summary: Strategy pattern and abstract factory pattern is like, except that the multi-mode strategy a unified interface class, here is BuyComputer.
Welcome Paizhuan.
Reproduced in: https: //www.cnblogs.com/springyangwc/archive/2011/04/02/2002809.html