[設計模式練習]享元模式結合工廠模式

[設計模式練習]享元模式結合工廠模式

利用工廠模式建立物件後,利用享元模式管理建立後的物件

類別圖

ClassDiagram2

程式碼

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;

namespace TestObjModel.Model
{
    /// <summary>
    /// 列舉要建立的物件
    /// </summary>
    public enum CheckDBTableTypeEnum
    {
        Date,
        Amount,
        Name,
    };

    class CheckItemBase
    {
        public CheckItemBase()
        {
        }
        public CheckItemBase(object[] inputvalue)
        {
            SetInputValue(inputvalue);
        }
        public void SetInputValue(object[] inputvalue)
        {
            Type objType = this.GetType();//取得物件的型別 
            PropertyInfo[] ps = objType.GetProperties();//依型別取得所有屬性資訊   
            int count = 0;
            foreach (PropertyInfo p in ps)//循繞所有屬性 
            {
                p.SetValue(this, inputvalue[count], null);
                count++;
            }
        }
        /// <summary>
        /// 顯示物件中所有屬性的值
        /// </summary>
        public void DisplayValue()
        {
            Type objType = this.GetType();//取得物件的型別 
            PropertyInfo[] ps = objType.GetProperties();//依型別取得所有屬性資訊               
            StringBuilder sb = new StringBuilder();
            foreach (PropertyInfo p in ps)//循繞所有屬性 
            {
                sb.AppendFormat("{0}:{1}\r\n", p.Name, p.GetValue(this, null));
            }
            Console.Write(sb.ToString());
        }

    }
    /// <summary>
    /// 日期
    /// </summary>
    class Date : CheckItemBase
    {
        public Date() : base() { }
        public Date(object[] value): base(value) { }
        public DateTime startdate { get; set; }
        public DateTime enddate { get; set; }
    }
    /// <summary>
    /// 金額
    /// </summary>
    class Amount : CheckItemBase
    {
        public Amount() : base() { }
        public Amount(object[] value) : base(value) { }
        public decimal outcome { get; set; }
        public decimal income { get; set; }
    }


    /// <summary>
    /// 姓名
    /// </summary>
    class Name : CheckItemBase
    {
        public Name() : base() { }
        public Name(object[] value) : base(value) { }
        public string firstname { get; set; }
        public string lastname { get; set; }
    }
    #region 享元工廠
    class FlyweightCheckItemFactory
    {
        private Hashtable flyweights = new Hashtable();
        public CheckItemBase GetChekItemCategory(CheckDBTableTypeEnum itemtype, object[] value)
        {
            if (flyweights.ContainsKey(itemtype) == false)
            {
                flyweights.Add(itemtype, ObjFactory.CreateCheckItemObj(itemtype, value));
            }
            ((CheckItemBase)flyweights[itemtype]).SetInputValue(value);
            return (CheckItemBase)flyweights[itemtype];
        }
        public CheckItemBase GetChekItemCategory(CheckDBTableTypeEnum itemtype)
        {
            if (flyweights.ContainsKey(itemtype) == false)
            {
                flyweights.Add(itemtype, ObjFactory.CreateCheckItemObj(itemtype));
            }
            return (CheckItemBase)flyweights[itemtype];
        }
        /// <summary>
        /// 設定所有列舉
        /// </summary>
        /// <returns></returns>
        public List<CheckDBTableTypeEnum> GetChekItemList()
        {
            return new List<CheckDBTableTypeEnum>() { CheckDBTableTypeEnum.Date, CheckDBTableTypeEnum.Amount, CheckDBTableTypeEnum.Name };
        }
        /// <summary>
        /// 取得物件總數
        /// </summary>
        /// <returns></returns>
        public int GetTotalItemNumber()
        {
            return flyweights.Count;
        }
    }
    #endregion    
    #region 工廠模式建立物件
    class ObjFactory
    {
        public static CheckItemBase CreateCheckItemObj(CheckDBTableTypeEnum item, object[] inputvalue)
        {
            CheckItemBase createobj = null;
            switch (item)
            {
                case CheckDBTableTypeEnum.Date:
                    createobj = new Date(inputvalue);
                    break;
                case CheckDBTableTypeEnum.Amount:
                    createobj = new Amount(inputvalue);
                    break;
                case CheckDBTableTypeEnum.Name:
                    createobj = new Name(inputvalue);
                    break;
            }
            return createobj;
        }
        public static CheckItemBase CreateCheckItemObj(CheckDBTableTypeEnum item)
        {
            CheckItemBase createobj = null;
            switch (item)
            {
                case CheckDBTableTypeEnum.Date:
                    createobj = new Date();
                    break;
                case CheckDBTableTypeEnum.Amount:
                    createobj = new Amount();
                    break;
                case CheckDBTableTypeEnum.Name:
                    createobj = new Name();
                    break;
            }
            return createobj;
        }
    }
    #endregion
}

主程式

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model = TestObjModel.Model;

namespace TestObjModel
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Model.FlyweightCheckItemFactory flyfactory = new Model.FlyweightCheckItemFactory();
            //建立日期物件
            List<object> datevalue = new List<object>(){DateTime.Now,DateTime.Now.AddDays(1)};
            Model.CheckItemBase dateItem = flyfactory.GetChekItemCategory(Model.CheckDBTableTypeEnum.Date, datevalue.ToArray());
            //建立金額物件
            List<object> amountvalue = new List<object>(){decimal.Parse("10"),decimal.Parse("20")};
            Model.CheckItemBase amountItem = flyfactory.GetChekItemCategory(Model.CheckDBTableTypeEnum.Amount, amountvalue.ToArray());
            //建立姓名物件
            List<object> namevalue   = new List<object>(){"林","阿貓"};
            List<object> namevalue2 = new List<object>() { "李", "阿狗" };                        
            Model.CheckItemBase nameItem = flyfactory.GetChekItemCategory(Model.CheckDBTableTypeEnum.Name, namevalue.ToArray());
            Model.CheckItemBase nameItem2 = flyfactory.GetChekItemCategory(Model.CheckDBTableTypeEnum.Name, namevalue2.ToArray());
            dateItem.DisplayValue();
            amountItem.DisplayValue();
            nameItem.DisplayValue();
            nameItem2.DisplayValue();
            Console.Write("物件總數:" + flyfactory.GetTotalItemNumber().ToString());
            Console.Read();

        }
    }
}

輸出結果

pic1