• CSharp: Composite Pattern in donet 6


         /// <summary>
        /// 组合模式 Composite Pattern 亦称: 对象树、Object Tree、Composite Pattern
        /// </summary>
        public abstract class Gift
        {
    
            /// <summary>
            /// 
            /// </summary>
            protected readonly string _description;
            /// <summary>
            /// 
            /// </summary>
            protected decimal _price;
            /// <summary>
            /// 
            /// </summary>
            /// <param name="description"></param>
            /// <param name="price"></param>
            protected Gift(string description, decimal price)
            {
                _description = description;
                _price = price;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public abstract decimal CalculatePrice();
        }
    
    
       /// <summary>
        /// 组合模式 Composite Pattern 亦称: 对象树、Object Tree、Composite Pattern
        /// </summary>
        public interface IGiftOperations
        {
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="gift"></param>
            void Add(Gift gift);
            /// <summary>
            /// 
            /// </summary>
            /// <param name="gift"></param>
            void Remove(Gift gift);
        }
    
     /// <summary>
        /// 组合模式 Composite Pattern 亦称: 对象树、Object Tree、Composite Pattern
        /// </summary>
        public class CompositeGift : Gift, IGiftOperations
        {
            /// <summary>
            /// This collection can store simple as well as composite gifts.
            /// </summary>
            private readonly List<Gift> _gifts;
            /// <summary>
            /// 
            /// </summary>
            /// <param name="description"></param>
            public CompositeGift(string description)
                : base(description, 0)
            {
                _gifts = new List<Gift>();
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="gift"></param>
            public void Add(Gift gift) =>
                _gifts.Add(gift);
            /// <summary>
            /// 
            /// </summary>
            /// <param name="gift"></param>
            public void Remove(Gift gift) =>
                // Consider using HashSet or Dictionary if you need remove an item from a collection.
                _gifts.Remove(gift);
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public override decimal CalculatePrice()
            {
                Console.WriteLine($"'{_description}' 包含以下产品及价格:");
                _price = _gifts.Sum(gift => gift.CalculatePrice());
    
                return _price;
            }
        }
    
    
    /// <summary>
        /// 组合模式 Composite Pattern 亦称: 对象树、Object Tree、Composite Pattern
        /// </summary>
        public class SimpleGift : Gift
        {
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="description"></param>
            /// <param name="price"></param>
            public SimpleGift(string description, decimal price)
                : base(description, price)
            {
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public override decimal CalculatePrice()
            {
                Console.WriteLine($"'{_description}' with the price of {_price:C}");
                return _price;
            }
        }
    
    
     /// <summary>
        ///  组合模式 Composite Pattern 亦称: 对象树、Object Tree、Composite Pattern
        /// </summary>
        public static class GiftExecutor
        {
    
            /// <summary>
            /// /
            /// </summary>
            public static void Execute()
            {
                ConsoleExtension.WriteSeparator("组合模式 Composite Pattern:  Gift demo");
    
                var smartWatchGift = new SimpleGift("智能手表 Smart watch", 200);
                var smartWatchPrice = smartWatchGift.CalculatePrice();
                Console.WriteLine($"智能手表礼品总价: {smartWatchPrice:C}");
    
                Console.WriteLine();
    
                var familyGift = new CompositeGift("家庭的礼物");
                var dadsGift = new SimpleGift("钓鱼竿Fishing rod", 50);
                var momsGift = new SimpleGift("项链Necklace", 80);
                var childrenGift = new CompositeGift("孩子们的礼物Children gift");
                childrenGift.Add(new SimpleGift("士兵玩具Soldier toy", 40));
                childrenGift.Add(new SimpleGift("芭比玩具Barbie toy", 50));
    
                familyGift.Add(dadsGift);
                familyGift.Add(momsGift);
                familyGift.Add(childrenGift);
    
                var familyGiftPrice = familyGift.CalculatePrice();
                Console.WriteLine($"家庭礼物总价: {familyGiftPrice:C}");
            }
        }
    

      调用:、

                Geovin.Du.DuComposite.DuGift.GiftExecutor.Execute();
                Console.WriteLine();
    

      

    输出:

    组合模式 Composite Pattern:  Gift demo
    --------------------------------------------------
    '智能手表 Smart watch' with the price of ¥200.00
    智能手表礼品总价: ¥200.00
    
    '家庭的礼物' 包含以下产品及价格::
    '钓鱼竿Fishing rod' with the price of ¥50.00
    '项链Necklace' with the price of ¥80.00
    '孩子们的礼物Children gift' 包含以下产品及价格::
    '士兵玩具Soldier toy' with the price of ¥40.00
    '芭比玩具Barbie toy' with the price of ¥50.00
    家庭礼物总价: ¥220.00
    

      

  • 相关阅读:
    复习提纲
    查看版本和存储的地方
    0到255的颜色
    stixel-world和psmnet结合出现的问题
    python plt 保存jpg出错
    三和韓長庚 著 正易 對讀 161-200
    startActivity、 startActivityForResult 、广播的使用
    01背包+卡精度 Hdu 2955
    c++ string 之 find_first_not_of 源码
    java:[1,0] illegal character: 65279 问题
  • 原文地址:https://www.cnblogs.com/geovindu/p/16890908.html
Copyright © 2020-2023  润新知