今天学习了Strategy模式,自己模仿写了一个,不知道算不算Strategy模式。 代码在下面http://student.csdn.net/space.php?uid=916701&do=blog&id=39577 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System; using System.Collections.Generic; using System.Text; namespace StrategyExample1 { public interface ICPU { string Insert(); } } namespace StrategyExample1 { public interface IMemory { string Insert(); } } namespace StrategyExample1 { public class IntelCPU:ICPU { public string Insert() { return "IntelCPU"; } } } namespace StrategyExample1 { public class AMDCPU:ICPU { public string Insert() { return "AMDCPU"; } } } namespace StrategyExample1 { public class HynixMemory:IMemory { public string Insert() { return "现代内存"; } } } namespace StrategyExample1 { class KingStonMemory:IMemory { public string Insert() { return "金士顿内存"; } } } namespace StrategyExample1 { public class PersonalComputer { private ICPU _cpu; private IMemory _memory; public void InsertCPU(ICPU cpu) { _cpu = cpu; } public void InsertMemory(IMemory memory) { _memory = memory; } public string GetPCInfo() { return "CPU:"+_cpu.Insert() + "\r\n内存:"+_memory.Insert(); } } } namespace StrategyExample1 { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { PersonalComputer pc1 = new PersonalComputer(); pc1.InsertCPU(new IntelCPU()); pc1.InsertMemory(new HynixMemory()); MessageBox.Show(pc1.GetPCInfo()); PersonalComputer pc2 = new PersonalComputer(); pc2.InsertCPU(new AMDCPU()); pc2.InsertMemory(new KingStonMemory()); MessageBox.Show(pc2.GetPCInfo()); } } } 如果你不去提任何模式术语,然后让一些设计师去实实在在地看大部分人自认为抄袭脱销设计模式的讲师写的代码,他们会有不少人像我一样有“脱裤子放屁”的感觉。当你强调这是什么什么模式,也许别人不好意思批评它,但是不是因为所谓的设计模式有多清晰和准确,而是因为当时java语言处于最低级水平(比当时的c++要低级,只不过去掉了指针比较干净而已),所以GOF不懂许多最近10几年才在java中出现的新特性,因此设计模式的代码范例中有太多的脱裤子放屁的设计了。 策略模式在大话设计模式里使用了商场收银软件打折计算使用策略模式。我觉得那样使用和楼主的使用藐视有些区别,大话的例子比较实用而楼主是套上去的。所以我认为代码像以下方式写就可以了:public class PersonalComputer { private string _cpuName; private string _memoryName; public PersonalComputer(string cpuName,string memoryName) { _cpuName = cpuName; _memoryName = memoryName; } public string GetPCInfo() { return "CPU:"+_cpu+ "\r\n内存:"+_memory; } } static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { PersonalComputer pc1 = new PersonalComputer ('InterCPU','HynixMemory'); MessageBox.Show(pc1.GetPCInfo()); PersonalComputer pc2 = new PersonalComputer('AmdCPU','KingMaxMemory'); MessageBox.Show(pc2.GetPCInfo()); } } 也许我只有小菜的水平,但是sp1234大神说的是不要刻意去套设计模式。自然一点也是可以的。呵呵 无法找到user32.dll 如何建支持SSL的Web Service? 上传文件后文件夹属性为什么会自动变为只读的??? 这样的时间如何比较? C# 方法中的参数语法 "=>",是怎么回事。进来看看。 C# class 实例作参数问题 如何实现拼音查询数据库 无法将类型为“System.Data.DataView”的对象强制转换为类型“System.Data.DataTable” 在线求救! 怎样调试存储过程?急!:< 树形结构的数据,应该怎样设计表? 关于c# 加载Dll路径的问题
using System;
using System.Collections.Generic;
using System.Text;
namespace StrategyExample1
{
public interface ICPU
{
string Insert();
}
}
namespace StrategyExample1
{
public interface IMemory
{
string Insert();
}
}
namespace StrategyExample1
{
public class IntelCPU:ICPU
{
public string Insert()
{
return "IntelCPU";
}
}
}
namespace StrategyExample1
{
public class AMDCPU:ICPU
{
public string Insert()
{
return "AMDCPU";
}
}
}
namespace StrategyExample1
{
public class HynixMemory:IMemory
{
public string Insert()
{
return "现代内存";
}
}
}
namespace StrategyExample1
{
class KingStonMemory:IMemory
{
public string Insert()
{
return "金士顿内存";
}
}
}
namespace StrategyExample1
{
public class PersonalComputer
{
private ICPU _cpu;
private IMemory _memory;
public void InsertCPU(ICPU cpu)
{
_cpu = cpu;
}
public void InsertMemory(IMemory memory)
{
_memory = memory;
}
public string GetPCInfo()
{
return "CPU:"+_cpu.Insert() + "\r\n内存:"+_memory.Insert();
}
}
}
namespace StrategyExample1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
PersonalComputer pc1 = new PersonalComputer();
pc1.InsertCPU(new IntelCPU());
pc1.InsertMemory(new HynixMemory());
MessageBox.Show(pc1.GetPCInfo());
PersonalComputer pc2 = new PersonalComputer();
pc2.InsertCPU(new AMDCPU());
pc2.InsertMemory(new KingStonMemory());
MessageBox.Show(pc2.GetPCInfo());
}
}
}
{
private string _cpuName;
private string _memoryName;
public PersonalComputer(string cpuName,string memoryName)
{
_cpuName = cpuName;
_memoryName = memoryName;
}
public string GetPCInfo()
{
return "CPU:"+_cpu+ "\r\n内存:"+_memory;
}
} static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
PersonalComputer pc1 = new PersonalComputer ('InterCPU','HynixMemory');
MessageBox.Show(pc1.GetPCInfo());
PersonalComputer pc2 = new PersonalComputer('AmdCPU','KingMaxMemory');
MessageBox.Show(pc2.GetPCInfo());
}
}
也许我只有小菜的水平,但是sp1234大神说的是不要刻意去套设计模式。自然一点也是可以的。呵呵