博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【设计模式】装饰者模式
阅读量:5241 次
发布时间:2019-06-14

本文共 6109 字,大约阅读时间需要 20 分钟。

※文件引自OneDrive,有些人可能看不到

代码如下:

IPrice

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Model 8 { 9     public interface IPrice10     {11         decimal Cost { get; set; }12     }13 }
View Code

BasePrice

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Model 8 { 9     public class BasePrice : IPrice10     {11         private decimal _cost;12 13         public decimal Cost14         {15             get16             {17                 return _cost;18             }19             set20             {21                 _cost = value;22             }23         }24     }25 }
View Code

TradeDiscountPriceDecorator

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Model 8 { 9     public class TradeDiscountPriceDecorator : IPrice10     {11         private IPrice _basePrice;12 13         public TradeDiscountPriceDecorator(IPrice price)14         {15             _basePrice = price;16         }17 18         public decimal Cost19         {20             get21             {22                 return _basePrice.Cost * 0.95m;23             }24             set25             {26                 _basePrice.Cost = value;27             }28         }29     }30 }
View Code

CurrencyPriceDecorator

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Model 8 { 9     public class CurrencyPriceDecorator : IPrice10     {11         private IPrice _basePrice;12 13         private decimal _exchangeRate;14 15         public CurrencyPriceDecorator(IPrice price, decimal exchangeRate)16         {17             _basePrice = price;18             _exchangeRate = exchangeRate;19         }20 21         public decimal Cost22         {23             get24             {25                 return _basePrice.Cost * _exchangeRate;26             }27             set28             {29                 _basePrice.Cost = value;30             }31         }32     }33 }
View Code

Product

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Model 8 { 9     public class Product10     {11         public IPrice Price { get; set; }12     }13 }
View Code

ProductService

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Model 8 { 9     public class ProductService10     {11         private IProductRepository _productRepository;12 13         public ProductService(IProductRepository productRepository)14         {15             _productRepository = productRepository;16         }17 18         public IEnumerable
GetAllProduct()19 {20 IEnumerable
products = _productRepository.FindAll();21 22 products.ApplyTradeDiscount();23 24 products.ApplyCurrencyMultiplier(0.78m);25 26 return products;27 }28 }29 }
View Code

IProductRepository

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Model 8 { 9     public interface IProductRepository10     {11         IEnumerable
FindAll();12 }13 }
View Code

ProductCollectionExtensionMethods

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Model 8 { 9     public static class ProductCollectionExtensionMethods10     {11         public static void ApplyCurrencyMultiplier(this IEnumerable
products, decimal exchangeRate)12 {13 foreach (Product p in products)14 {15 p.Price = new CurrencyPriceDecorator(p.Price, exchangeRate);16 }17 }18 19 public static void ApplyTradeDiscount(this IEnumerable
products)20 {21 foreach (Product p in products)22 {23 p.Price = new TradeDiscountPriceDecorator(p.Price);24 }25 }26 }27 }
View Code

Program

1 using DecoratorConsole.Test; 2 using Model; 3 using System; 4 using System.Collections.Generic; 5  6 namespace DecoratorConsole 7 { 8     class Program 9     {10         static void Main(string[] args)11         {12             ProductService service = new ProductService(new TestPorductRepository());13             IEnumerable
products = service.GetAllProduct();14 15 foreach (Product p in products)16 {17 Console.WriteLine(string.Format("Product's Price : {0}", p.Price.Cost));18 }19 20 Console.ReadLine();21 }22 }23 }
View Code

TestPorductRepository

1 using Model; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7  8 namespace DecoratorConsole.Test 9 {10     public class TestPorductRepository : IProductRepository11     {12         public IEnumerable
FindAll()13 {14 IEnumerable
products = new Product[] {15 new Product() { Price = new BasePrice() { Cost = 100m }},16 new Product() { Price = new BasePrice() { Cost = 200m }},17 new Product() { Price = new BasePrice() { Cost = 50m }},18 new Product() { Price = new BasePrice() { Cost = 240.5m }},19 new Product() { Price = new BasePrice() { Cost = 500m }},20 new Product() { Price = new BasePrice() { Cost = 1000m }},21 new Product() { Price = new BasePrice() { Cost = 1500m }},22 };23 24 return products;25 }26 }27 }
View Code

 

转载于:https://www.cnblogs.com/Ryukaka/p/4651983.html

你可能感兴趣的文章
第16周总结
查看>>
C#编程时应注意的性能处理
查看>>
Fragment
查看>>
比较安全的获取站点更目录
查看>>
苹果开发者账号那些事儿(二)
查看>>
使用C#交互快速生成代码!
查看>>
UVA11374 Airport Express
查看>>
P1373 小a和uim之大逃离 四维dp,维护差值
查看>>
NOIP2015 运输计划 树上差分+树剖
查看>>
P3950 部落冲突 树链剖分
查看>>
读书_2019年
查看>>
读书汇总贴
查看>>
微信小程序 movable-view组件应用:可拖动悬浮框_返回首页
查看>>
MPT树详解
查看>>
空间分析开源库GEOS
查看>>
RQNOJ八月赛
查看>>
前端各种mate积累
查看>>
jQuery 1.7 发布了
查看>>
Python(软件目录结构规范)
查看>>
Windows多线程入门のCreateThread与_beginthreadex本质区别(转)
查看>>