Advertisement
Guest User

Example of Composite pattern

a guest
Dec 1st, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             var compositeDiscountEvaluator = ConfigureEvaluator();
  12.             var scienceBook = new TextBook
  13.                                 {
  14.                                     Date = DateTime.Now,
  15.                                     Price = 100,
  16.                                     Genres = new[] {TextBooksGenre.Math}
  17.                                 };
  18.             var textBook = new TextBook
  19.                                 {
  20.                                     Date = DateTime.Now,
  21.                                     Price = 100,
  22.                                     Genres = new[] {TextBooksGenre.Math, TextBooksGenre.Science}
  23.                                 };
  24.             var fictionBook = new ReadingBook
  25.                         {
  26.                             Date = DateTime.Now,
  27.                             Price = 200,
  28.                             Genres = new[] {ReadingBooksGenre.Fiction}
  29.                         };
  30.             var readingBook = new ReadingBook
  31.                                     {
  32.                                         Date = DateTime.Now,
  33.                                         Price = 300,
  34.                                         Genres = new[] {ReadingBooksGenre.Fiction, ReadingBooksGenre.NonFiction}
  35.                                     };
  36.             Console.WriteLine(compositeDiscountEvaluator.GetDiscount(scienceBook));
  37.             Console.WriteLine(compositeDiscountEvaluator.GetDiscount(textBook));
  38.             Console.WriteLine(compositeDiscountEvaluator.GetDiscount(fictionBook));
  39.             Console.WriteLine(compositeDiscountEvaluator.GetDiscount(readingBook));
  40.         }
  41.  
  42.         private static IDiscountEvaluator ConfigureEvaluator()
  43.         {
  44.             var evaluator = new CompositeDiscountEvaluator();
  45.             evaluator.AddEvaluator(new ReadingBookDiscountEvaluator());
  46.             evaluator.AddEvaluator(new TextBookDiscountEvaluator());
  47.             return evaluator;
  48.         }
  49.     }
  50.  
  51.     class CompositeDiscountEvaluator : IDiscountEvaluator
  52.     {
  53.         private readonly ICollection<IDiscountEvaluator> evaluators;
  54.  
  55.         public CompositeDiscountEvaluator()
  56.         {
  57.             evaluators = new List<IDiscountEvaluator>();
  58.         }
  59.  
  60.         public void AddEvaluator(IDiscountEvaluator evaluator)
  61.         {
  62.             evaluators.Add(evaluator);
  63.         }
  64.  
  65.         public bool CanEvaluate<TGenre>(IBook<TGenre> book)
  66.         {
  67.             return evaluators.Any(e => e.CanEvaluate(book));
  68.         }
  69.  
  70.         public int GetDiscount<TGenre>(IBook<TGenre> book)
  71.         {
  72.             if (!CanEvaluate(book))
  73.                 throw new ArgumentException("No suitable evaluator");
  74.             return evaluators.Where(e => e.CanEvaluate(book)).Select(e => e.GetDiscount(book)).Max();
  75.         }
  76.     }
  77.  
  78.     interface IDiscountEvaluator
  79.     {
  80.         bool CanEvaluate<TGenre>(IBook<TGenre> book);
  81.         int GetDiscount<TGenre>(IBook<TGenre> book);
  82.     }
  83.  
  84.     class ReadingBookDiscountEvaluator : IDiscountEvaluator
  85.     {
  86.         private readonly IDictionary<ReadingBooksGenre, int> discounts;
  87.  
  88.         public ReadingBookDiscountEvaluator()
  89.         {
  90.             discounts = new Dictionary<ReadingBooksGenre, int>
  91.                             {
  92.                                 {ReadingBooksGenre.Fiction, 3},
  93.                                 {ReadingBooksGenre.NonFiction, 4}
  94.                             };
  95.         }
  96.  
  97.         public bool CanEvaluate<TGenre>(IBook<TGenre> book)
  98.         {
  99.             return book is ReadingBook;
  100.         }
  101.  
  102.         public int GetDiscount<TGenre>(IBook<TGenre> book)
  103.         {
  104.             var readingBook = (ReadingBook) book;
  105.             return readingBook.Genres.Select(g => discounts[g]).Max();
  106.         }
  107.     }
  108.  
  109.     class TextBookDiscountEvaluator : IDiscountEvaluator
  110.     {
  111.         private readonly IDictionary<TextBooksGenre, int> discounts;
  112.  
  113.         public TextBookDiscountEvaluator()
  114.         {
  115.             discounts = new Dictionary<TextBooksGenre, int>
  116.                             {
  117.                                 {TextBooksGenre.Math, 1},
  118.                                 {TextBooksGenre.Science, 2}
  119.                             };
  120.         }
  121.  
  122.         public bool CanEvaluate<TGenre>(IBook<TGenre> book)
  123.         {
  124.             return book is TextBook;
  125.         }
  126.  
  127.         public int GetDiscount<TGenre>(IBook<TGenre> book)
  128.         {
  129.             var textBook = (TextBook) book;
  130.             return textBook.Genres.Select(g => discounts[g]).Max();
  131.         }
  132.     }
  133.  
  134.     interface IBook<TGenre>
  135.     {
  136.         int Price { get; set; }
  137.         DateTime Date { get; set; }
  138.         TGenre[] Genres { get; set; }
  139.     }
  140.  
  141.     class ReadingBook : IBook<ReadingBooksGenre>
  142.     {
  143.         public int Price { get; set; }
  144.         public DateTime Date { get; set; }
  145.         public ReadingBooksGenre[] Genres { get; set; }
  146.     }
  147.  
  148.     class TextBook : IBook<TextBooksGenre>
  149.     {
  150.         public int Price { get; set; }
  151.         public DateTime Date { get; set; }
  152.         public TextBooksGenre[] Genres { get; set; }
  153.     }
  154.  
  155.     enum TextBooksGenre
  156.     {
  157.         Math,
  158.         Science
  159.     }
  160.  
  161.     public enum ReadingBooksGenre
  162.     {
  163.         Fiction,
  164.         NonFiction
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement