现在的位置: 首页 > 综合 > 正文

策略模式+反射 策略模式+反射

2017年09月28日 ⁄ 综合 ⁄ 共 7121字 ⁄ 字号 评论关闭
 

策略模式+反射

分类: 设计模式 294人阅读 评论(24) 收藏 举报

        开放-封闭原则:软件实体(类、模块、函数等等)应该可以扩展,但是不可修改。也就是对于扩展开放的,对于更改是封闭的。

      学习设计模式的时候对这句话的记忆很深,而且“封装变化”这句话更是记忆深刻,但是也只是记忆深刻,这次做考试系统的时候是真的深刻体会到了。整个考试系统的变化点就是题型,不同的考试会有不同的题型,不同的题型需要不同的字段。这样我们就需要把题型的变化封装起来,当我们添加一个新的题型的时候,尽量不更改原来的系统。初步的设想是将不同的题型的封装到不同DLL文件里面,当需要添加一个题型的时候只需要导入一个DLL文件即可,而不影响其他部分。

        以上是需求,我们采用的是策略模式+反射。将各种题型封装成DLL文件,这样就把变化封装起来。

    下面看图看代码:

  

  1. <span style="font-size:18px">///////////////////////////////////////////////////////////  
  2. //  QuestionManageBLL.cs  
  3. //作者: 王永俊  
  4. //小组:提高班-考试系统3.0版-教师端  
  5. //说明:试题抽象工厂+反射——策略模式  
  6. //创建日期:4-11月-2013 20:15:14  
  7. //版本号:  
  8. ///////////////////////////////////////////////////////////  
  9. using System;  
  10. using System.Collections.Generic;  
  11. using System.Linq;  
  12. using System.Text;  
  13. using System.Reflection;  
  14. using System.Collections;  
  15. using System.Data;  
  16. using System.Data.SqlClient;  
  17. using ExamSystemV3.IDAL ;  
  18. //using QuestionManageDAL;  
  19. namespace QuestionManageBLL  
  20. {  
  21.   public class QuestionContextBLL  
  22.     {  
  23.          //获取程序集名称  
  24.         private static readonly string AssemblyName = "QuestionManageDAL";  
  25.         //实例化一个试题公共对象  
  26.         QuestionCommonDAL question = null ;  
  27.   
  28.           
  29.         //利用反射取得具体的题型  
  30.         public  QuestionContextBLL(string questionType)  
  31.         {  
  32.   
  33.             string className = AssemblyName + "." + questionType + "DAL";  
  34.   
  35.             question = (QuestionCommonDAL)Assembly.Load(AssemblyName).CreateInstance(className);  
  36.               
  37.            
  38.         }  
  39.         /// <summary>  
  40.         ///创建题型表  
  41.         /// </summary>  
  42.         /// <param name="courseName">表名</param>  
  43.         public bool CreateTableResult(Hashtable map)  
  44.         {  
  45.             return question.CreateQuestionTable(map);  
  46.         }  
  47.               /// <summary>  
  48.         /// 增加一条数据  
  49.         /// </summary>  
  50.         public bool InsertQuestionResult(Hashtable map)  
  51.         {  
  52.             return question.InsertOneQuestion(map);  
  53.         }  
  54. }  
  55.   
  56. ///////////////////////////////////////////////////////////  
  57. //  QuestionManageBLL.cs  
  58. //作者: 王永俊  
  59. //小组:提高班-考试系统3.0版-教师端  
  60. //说明:试题公共类——策略模式  
  61. //创建日期:3-11月-2013 20:15:14  
  62. //版本号:1.0  
  63. ///////////////////////////////////////////////////////////  
  64. using System;  
  65. using System.Collections.Generic;  
  66. using System.Linq;  
  67. using System.Text;  
  68. using System.Data;  
  69. using System.Data.SqlClient;  
  70. using System.Collections;  
  71. namespace ExamSystemV3.IDAL  
  72. {  
  73.    public abstract class  QuestionCommonDAL  
  74.     {  
  75.        /// <summary>  
  76.         /// 根据课程创建选择题表  
  77.         /// </summary>  
  78.         /// <param name="courseName">哈希表</param>  
  79.        public abstract   bool CreateQuestionTable(Hashtable map);  
  80.   
  81.         /// <summary>  
  82.         /// 增加一条数据  
  83.         /// </summary>  
  84.        public abstract   bool InsertOneQuestion(Hashtable map);  
  85. }  
  86. ///////////////////////////////////////////////////////////  
  87. //  QuestionManageDAL.cs  
  88. //作者: 王永俊  
  89. //小组:提高班-考试系统3.0版-教师端  
  90. //说明:试题公共类——策略模式  
  91. //创建日期:3-11月-2013 20:15:14  
  92. //版本号:1.0  
  93. ///////////////////////////////////////////////////////////  
  94. using System;  
  95. using System.Data;  
  96. using System.Text;  
  97. using System.Data.SqlClient;  
  98. using Maticsoft.DBUtility;//Please add references  
  99. using System.Collections;  
  100. using ExamSystemV3.IDAL ;  
  101. namespace QuestionManageDAL  
  102. {  
  103.    public class XuanZeTiDAL:QuestionCommonDAL  
  104.     {  
  105.        //public XuanZeTiDAL()  
  106.        // {}  
  107.         #region  BasicMethod  
  108.         /// <summary>  
  109.         /// 根据课程创建选择题表  
  110.         /// </summary>  
  111.         /// <param name="courseName">哈希表</param>  
  112.         public override  bool CreateQuestionTable(Hashtable map)  
  113.         {  
  114.             string strSql = @"IF object_id('" + map["TableName"].ToString() + "') IS NULL Create Table " + map["TableName"].ToString() + "(QuestionId varchar(20) ," +  
  115.                   "ChapterId varchar(20) not null," +  
  116.                   "QuestionTypeId varchar(20) not null," +  
  117.                   "Degree int not null," +  
  118.                   "Fraction float not null," +  
  119.                   "QuestionContent text not null," +  
  120.                   "Answer1 text," +  
  121.                   "Answer2 text," +  
  122.                   "Answer3 text," +  
  123.                   "Answer4 text," +  
  124.                   "CorrectAnswer text," +  
  125.                   "IsValid varchar(10)," +  
  126.                   "AddUser varchar(20)," +  
  127.                   "Timestamp varchar(20)," +  
  128.                   "Remark text," +  
  129.                   "Other1 varchar(50)," +  
  130.                   "Other2 varchar(50) constraint PK_" + map["TableName"].ToString() + " primary key (QuestionId))";  
  131.             int rows = DbHelperSQL.ExecuteSql(strSql.ToString());  
  132.             return true;  
  133.         }  
  134.                       /// <summary>  
  135.         /// 添加一条题库信息  
  136.         /// </summary>  
  137.         public  override  bool InsertOneQuestion(Hashtable map)  
  138.         {  
  139.             StringBuilder strSql = new StringBuilder();  
  140.             strSql.Append("insert into" + map["TableName"].ToString ());  
  141.             strSql.Append("QuestionID,ChapterID,QuestionTypeId,Degree,Fraction,QuestionContent,Answer1,Answer2,Answer3,Answer4,CorrectAnswer,IsValid,AddUser,Remark,other1,other2)");  
  142.             strSql.Append(" values (");  
  143.             strSql.Append("@QuestionID,@ChapterID,@QuestionTypeId,@Degree,@Fraction,@QuestionContent,@Answer1,@Answer2,@Answer3,@Answer4,@Answer5,@Answer6,@Answer7,@CorrectAnswer,@IsValid,@AddUser,@Remark,@other1,@other2)");  
  144.             SqlParameter[] parameters = {  
  145.                     new SqlParameter("@QuestionID",map["QuestionID"].ToString () ),  
  146.                     new SqlParameter("@ChapterID",map["ChapterID"].ToString () ),  
  147.                     new SqlParameter("@QuestionTypeId",map["QuestionTypeId"].ToString () ),  
  148.                     new SqlParameter("@Degree",Convert.ToInt32 ( map["Degree"]) ),  
  149.                     new SqlParameter("@Fraction",Convert.ToDouble ( map["Fraction"] )),  
  150.                     new SqlParameter("@QuestionContent", map["QuestionContent"].ToString () ),  
  151.                     new SqlParameter("@Answer1",map["Answer1"].ToString ()),  
  152.                     new SqlParameter("@Answer2", map["Answer2"] .ToString ()),  
  153.                     new SqlParameter("@Answer3",map["Answer3"].ToString ()),  
  154.                     new SqlParameter("@Answer4",map["Answer4"].ToString ()),  
  155.               
  156.                     new SqlParameter("@CorrectAnswer",map["CorrectAnswer"].ToString () ),  
  157.                     new SqlParameter("@IsValid", map["IsValid"].ToString () ),  
  158.                     new SqlParameter("@AddUser", map["AddUser"].ToString ()),  
  159.                     new SqlParameter("@Remark", map["Remark"].ToString () ),  
  160.                     new SqlParameter("@other1", map["other1"] .ToString ()),  
  161.                     new SqlParameter("@other2", map["other2"] .ToString ())};  
  162.   
  163.               
  164.             int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);  
  165.             if (rows > 0)  
  166.             {  
  167.                 return true;  
  168.             }  
  169.             else  
  170.             {  
  171.                 return false;  
  172.             }  
  173.         }  
  174. }</span>  

      总结:虽然题型不同,但是对题型的操作是一样的,这样我们就可以抽象出来一个

类,然后利用反射根据选择的不同的题型,实例化不同的题型类。这样就将题型的变

封装一起,当添加新题型的时候只需要导入一个DLL文件即可!

抱歉!评论已关闭.