从一个舆论调查的制作谈面向对象的编程思路(一)
2024-07-21 02:16:40
供稿:网友
 
网站运营seo文章大全提供全面的站长运营经验及seo技术!    一般的web程序员刚刚转到.net或jsp时,往往编程观念转不过来,还是按照以前那种结构化的编程思
路来,而不从面向对象的角度考虑,造成业务逻辑与页面html代码混杂在一起,一旦页面原型改变,相应
的程序也要修改,这样造成代码的可重用性太低。而asp.net或jsp比asp最大的一个进步就是面向对象,
使代码可重用性达到最高。作为一个典型的web程序来说,一般把它分为三层比较理想,业务层,数据层
和页面显示层。下面以一个舆论调查的例子来讲一下。
    让我们先来看一下如果一个舆论调查从面向对象的角度来考虑应该是怎样的。首先,从数据方面来说
,一个舆论调查应该有一个调查主题,然后应该有几个调查项,最后还应该有参加调查的用户以及比如调
查起止时间等等;其次,从舆论调查的方法来说,很简单,一个投票的方法,然后就是显示调查结果的方
法。知道了上面这些,我们就可以这样来构造一个调查类:
namespace myclass.util
{
    using system;
    using system.collections ;
    using system.drawing ;
    using myclass.util ;
    /// <summary>
    ///    一个通用的调查类
    /// </summary>
    public class survey : object
    {
        /// <summary>
        /// 调查编号
        /// </summary>
        /// <remarks>
        /// 在数据库中是varchar型,20字节
        /// </remakrs>
        protected string m_strid ;
        /// <summary>
        /// 调查标题
        /// </summary>
        protected string m_strtitle ;
        /// <summary>
        /// 调查开始时间
        /// </summary>
        protected datetime m_datbegintime ;
        /// <summary>
        /// 调查截止时间
        /// </summary>
        protected datetime m_datendtime ;
        /// <summary>
        /// 点击数
        /// </summary>
        /// <remarks>
        /// 浏览人数
        /// </remarks>
        protected int m_inthits ;
        /// <summary>
        /// 调查项
        /// </summary>
        protected arraylist m_arritems ;
        //属性
        /// <summary>
        /// 调查标题
        /// </summary>
        public string title
        {
            get
            {
                return m_strtitle ;
            }
            set
            {
                m_strtitle = value ;
            }
        }
        /// <summary>
        /// 总共点击数
        /// </summary>
        public int hits
        {
            get
            {
                return m_inthits ;
            }
            set
            {
                m_inthits = 0 ;
            }
        }
        /// <summary>
        /// 调查开始时间属性
        /// </summary>
        public datetime begintime
        {
            get
            {
                return m_datbegintime ;
            }
            set
            {
                m_datbegintime = value ;
            }
        }
        /// <summary>
        /// 调查截止时间属性
        /// </summary>
        public datetime endtime
        {
            get
            {
                return m_datendtime ;
            }
            set
            {
                m_datendtime = value ;
            }
        }
        /// <summary>
        /// 调查项集合
        /// </summary>
        /// <remarks>是一个surveyitem类的集合</remarks>
        public arraylist items
        {
            get
            {
                return m_arritems ;
            }
            set
            {
                m_arritems = value ;
            }
        }
        
        /// <summary>
        /// 调查编号
        /// </summary>
        public string surveyid 
        {
            get
            {
                return m_strid ;
            }
            set
            {
                m_strid = value ;
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        public survey()
        {
            //
            // todo: add constructor logic here
            //
            m_strtitle = "" ;
            m_arritems = new arraylist() ;
        }
        /// <summary>
        /// 重载构造函数
        /// </summary>
        /// <param name="a_strtitle">调查标题 </param>
        /// <remarks>适用于没有截止时间的调查</remarks>
        public survey(string a_strtitle)
        {
            m_strtitle = a_strtitle ;
            m_datbegintime = datetime.today ;
            m_datendtime = datetime.today ;
            m_arritems = new arraylist() ;
        }
        /// <summary>
        /// 重载构造函数
        /// </summary>
        /// <param name="a_strtitle">调查标题 </param>
        /// <param name="a_datbegintime">开始时间 </param>
        /// <param name="a_datendtime">结束时间 </param>
        /// <remarks>适用于有截止时间的调查</remarks>
        public survey(string a_strtitle , datetime a_datbegintime , datetime 
a_datendtime)
        {
            m_strtitle = a_strtitle ;
            m_datbegintime = a_datbegintime ;
            m_datendtime = a_datendtime ;
        }
        /// <summary>
        /// 增加调查项
        /// </summary>
        /// <param name="a_objitem"> </param>
        public void additem(object a_objitem)
        {
            if (a_objitem is surveyitem)
            {
                m_arritems.add(a_objitem) ;
            }
        }
        /// <summary>
        /// 虚函数
        /// </summary>
        /// <param name="a_intid">该项编号 </param>
        public virtual void vote(int a_intid) 
        {
            
        }
        /// <summary>
        /// 虚函数,保存到数据库
        /// </summary>
        /// <param name="a_objitem"> </param>
        public virtual void saveitem(object a_objitem)
        {
            
        }
        /// <summary>
        /// 虚函数,保存调查到数据库
        /// </summary>
        public virtual void savetodatabase(string m_strsurveyid)
        {
            throw(new exception("基类函数不能直接使用")) ;
        }
        /// <summary>
        ///  从数据库中取得调查及调查项
        /// </summary>
        /// <param name="a_intid">对应数据库中的id </param>
        public virtual void loadfromdatabase(string a_strid)
        {
            
        }
        /// <summary>
        /// 查找调查项
        /// </summary>
        /// <param name="a_intid">调查项编号 </param>
        public surveyitem getitem(int a_intid)
        {
            if (a_intid > 0 && a_intid < m_arritems.count)
            {
                return (surveyitem)(m_arritems[a_intid]) ;
            }
            else
            {
                throw(new exception("调查项下标越界")) ;
            }
        }
        
        /// <summary>
        /// 查找调查项
        /// </summary>
        /// <param name="a_strtext">调查项标题 </param>
        /// <remarks>根据标题查找调查项,找到就返回它的序号,否则就返回-1</remarks>
        public int indexof(string a_strtext)
        {
            for (int i = 0 ; i < m_arritems.count ; i ++)
            {
                if (a_strtext == ((surveyitem)m_arritems[i]).text)
                {
                    return i ;
                }
            }
            return -1 ;
        }
        /// <summary>
        /// 查找调查项
        /// </summary>
        /// <param name="a_intid">调查项编号 </param>
        /// <remarks>
        ///  根据调查项标号来查找调查项,如果找到返回序号,否则返回-1</remarks>
        public int indexof(int a_intid)
        {
            for ( int i = 0 ; i < m_arritems.count ; i ++)
            {
                if (a_intid == ((surveyitem)m_arritems[i]).id)
                {
                    return i ;
                }
            }
            return - 1 ;
        }
        /// <summary>
        /// 显示结果
        /// </summary>
        /// <param name="a_strfilename">生成图片的保存位置 </param>
        public void createresultimage(string a_strfilename , mychart.charttype 
a_intcharttype , 
                                int a_intwidth , int 
a_intheight , color a_objbackcolor)
        {
            //定义一个颜色数组
            arraylist itemcolors = new arraylist() ;
            itemcolors.add(color.red) ;
            itemcolors.add(color.black) ;
            itemcolors.add(color.blue) ;
            itemcolors.add(color.deepskyblue) ;
            itemcolors.add(color.firebrick) ;
            itemcolors.add(color.orange) ;
            itemcolors.add(color.green) ;
            itemcolors.add(color.whitesmoke) ;
            itemcolors.add(color.tan) ;
            itemcolors.add(color.darkseagreen) ;
            itemcolors.add(color.fuchsia) ;
            itemcolors.add(color.goldenrod) ;
            mychart mychart = new mychart(m_strtitle , a_objbackcolor , 
a_intwidth , 
                                a_intheight , a_intcharttype 
, "人次") ;
            for (int i = 0 ; i < m_arritems.count ; i++)
            {
                //调查项
                surveyitem item = (surveyitem)m_arritems[i] ;
                //图表项
                mychart.additem(new chartitem(item.text , item.count , 
(color)itemcolors[i])) ;
 
            }
            try
            {
                mychart.create(a_strfilename) ;
            }
            catch(exception e)
            {
                throw(new exception(e.tostring())) ;
            }
        }
        
    }
    /// <summary>
    /// 调查项类
    /// </summary>
    public class surveyitem : object
    {
        /// <summary>
        /// 调查项编号,对应将来数据库中的id
        /// </summary>
        protected int m_intid ;
        /// <summary>
        /// 调查项标题
        /// </summary>
        protected string m_strtext ;
        /// <summary>
        /// 调查项描述
        /// </summary>
        protected string m_strdescription ;
        /// <summary>
        /// 调查项数量
        /// </summary>
        protected int m_intcount ;
        public int id
        {
            get
            {
                return m_intid ;
            }
            set
            {
                m_intid = value ;
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        public surveyitem()
        {
            m_strtext = "" ;
            m_strdescription = "" ;
            m_intcount = 0 ;
        }
        //属性
        /// <summary>
        /// 调查项标题属性
        /// </summary>
        public string text
        {
            get
            {
                return m_strtext ;
            }
            set
            {
                m_strtext = value ;
            }
        }
        /// <summary>
        /// 调查项描述属性
        /// </summary>
        public string description
        {
            get
            {
                return m_strdescription ;
            }
            set
            {
                m_strdescription = value ;
            }
        }
        /// <summary>
        /// 调查项数量属性
        /// </summary>
        public int count
        {
            get
            {
                return m_intcount ;
            }
            set
            {
                m_intcount = value ;
            }
        }
        //函数
        /// <summary>
        /// 重载构造函数
        /// </summary>
        /// <param name="a_intid">调查项编号 </param>
        /// <param name="a_strtext">调查项标题 </param>
        /// <param name="a_strdescription">调查项描述 </param>
        /// <param name="a_intcount">调查项数量 </param>
        public surveyitem(int a_intid , string a_strtext , 
                        string a_strdescription , int a_intcount)
        {
            m_intid = a_intid ;
            m_strtext = a_strtext ;
            m_strdescription = a_strdescription ;
            m_intcount = a_intcount ;
        }
        public surveyitem(string a_strtext)
        {
            m_strtext = a_strtext ;
        }
    }
}
一个基本的调查类就做好了,这个可以作为业务层。但是你可以发现实际上这个类现在什么都做不了,原
因很简单,因为没有同数据库挂接。众所周知,没有数据库的支持什么都是白搭,那么,我们现在如何来
挂接数据库,也就是做数据层呢?