從一個(gè)輿論調(diào)查的制作談面向?qū)ο蟮木幊趟悸罚ㄒ唬?/h1>
發(fā)表時(shí)間:2024-05-15 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]一般的web程序員剛剛轉(zhuǎn)到.net或jsp時(shí),往往編程觀念轉(zhuǎn)不過(guò)來(lái),還是按照以前那種結(jié)構(gòu)化的編程思 路來(lái),而不從面向?qū)ο蟮慕嵌瓤紤],造成業(yè)務(wù)邏輯與頁(yè)面html代碼混雜在一起,一旦頁(yè)面原型改變,相應(yīng)的程序也要修改,這樣造成代碼的可重用性太低。而asp.net或jsp比asp最大的一個(gè)進(jìn)步就是面向?qū)ο?..
一般的web程序員剛剛轉(zhuǎn)到.net或jsp時(shí),往往編程觀念轉(zhuǎn)不過(guò)來(lái),還是按照以前那種結(jié)構(gòu)化的編程思
路來(lái),而不從面向?qū)ο蟮慕嵌瓤紤],造成業(yè)務(wù)邏輯與頁(yè)面html代碼混雜在一起,一旦頁(yè)面原型改變,相應(yīng)
的程序也要修改,這樣造成代碼的可重用性太低。而asp.net或jsp比asp最大的一個(gè)進(jìn)步就是面向?qū)ο螅?/p>
使代碼可重用性達(dá)到最高。作為一個(gè)典型的web程序來(lái)說(shuō),一般把它分為三層比較理想,業(yè)務(wù)層,數(shù)據(jù)層
和頁(yè)面顯示層。下面以一個(gè)輿論調(diào)查的例子來(lái)講一下。
讓我們先來(lái)看一下如果一個(gè)輿論調(diào)查從面向?qū)ο蟮慕嵌葋?lái)考慮應(yīng)該是怎樣的。首先,從數(shù)據(jù)方面來(lái)說(shuō)
,一個(gè)輿論調(diào)查應(yīng)該有一個(gè)調(diào)查主題,然后應(yīng)該有幾個(gè)調(diào)查項(xiàng),最后還應(yīng)該有參加調(diào)查的用戶以及比如調(diào)
查起止時(shí)間等等;其次,從輿論調(diào)查的方法來(lái)說(shuō),很簡(jiǎn)單,一個(gè)投票的方法,然后就是顯示調(diào)查結(jié)果的方
法。知道了上面這些,我們就可以這樣來(lái)構(gòu)造一個(gè)調(diào)查類:
namespace MyClass.Util
{
using System;
using System.Collections ;
using System.Drawing ;
using MyClass.Util ;
/// <summary>
/// 一個(gè)通用的調(diào)查類
/// </summary>
public class Survey : object
{
/// <summary>
/// 調(diào)查編號(hào)
/// </summary>
/// <remarks>
/// 在數(shù)據(jù)庫(kù)中是varchar型,20字節(jié)
/// </remakrs>
protected string m_strID ;
/// <summary>
/// 調(diào)查標(biāo)題
/// </summary>
protected string m_strTitle ;
/// <summary>
/// 調(diào)查開始時(shí)間
/// </summary>
protected DateTime m_datBeginTime ;
/// <summary>
/// 調(diào)查截止時(shí)間
/// </summary>
protected DateTime m_datEndTime ;
/// <summary>
/// 點(diǎn)擊數(shù)
/// </summary>
/// <remarks>
/// 瀏覽人數(shù)
/// </remarks>
protected int m_intHits ;
/// <summary>
/// 調(diào)查項(xiàng)
/// </summary>
protected ArrayList m_arrItems ;
//屬性
/// <summary>
/// 調(diào)查標(biāo)題
/// </summary>
public string Title
{
get
{
return m_strTitle ;
}
set
{
m_strTitle = value ;
}
}
/// <summary>
/// 總共點(diǎn)擊數(shù)
/// </summary>
public int Hits
{
get
{
return m_intHits ;
}
set
{
m_intHits = 0 ;
}
}
/// <summary>
/// 調(diào)查開始時(shí)間屬性
/// </summary>
public DateTime BeginTime
{
get
{
return m_datBeginTime ;
}
set
{
m_datBeginTime = value ;
}
}
/// <summary>
/// 調(diào)查截止時(shí)間屬性
/// </summary>
public DateTime EndTime
{
get
{
return m_datEndTime ;
}
set
{
m_datEndTime = value ;
}
}
/// <summary>
/// 調(diào)查項(xiàng)集合
/// </summary>
/// <remarks>是一個(gè)SurveyItem類的集合</remarks>
public ArrayList Items
{
get
{
return m_arrItems ;
}
set
{
m_arrItems = value ;
}
}
/// <summary>
/// 調(diào)查編號(hào)
/// </summary>
public string SurveyID
{
get
{
return m_strID ;
}
set
{
m_strID = value ;
}
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public Survey()
{
//
// TODO: Add Constructor Logic here
//
m_strTitle = "" ;
m_arrItems = new ArrayList() ;
}
/// <summary>
/// 重載構(gòu)造函數(shù)
/// </summary>
/// <param name="a_strTitle">調(diào)查標(biāo)題 </param>
/// <remarks>適用于沒(méi)有截止時(shí)間的調(diào)查</remarks>
public Survey(string a_strTitle)
{
m_strTitle = a_strTitle ;
m_datBeginTime = DateTime.Today ;
m_datEndTime = DateTime.Today ;
m_arrItems = new ArrayList() ;
}
/// <summary>
/// 重載構(gòu)造函數(shù)
/// </summary>
/// <param name="a_strTitle">調(diào)查標(biāo)題 </param>
/// <param name="a_datBeginTime">開始時(shí)間 </param>
/// <param name="a_datEndTime">結(jié)束時(shí)間 </param>
/// <remarks>適用于有截止時(shí)間的調(diào)查</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>
/// 增加調(diào)查項(xiàng)
/// </summary>
/// <param name="a_objItem"> </param>
public void AddItem(object a_objItem)
{
if (a_objItem is SurveyItem)
{
m_arrItems.Add(a_objItem) ;
}
}
/// <summary>
/// 虛函數(shù)
/// </summary>
/// <param name="a_intID">該項(xiàng)編號(hào) </param>
public virtual void Vote(int a_intID)
{
}
/// <summary>
/// 虛函數(shù),保存到數(shù)據(jù)庫(kù)
/// </summary>
/// <param name="a_objItem"> </param>
public virtual void SaveItem(object a_objItem)
{
}
/// <summary>
/// 虛函數(shù),保存調(diào)查到數(shù)據(jù)庫(kù)
/// </summary>
public virtual void SaveToDatabase(string m_strSurveyID)
{
throw(new Exception("基類函數(shù)不能直接使用")) ;
}
/// <summary>
/// 從數(shù)據(jù)庫(kù)中取得調(diào)查及調(diào)查項(xiàng)
/// </summary>
/// <param name="a_intID">對(duì)應(yīng)數(shù)據(jù)庫(kù)中的id </param>
public virtual void LoadFromDatabase(string a_strID)
{
}
/// <summary>
/// 查找調(diào)查項(xiàng)
/// </summary>
/// <param name="a_intID">調(diào)查項(xiàng)編號(hào) </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("調(diào)查項(xiàng)下標(biāo)越界")) ;
}
}
/// <summary>
/// 查找調(diào)查項(xiàng)
/// </summary>
/// <param name="a_strText">調(diào)查項(xiàng)標(biāo)題 </param>
/// <remarks>根據(jù)標(biāo)題查找調(diào)查項(xiàng),找到就返回它的序號(hào),否則就返回-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>
/// 查找調(diào)查項(xiàng)
/// </summary>
/// <param name="a_intID">調(diào)查項(xiàng)編號(hào) </param>
/// <remarks>
/// 根據(jù)調(diào)查項(xiàng)標(biāo)號(hào)來(lái)查找調(diào)查項(xiàng),如果找到返回序號(hào),否則返回-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>
/// 顯示結(jié)果
/// </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)
{
//定義一個(gè)顏色數(shù)組
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++)
{
//調(diào)查項(xiàng)
SurveyItem item = (SurveyItem)m_arrItems[i] ;
//圖表項(xiàng)
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>
/// 調(diào)查項(xiàng)類
/// </summary>
public class SurveyItem : object
{
/// <summary>
/// 調(diào)查項(xiàng)編號(hào),對(duì)應(yīng)將來(lái)數(shù)據(jù)庫(kù)中的id
/// </summary>
protected int m_intID ;
/// <summary>
/// 調(diào)查項(xiàng)標(biāo)題
/// </summary>
protected string m_strText ;
/// <summary>
/// 調(diào)查項(xiàng)描述
/// </summary>
protected string m_strDescription ;
/// <summary>
/// 調(diào)查項(xiàng)數(shù)量
/// </summary>
protected int m_intCount ;
public int ID
{
get
{
return m_intID ;
}
set
{
m_intID = value ;
}
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public SurveyItem()
{
m_strText = "" ;
m_strDescription = "" ;
m_intCount = 0 ;
}
//屬性
/// <summary>
/// 調(diào)查項(xiàng)標(biāo)題屬性
/// </summary>
public string Text
{
get
{
return m_strText ;
}
set
{
m_strText = value ;
}
}
/// <summary>
/// 調(diào)查項(xiàng)描述屬性
/// </summary>
public string Description
{
get
{
return m_strDescription ;
}
set
{
m_strDescription = value ;
}
}
/// <summary>
/// 調(diào)查項(xiàng)數(shù)量屬性
/// </summary>
public int Count
{
get
{
return m_intCount ;
}
set
{
m_intCount = value ;
}
}
//函數(shù)
/// <summary>
/// 重載構(gòu)造函數(shù)
/// </summary>
/// <param name="a_intID">調(diào)查項(xiàng)編號(hào) </param>
/// <param name="a_strText">調(diào)查項(xiàng)標(biāo)題 </param>
/// <param name="a_strDescription">調(diào)查項(xiàng)描述 </param>
/// <param name="a_intCount">調(diào)查項(xiàng)數(shù)量 </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 ;
}
}
}
一個(gè)基本的調(diào)查類就做好了,這個(gè)可以作為業(yè)務(wù)層。但是你可以發(fā)現(xiàn)實(shí)際上這個(gè)類現(xiàn)在什么都做不了,原
因很簡(jiǎn)單,因?yàn)闆](méi)有同數(shù)據(jù)庫(kù)掛接。眾所周知,沒(méi)有數(shù)據(jù)庫(kù)的支持什么都是白搭,那么,我們現(xiàn)在如何來(lái)
掛接數(shù)據(jù)庫(kù),也就是做數(shù)據(jù)層呢?