Skip to content

Instantly share code, notes, and snippets.

View asarnaout's full-sized avatar

Ahmed Shirin asarnaout

  • Tampa, FL
View GitHub Profile
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()
{
@asarnaout
asarnaout / ZoneClient.cs
Last active October 2, 2018 19:11
Flyweight Pattern Gist
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++)
@asarnaout
asarnaout / ZoneFactory.cs
Last active October 2, 2018 19:11
Flyweight Pattern Gist
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))
@asarnaout
asarnaout / Zone.cs
Last active October 3, 2018 14:47
Flyweight Pattern Gist
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;
@asarnaout
asarnaout / A.Refactoring_To_Side_Effect_Free_Functions.cs
Last active September 28, 2018 03:09
Side Effect Free Functions
public class CartService
{
public decimal GetShippingQuoteForUpdatedAddress(ShoppingCart cart,
Address updatedAddress)
{
cart.CalculateShippingDetail(updatedAddress); //Side effect
return cart.ShippingQuote;
}
}
@asarnaout
asarnaout / A.Flyweight.cs
Last active September 28, 2018 02:02
Flyweight & Factory Patterns
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
{
class TestBlock
{
public string Hash { get; set; }
private int Nonce { get; set; }
private int Index { get; }
private string Data { get; }
@asarnaout
asarnaout / A.Memento.cs
Created July 6, 2018 17:42
Memento Design Pattern
public class Program
{
public static void Main(string[] args)
{
var account = new Account {
Id = 1,
Verified = false,
Credit = 10,
Notes = "None"
};
@asarnaout
asarnaout / A.Observer.Design.Pattern.cs
Last active May 5, 2018 03:36
Observer Design Pattern
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)
{
@asarnaout
asarnaout / A.TemplateMethod.Design.Pattern.cs
Last active May 5, 2018 03:16
Template Method Design Pattern
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.