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

設計模式(C#) - 享元模式(Flyweight Pattern)(3)

編輯:關於C語言

XMLMessage

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

namespace Pattern.Flyweight
{
  /**//// <summary>
  /// XML方式操作Message(ConcreteFlyweight)
  /// </summary>
  public class XMLMessage : AbstractMessage
  {
    /**//// <summary>
    /// 獲取Message
    /// </summary>
    /// <returns></returns>
    public override 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 override bool Insert(MessageModel mm)
    {
      // 代碼略
      return true;
    }
  }
}

MessageFactory

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

namespace Pattern.Flyweight
{
  /**//// <summary>
  /// Message工廠(FlyweightFactory)
  /// </summary>
  public class MessageFactory
  {
    private Dictionary<string, AbstractMessage> _messageObjects = new Dictionary<string, AbstractMessage>();

    /**//// <summary>
    /// 獲取Message對象
    /// </summary>
    /// <param name="key">key</param>
    /// <returns></returns>
    public AbstractMessage GetMessageObject(string key)
    {
      AbstractMessage messageObject = null;

      if (_messageObjects.ContainsKey(key))
      {
        messageObject = _messageObjects[key];
      }
      else
      {
        switch (key)
        {
          case "XML": messageObject = new SqlMessage(); break;
          case "sql": messageObject = new XMLMessage(); break;
        }

        _messageObjects.Add(key, messageObject);
      }

      return messageObject;
    }
  }
}

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