public static AbstractTools GetTools(string a_strProcessObject)
        {
            AbstractTools l_absTools = null;

            try
            {
                l_absTools = m_dicTools[a_strProcessObject];
            }
            catch
            {
                if (a_strProcessObject == "夹子")
                {
                    l_absTools = new Clamp();
                }
                if (a_strProcessObject == "框子")
                {
                    l_absTools = new Frame();
                }
                if (a_strProcessObject == "推车")
                {
                    l_absTools = new Cart();
                }
                else
                {
                    throw new Exception("请输入合法的工具");
                }
                m_dicTools.Add(a_strProcessObject, l_absTools);
            }
            return(l_absTools);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // 现在我们需要运输大理石,我们选一个推车吧
            AbstractTools l_Tools = FlyweightToolsFactory.GetTools("推车");

            l_Tools.ProcessObject = "大理石";
            l_Tools.Operation();

            // 现在我们再用推车搬运红砖吧
            AbstractTools l_Cart = FlyweightToolsFactory.GetTools("推车");

            l_Cart.ProcessObject = "红砖";
            l_Cart.Operation();

            // 我们再判断l_Tools 和 l_Cart的类型是否相同
            Console.WriteLine(object.Equals(l_Cart, l_Tools));
            Console.ReadKey();
        }