Without Flyweight Pattern

  • Structural pattern
  • The intent of this pattern is to use sharing to support large number of fine-grained objects efficiently. It does that by sharing parts of the state between these objects instead of keeping all the state in all of the objects.
  • Common properties have to be moved . This will be discussed in next article when flyweight design pattern will be implemented(OrderItem class has to be modifed)

            public string Name { get; set; }
            public string Description { get; set; }
            public ProductType Type { get; set; }
            public double Price { get; set; }

     

				
					namespace WithoutFlyweightPattern
{
    public class Order
    {
        private readonly List<OrderItem> _items;
        public Order()
        {
            _items = new List<OrderItem>();
        }

        public void Add(OrderItem item)
        {
            _items.Add(item);
        }
    }
}

				
			
				
					namespace WithoutFlyweightPattern
{
    public class OrderItem
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public ProductType Type { get; set; }
        public double Price { get; set; }
        public string SerialNumber { get; set; }

        public OrderItem(string name, string description, ProductType type, double price, string serialNumber)
        {
            Name = name;
            Description = description;
            Type = type;
            Price = price;
            SerialNumber = serialNumber;
        }
    }

    public enum ProductType
    {
        GamingConsole,
        Desktops,
        Laptop
    }
}
				
			
				
					namespace WithoutFlyweightPattern
{
    public class PSItemFactory 
    {
        public OrderItem Create(PSSeries series, string serialNumber)
        {
            switch (series)
            {
                case PSSeries.PS5ConsoleEdition:
                    return new OrderItem("PS5 Console Edition",
                        "PS5 Console Edition with a Console free", ProductType.GamingConsole, 54990, serialNumber);
                case PSSeries.PS5DiscEdition:
                    return new OrderItem("PS5 Disc Edition",
                        "PS5 Disc Edition with a webcam", ProductType.GamingConsole, 54990, serialNumber);
                case PSSeries.PS4:
                    return new OrderItem("PS4 Console",
                        "PS4 Console old but still the best", ProductType.GamingConsole, 25000, serialNumber);
                case PSSeries.PS3:
                    return new OrderItem("PS3 Console Edition",
                         "PS3 2006 product", ProductType.GamingConsole, 15000, serialNumber);
                default:
                    throw new Exception("Unknown Gaming Console");
            }
        }
    }

    public enum PSSeries
    {
        PS5DiscEdition,
        PS5ConsoleEdition,
        PS4,
        PS3
    }
}

				
			
				
					using System.Text;
using WithoutFlyweightPattern;
class Program
{
    private static int orderCount = 100000;
    static void Main()
    {
        PSItemFactory pSItemFactory = new PSItemFactory();
        List<Order> orders = new List<Order>();
        //from front dropdown you fetch the PS type
        //for example : it is PS5ConsoleEdition
        //1 lakh order count.....
        var orderPsType = PSSeries.PS5ConsoleEdition;
        for (int i = 0; i < orderCount; i++)
        {
            Order order = new Order();
            OrderItem psItem = pSItemFactory.Create(orderPsType, ToBase36((ulong)i));
            order.Add(psItem);
            orders.Add(order);
        }
        Console.ReadKey();
    }

    private static string ToBase36(ulong value)
    {
        const string base36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var sb = new StringBuilder(9);
        do
        {
            sb.Insert(0, base36[(byte)(value % 36)]);
            value /= 36;
        } while (value != 0);

        var paddedString = "ABC" + sb.ToString().PadLeft(6, '0');
        return paddedString;
    }
}
				
			

Leave a Comment