你的位置:首页 > ASP.net教程

[ASP.net教程]【设计模式】装饰者模式


※文件引自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<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 IProductRepository10   {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 ProductCollectionExtensionMethods10   {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 : IProductRepository11   {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