This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Customer // PRE REFACTORING | |
{ | |
public decimal EarnedRewards { get; private set; } | |
public int TotalPoints { get; private set; } | |
public DateTime MemberSince { get; private set; } | |
public void CalculateRewards() | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ZoneClient | |
{ | |
static Random rnd = new Random(); | |
public IEnumerable<ShippingRoute> CreateShippingRoutes(IList<string> usZipCodes) | |
{ | |
var routes = new List<ShippingRoute>(); | |
var zoneFactory = new ZoneFactory(); | |
for (var i = 0; i < 2000000; i++) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ZoneFactory | |
{ | |
private readonly IDictionary<string, Zone> _zones = new Dictionary<string, Zone>(); | |
private readonly object _lock = new object(); | |
public Zone GetZone(string zipCode) | |
{ | |
lock (_lock) | |
{ | |
if (_zones.TryGetValue(zipCode, out var zone)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Zone : IEquatable<Zone> | |
//An immutable flyweight...This represents a domain value object | |
{ | |
public string ZipCode { get; } | |
public IReadOnlyCollection<string> AvailableCouriers { get; private set; } | |
public Zone(string zipCode) | |
{ | |
ZipCode = zipCode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CartService | |
{ | |
public decimal GetShippingQuoteForUpdatedAddress(ShoppingCart cart, | |
Address updatedAddress) | |
{ | |
cart.CalculateShippingDetail(updatedAddress); //Side effect | |
return cart.ShippingQuote; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Client | |
{ | |
public static void Main(string[] args) | |
{ | |
var routes = new List<ShipmentRoute>(); | |
var zoneFactory = new ZoneFactory(); | |
var shipmentRouteFactory = new ShipmentRouteFactory(); | |
for (var i = 0; i < 2000000; i++) //Creating 2 million identical routes, just coz I can lol | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestBlock | |
{ | |
public string Hash { get; set; } | |
private int Nonce { get; set; } | |
private int Index { get; } | |
private string Data { get; } | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var account = new Account { | |
Id = 1, | |
Verified = false, | |
Credit = 10, | |
Notes = "None" | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Observer | |
{ | |
class Program | |
{ | |
/* | |
* The Observer pattern allows you to define a one-to-many dependency between objects so that when one object changes state, all its | |
* dependents are notified and updated automatically. Note that the `event` keyword is a dot net implementation of the observer pattern. | |
*/ | |
static void Main(string[] args) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace TemplateMethod | |
{ | |
/* | |
* The template method Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets | |
* subclasses redefine certain steps of an algorithm without changing the algorithm's structure. | |
* | |
* The Template Method pattern is used when two or more implementations of a similar algorithm exist. In the real world templates are | |
* used all the time: for architectural plans, and throughout the engineering domain. A template plan may be defined which is then built | |
* on with further variations. For example, a basic house plan can have many variations such as adding an extensions or using a different | |
* heating system. |
NewerOlder