/// <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