Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(String[] args)
- {
- // Thanks to immutability this can be reused without fear of change later on
- CarConfiguration standardBlack = CarConfigurations.StandardConfiguration.WithColor("black", false);
- CarConfiguration[] carConfigurationsCatalog =
- {
- CarConfigurations.StandardConfiguration,
- CarConfigurations.StandardConfiguration.WithAC(),
- CarConfigurations.StandardConfiguration.WithAC().WithNavigation(),
- standardBlack,
- standardBlack.WithAC(),
- CarConfigurations.StandardConfiguration.With(color: "black", glossyPaint:true),
- // An alternative with using a separate method for color
- CarConfigurations.StandardConfiguration.WithColor("red", true),
- CarConfigurations.LimitedConfiguration.With(navigation:true, leatherSeats:true), // Multiple parameters still look great!
- CarConfigurations.LimitedConfiguration.With(color:"red").With(leatherSeats: true)
- };
- }
- }
- // Turns out the base class doesn't really need to be fluent at all!
- public class CarConfiguration
- {
- public String Color { get; }
- public Boolean GlossyPaint { get; }
- public Boolean AC { get; }
- public Boolean LeatherSeats { get; }
- public Boolean Navigation { get; }
- public CarConfiguration(
- String color = "White",
- Boolean glossyPaint = false,
- Boolean ac = false,
- Boolean leatherSeats = false,
- Boolean navigation = false)
- {
- Color = color;
- GlossyPaint = glossyPaint;
- AC = ac;
- LeatherSeats = leatherSeats;
- Navigation = navigation;
- }
- }
- public static class CarConfigurations
- {
- // Thanks to immutability this can return the same object all the time. No need to construct it multiple times
- public static CarConfiguration StandardConfiguration { get; } = new CarConfiguration();
- public static CarConfiguration LimitedConfiguration { get; } = StandardConfiguration.WithAC().WithNavigation();
- }
- public static class CarConfigurationFluentExtensions
- {
- // All With's can be configured either this way
- public static CarConfiguration WithAC(this CarConfiguration carConfiguration) =>
- new CarConfiguration(
- carConfiguration.Color,
- carConfiguration.GlossyPaint,
- true,
- carConfiguration.LeatherSeats,
- carConfiguration.Navigation);
- // Or this way by reusing generic With() method
- public static CarConfiguration WithNavigation(this CarConfiguration carConfiguration) => carConfiguration.With(navigation: true);
- public static CarConfiguration WithColor(this CarConfiguration carConfiguration, String color, Boolean glossy) => carConfiguration.With(color: color, glossyPaint: glossy);
- // Generic With method that can be reused to implement specific With methods.
- // In some cases specific methods are not really needed
- public static CarConfiguration With(
- this CarConfiguration carConfiguration,
- String color = null, // In this case I'd consider taking a null as anti-pattern. Option type should be used instead
- Boolean? glossyPaint = null,
- Boolean? ac = null,
- Boolean? leatherSeats = null,
- Boolean? navigation = null)
- {
- return new CarConfiguration(
- color ?? carConfiguration.Color,
- glossyPaint ?? carConfiguration.GlossyPaint,
- ac ?? carConfiguration.AC,
- leatherSeats ?? carConfiguration.LeatherSeats,
- navigation ?? carConfiguration.Navigation);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement