程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 設計模式(C#) - 建造者模式(Builder Pattern)(3)

設計模式(C#) - 建造者模式(Builder Pattern)(3)

編輯:關於C語言

XMLMessage

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Builder
{
  /**//// <summary>
  /// XML方式操作Message
  /// </summary>
  public class XMLMessage
  {
    /**//// <summary>
    /// 獲取Message
    /// </summary>
    /// <returns></returns>
    public List<MessageModel> Get()
    {
      List<MessageModel> l = new List<MessageModel>();
      l.Add(new MessageModel("XML方式獲取Message", DateTime.Now));

      return l;
    }

    /**//// <summary>
    /// 插入Message
    /// </summary>
    /// <param name="mm">Message實體對象</param>
    /// <returns></returns>
    public bool Insert(MessageModel mm)
    {
      // 代碼略
      return true;
    }
  }
}

Operation

using System;
using System.Collections.Generic;
using System.Text;

using System.Reflection;

namespace Pattern.Builder
{
  /**//// <summary>
  /// 操作(Product)
  /// </summary>
  public class Operation
  {
    private string _type;
    private Dictionary<string, string> _dictionary;

    /**//// <summary>
    /// 構造函數
    /// </summary>
    /// <param name="type">產品類型</param>
    public Operation(string type)
    {
      _dictionary = new Dictionary<string, string>();
      this._type = type;
    }

    /**//// <summary>
    /// 索引器
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public string this[string key]
    {
      get { return _dictionary[key]; }
      set { _dictionary[key] = value; }
    }

    /**//// <summary>
    /// 獲得結果
    /// </summary>
    /// <returns></returns>
    public string GetResult()
    {
      Assembly assembly = Assembly.Load("Pattern.Builder");

      MethodInfo methodGet = assembly.GetType("Pattern.Builder." + _dictionary["get"].Split('|')[0]).GetMethod(_dictionary["get"].Split('|')[1]);
      object objGet = methodGet.Invoke(assembly.CreateInstance("Pattern.Builder." + _dictionary["get"].Split('|')[0]), null);
      List<MessageModel> m = (List<MessageModel>)objGet;

      MethodInfo methodInsert = assembly.GetType("Pattern.Builder." + _dictionary["insert"].Split('|')[0]).GetMethod(_dictionary["insert"].Split('|')[1]);
      object objInsert = methodInsert.Invoke(assembly.CreateInstance("Pattern.Builder." + _dictionary["insert"].Split('|')[0]), new object[] { new MessageModel(_dictionary["insert"].Split('|')[2], Convert.ToDateTime(_dictionary["insert"].Split('|')[3])) });
      bool b = (bool)objInsert;

      return "類型為" + this._type + "的執行結果:<br />" + b.ToString() + "<br />" + m[0].Message + " " + m[0].PublishTime.ToString() + "<br />";
    }
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved