解决方案 »

  1.   


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

  2.   

    如果你不去提任何模式术语,然后让一些设计师去实实在在地看大部分人自认为抄袭脱销设计模式的讲师写的代码,他们会有不少人像我一样有“脱裤子放屁”的感觉。当你强调这是什么什么模式,也许别人不好意思批评它,但是不是因为所谓的设计模式有多清晰和准确,而是因为当时java语言处于最低级水平(比当时的c++要低级,只不过去掉了指针比较干净而已),所以GOF不懂许多最近10几年才在java中出现的新特性,因此设计模式的代码范例中有太多的脱裤子放屁的设计了。
      

  3.   

    策略模式在大话设计模式里使用了商场收银软件打折计算使用策略模式。我觉得那样使用和楼主的使用藐视有些区别,大话的例子比较实用而楼主是套上去的。所以我认为代码像以下方式写就可以了: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大神说的是不要刻意去套设计模式。自然一点也是可以的。呵呵