• OO的感觉


    看了这篇文章《使用了继承、多态还有工厂模式和反射,但是还是没有OO的感觉》。
    第一感觉就是作者对OO的理解和运用都不够深入。他给的例子好像也不是很OO,可惜不知道作者的邮箱,只能将改正后的代码贴在自己的blog中了。
    namespace Lin
    {
        
    //"使用了继承、多态还有工厂模式和反射,但是还是没有OO的感觉"的回复
        public interface IMessageReceive
        
    {
            
    bool SaveMessage(MessageInfo message);
            
    //其它省略
        }


        
    public class OAMessageReceive : IMessageReceive
        
    {
            
    public bool SaveMessage(MessageInfo message)
            
    {
                
    if (message.Content.StartsWith("OA"))
                
    {
                    
    this.Save(message);
                    
    return true;
                }

                
    return false;
            }

            
    private void Save(MessageInfo message)
            
    {
                
    //保存信息到OA
            }

        }

        
    public class ERPMessageReceive : IMessageReceive
        
    {
            
    public bool SaveMessage(MessageInfo message)
            
    {
                
    if (message.Content.StartsWith("ERP"))
                
    {
                    
    this.Save(message);
                    
    return true;
                }

                
    return false;
            }

            
    private void Save(MessageInfo message)
            
    {
                
    //保存信息到ERP
            }

        }


        
    public class MessageInfo
        
    {
            
    public string Mobile
            
    {
                
    get return _mobile; }
                
    set { _mobile = value; }
            }

            
    public DateTime ReceiveDate
            
    {
                
    get return _receiveDate; }
                
    set { _receiveDate = value; }
            }

            
    public string Content
            
    {
                
    get return _content; }
                
    set { _content = value; }
            }


            
    private string _mobile = string.Empty;
            
    private DateTime _receiveDate;
            
    private string _content = string.Empty;

        }


        
    public class MessageReceiveManager
        
    {
            
    public static void Execute()
            
    {
                List
    <IMessageReceive> messageReceiveList = new List<IMessageReceive>();//这里从配置文件读取
                List<MessageInfo> msgList = new List<MessageInfo>();//这里从数据库读取
                List<MessageInfo> errorList = new List<MessageInfo>();
                
    foreach (MessageInfo item in msgList)
                
    {
                    
    bool result = false;
                    
    foreach (IMessageReceive handler in messageReceiveList)
                    
    {
                        
    if (handler.SaveMessage(item))
                        
    {
                            result 
    = true;
                            
    break;
                        }

                    }

                    
    if (!result)//没能被处理
                        errorList.Add(item);
                }

            }

        }


    }

    还有一种
    public class MessageReceiveFactory
        
    {
            
    public static IMessageReceive CreateMessageReceive(string content)
            
    {
                
    //这里使用反射,我这里直接使用IF
                if (content.StartsWith("OA"))
                    
    return new OAMessageReceive();
                
    else if (content.StartsWith("ERP"))
                    
    return new ERPMessageReceive();

                
    throw new Exception("未能找到正确的处理者");
                
            }

        }




       
    /// <summary>
            
    /// 方法2
            
    /// </summary>

            public static void Execute2()
            
    {
                List
    <MessageInfo> msgList = new List<MessageInfo>();//这里从数据库读取
                foreach (MessageInfo item in msgList)
                
    {
                    IMessageReceive handler 
    = MessageReceiveFactory.CreateMessageReceive(item.Content);
                    handler.SaveMessage(item);
                }

            }

    OO主要是为了变化。如果没有变化或者变化频率很小,个人觉得这时就不能体现OO的强大。

    获取处理短信对象
    public class Helper
    {
        
    private static string path=string.Empty;
        
    private static List<IMessageReceive> lst;
        
    private static object obj = new object();
        
    public static List<IMessageReceive> GetHandlers()
        
    {
            
    if (lst == null)
            
    {
                
    lock (obj)
                
    {
                    
    if (lst == null)
                    
    {
                        lst 
    = new List<IMessageReceive>();
                        path 
    = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["MessageConfig"]);

                        XmlDocument xmlDoc 
    = new XmlDocument();
                        xmlDoc.Load(path);
                        XmlNodeList list 
    = xmlDoc.SelectNodes("/*/add");
                        
    foreach (XmlNode node in list)
                        
    {
                            
    string TypeName = node.Attributes["Type"].Value;
                            IMessageReceive handler;
                            
    try
                            
    {
                                handler 
    = Type.GetType(TypeName).Assembly.CreateInstance(TypeName) as IMessageReceive;
                                lst.Add(handler);
                            }

                            
    catch
                            
    {
                                
    continue;
                            }

                        }

                    }

                }

            }

            
    return lst;
        }

    }
  • 相关阅读:
    验证码生成程序
    会话技术Cookie&Session
    jsp基础、el技术、jstl标签、javaEE的开发模式
    js原生的Ajax
    java基础-反射
    hibernate 联合主键 composite-id
    在ubuntu18 安装nginx过程,以及遇到的错误!
    报错解决:error: this statement may fall through [-Werror=implicit-fallthrough=]
    nginx.conf中关于nginx-rtmp-module配置指令详解
    搭建Nginx+nginx-rtmp-module的hls流媒体服务器并用OBS进行推流
  • 原文地址:https://www.cnblogs.com/Linjianyu/p/1046875.html
Copyright © 2020-2023  润新知