【设计模式】装饰者模式

※文件引自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 IPrice
10     {
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 : IPrice
10     {
11         private decimal _cost;
12 
13         public decimal Cost
14         {
15             get
16             {
17                 return _cost;
18             }
19             set
20             {
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 : IPrice
10     {
11         private IPrice _basePrice;
12 
13         public TradeDiscountPriceDecorator(IPrice price)
14         {
15             _basePrice = price;
16         }
17 
18         public decimal Cost
19         {
20             get
21             {
22                 return _basePrice.Cost * 0.95m;
23             }
24             set
25             {
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 : IPrice
10     {
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 Cost
22         {
23             get
24             {
25                 return _basePrice.Cost * _exchangeRate;
26             }
27             set
28             {
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 Product
10     {
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 ProductService
10     {
11         private IProductRepository _productRepository;
12 
13         public ProductService(IProductRepository productRepository)
14         {
15             _productRepository = productRepository;
16         }
17 
18         public IEnumerable<Product> GetAllProduct()
19         {
20             IEnumerable<Product> 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 IProductRepository
10     {
11         IEnumerable<Product> 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 ProductCollectionExtensionMethods
10     {
11         public static void ApplyCurrencyMultiplier(this IEnumerable<Product> 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<Product> 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<Product> 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 : IProductRepository
11     {
12         public IEnumerable<Product> FindAll()
13         {
14             IEnumerable<Product> 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