Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(String[] args)
  8.         {
  9.             // Thanks to immutability this can be reused without fear of change later on
  10.             CarConfiguration standardBlack = CarConfigurations.StandardConfiguration.WithColor("black", false);
  11.  
  12.             CarConfiguration[] carConfigurationsCatalog =
  13.             {
  14.                 CarConfigurations.StandardConfiguration,
  15.                 CarConfigurations.StandardConfiguration.WithAC(),
  16.                 CarConfigurations.StandardConfiguration.WithAC().WithNavigation(),
  17.                 standardBlack,
  18.                 standardBlack.WithAC(),
  19.  
  20.                 CarConfigurations.StandardConfiguration.With(color: "black", glossyPaint:true),
  21.                 // An alternative with using a separate method for color
  22.                 CarConfigurations.StandardConfiguration.WithColor("red", true),
  23.  
  24.                 CarConfigurations.LimitedConfiguration.With(navigation:true, leatherSeats:true), // Multiple parameters still look great!
  25.                 CarConfigurations.LimitedConfiguration.With(color:"red").With(leatherSeats: true)
  26.             };
  27.  
  28.         }
  29.     }
  30.  
  31.     // Turns out the base class doesn't really need to be fluent at all!
  32.     public class CarConfiguration
  33.     {
  34.         public String Color { get; }
  35.         public Boolean GlossyPaint { get; }
  36.         public Boolean AC { get; }
  37.         public Boolean LeatherSeats { get; }
  38.         public Boolean Navigation { get; }
  39.  
  40.         public CarConfiguration(
  41.             String color = "White",
  42.             Boolean glossyPaint = false,
  43.             Boolean ac = false,
  44.             Boolean leatherSeats = false,
  45.             Boolean navigation = false)
  46.         {
  47.             Color = color;
  48.             GlossyPaint = glossyPaint;
  49.             AC = ac;
  50.             LeatherSeats = leatherSeats;
  51.             Navigation = navigation;
  52.         }
  53.  
  54.     }
  55.  
  56.     public static class CarConfigurations
  57.     {
  58.         // Thanks to immutability this can return the same object all the time. No need to construct it multiple times
  59.         public static CarConfiguration StandardConfiguration { get; } = new CarConfiguration();
  60.         public static CarConfiguration LimitedConfiguration { get; } = StandardConfiguration.WithAC().WithNavigation();
  61.     }
  62.  
  63.     public static class CarConfigurationFluentExtensions
  64.     {
  65.         // All With's can be configured either this way
  66.         public static CarConfiguration WithAC(this CarConfiguration carConfiguration) =>
  67.             new CarConfiguration(
  68.                 carConfiguration.Color,
  69.                 carConfiguration.GlossyPaint,
  70.                 true,
  71.                 carConfiguration.LeatherSeats,
  72.                 carConfiguration.Navigation);
  73.  
  74.         // Or this way by reusing generic With() method
  75.         public static CarConfiguration WithNavigation(this CarConfiguration carConfiguration) => carConfiguration.With(navigation: true);
  76.         public static CarConfiguration WithColor(this CarConfiguration carConfiguration, String color, Boolean glossy) => carConfiguration.With(color: color, glossyPaint: glossy);
  77.  
  78.         // Generic With method that can be reused to implement specific With methods.
  79.         // In some cases specific methods are not really needed
  80.         public static CarConfiguration With(
  81.             this CarConfiguration carConfiguration,
  82.             String color = null, // In this case I'd consider taking a null as anti-pattern. Option type should be used instead
  83.             Boolean? glossyPaint = null,
  84.             Boolean? ac = null,
  85.             Boolean? leatherSeats = null,
  86.             Boolean? navigation = null)
  87.         {
  88.             return new CarConfiguration(
  89.                 color ?? carConfiguration.Color,
  90.                 glossyPaint ?? carConfiguration.GlossyPaint,
  91.                 ac ?? carConfiguration.AC,
  92.                 leatherSeats ?? carConfiguration.LeatherSeats,
  93.                 navigation ?? carConfiguration.Navigation);
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement