首页 > 代码库 > 随便写点什么

随便写点什么

  1 /// <summary>  2     /// 业务经理  3     /// </summary>  4     public class Manager  5     {  6         private static Manager _singleManager;  7         private Manager()  8         {  9         } 10         public static Manager GetSingelManager() 11         { 12             if (_singleManager == null) 13                 _singleManager = new Manager(); 14             return _singleManager; 15         } 16         public void UpLevel(User user) 17         { 18             if (user.Key.UserType == CardType.Gold) 19             { 20                 Console.WriteLine(user.UserName + ":已经是Gold用户"); 21                 return; 22             } 23             user.Key.UserType += 1; 24             Console.WriteLine(user.UserName + ":升级为" + user.Key.UserType); 25         } 26         public void DownLevel(User user) 27         { 28             if (user.Key.UserType == CardType.Gold) 29             { 30                 user.Key.UserType -= 1; 31                 Console.WriteLine(user.UserName + ":已经降级为" + user.Key.UserType); 32                 return; 33             } 34             Console.WriteLine(user.UserName + ":已经是最低" + user.Key.UserType + "用户"); 35         } 36     } 37     /// <summary> 38     /// 卡片类型 39     /// </summary> 40     public enum CardType 41     { 42         Sliver, 43         Gold, 44     } 45     /// <summary> 46     /// 用户key 47     /// </summary> 48     public class Key 49     { 50         public CardType UserType { get; set; } 51         public string SoleString { get; private set; }//Key的唯一标识 52         public Key(CardType e, string soleString) 53         { 54             UserType = e; 55             SoleString = soleString; 56         } 57         public override string ToString() 58         { 59             return UserType+":"+SoleString; 60         } 61     } 62     /// <summary> 63     /// 用户获取、提交数据要实现的接口 64     /// </summary> 65     public interface IMyDataInterFace 66     { 67         string GetData();//获取数据 68  69         bool PostData(string message);//提交数据 70     } 71     /// <summary> 72     /// 用户类 73     /// </summary> 74     public class User : IMyDataInterFace 75     { 76         public Key Key { get; private set; } 77         public string UserName { get; private set; } 78         public MyServer Server { get; private set; } 79         /// <summary> 80         /// 初始化用户,绑定服务器 81         /// </summary> 82         /// <param name="key"></param> 83         /// <param name="userName"></param> 84         /// <param name="server"></param> 85         public User(Key key, string userName,MyServer server) 86         { 87             Key = key; 88             UserName = userName; 89             this.Server = server; 90         } 91         public string GetData() 92         { 93             return Server.GetMessage(Key); 94         } 95         public bool PostData(string message) 96         { 97             if (Server.PostMessge(Key, message)) 98             { 99                 Console.WriteLine("成功提交数据");100                 return true;101             }102             Console.WriteLine("提交数据失败");103             return false;104         }105     }106     /// <summary>107     /// 服务类108     /// </summary>109     public class MyServer110     {111         private static MyServer _SingleServer;112         public  Dictionary<Key, string> Dictionary { get; private set; }113         private MyServer()114         {115         }116         public static MyServer GetSingleServer()117         {118             if (_SingleServer==null)119                 _SingleServer=new MyServer {Dictionary = new Dictionary<Key, string>()};120             return _SingleServer;121         }122 123         public string GetMessage(Key key)124         {125             var result = string.Empty;126             try127             {128                 if(key.UserType==CardType.Gold)129                    result ="GoleUser:"+ Dictionary[key];130                 else131                 {132                     result = "SliverUser:" + Dictionary[key];133                 }134                 return result;135             }136             catch (Exception)137             {138                 Console.WriteLine(key+"->不存在");//怎么返回key对应的用户139                 return null;140             }141             142         }143         public bool PostMessge(Key key, string message)144         {145             try146             {147                 Dictionary[key] = message;148                 return true;149             }150             catch (Exception)151             {152                 return false;153             }154         }155     }156     class Program157     {158         static  void Main(string[] args)159         {160             var server = MyServer.GetSingleServer();161             var manager = Manager.GetSingelManager();162 163             var key=new Key(CardType.Gold,"33520");164             var user1 = new User(key, "张三",server);165             166             var postSuccess=user1.PostData("hello");167             Console.WriteLine(user1.GetData()); 168 169             manager.DownLevel(user1);170             Console.WriteLine(user1.Key.UserType); 171 172         }173     }

 

随便写点什么