Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- public class Bread : ProductPrototype
- {
- public Bread(decimal price) : base(price) { }
- public override ProductPrototype Clone()
- {
- return (ProductPrototype)this.MemberwiseClone();
- }
- }
- public class Milk : ProductPrototype
- {
- public Milk(decimal price) : base(price) { }
- public override ProductPrototype Clone()
- {
- return (ProductPrototype)this.MemberwiseClone();
- }
- }
- public abstract class ProductPrototype
- {
- public decimal Price { get; set; }
- public ProductPrototype(decimal price)
- {
- Price = price;
- }
- public abstract ProductPrototype Clone();
- }
- public class Supermarket
- {
- private Dictionary<string, ProductPrototype> _productList = new Dictionary<string, ProductPrototype>();
- public void AddProduct(string key, ProductPrototype productPrototype)
- {
- _productList.Add(key, productPrototype);
- }
- public ProductPrototype GetProduct(string key)
- {
- return _productList[key].Clone();
- }
- }
- class MainClass
- {
- public static void Main(string[] args)
- {
- var supermarket = new Supermarket();
- supermarket.AddProduct("Milk", new Milk(0.89m));
- supermarket.AddProduct("Bread", new Bread(1.10m));
- decimal sourcePrice;
- decimal currentPrice;
- var clonedMilk = (Milk)supermarket.GetProduct("Milk");
- sourcePrice = clonedMilk.Price;
- clonedMilk.Price = 0.85m;
- currentPrice = clonedMilk.Price;
- Console.WriteLine(String.Format("Milk - {0} > {1}", sourcePrice, currentPrice));
- var clonedBread = (Bread)supermarket.GetProduct("Bread");
- sourcePrice = clonedBread.Price;
- clonedBread.Price = 0.99m;
- currentPrice = clonedBread.Price;
- Console.WriteLine(String.Format("Bread - {0} > {1}", sourcePrice, currentPrice));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement