patterncsharpMinor
Vending Machine
Viewed 0 times
machinevendingstackoverflow
Problem
After doing a lot of reading and practicing, I've come up with a simple program that implements multiple OOP techniques, principles and several design patterns such as Observers, Factories and Singletons.
The program represents a vending machine with all the relevant actions and properties. The vending machine can be refilled with items, a client can purchase a specific item, the machine screen notifies the client about the result of his actions etc'.
VendingMachine.cs
```
public sealed class VendingMachine : ObserverCommands
{
private static VendingMachine instance;
private readonly Dictionary machineItems;
private readonly Stack saleRecords;
private double machineBank;
private List machineObservers;
private VendingMachine()
{
this.machineItems = new Dictionary();
this.saleRecords = new Stack();
this.machineBank = 0;
this.machineObservers = new List();
}
public static VendingMachine GetInstance()
{
if (instance == null)
instance = new VendingMachine();
return instance;
}
public double MachineBank
{
get
{
return this.machineBank;
}
}
public Dictionary MachineItems
{
get
{
return this.machineItems;
}
}
public Stack SaleRecords
{
get
{
return this.saleRecords;
}
}
public List GetLastSaleRecords(int num)
{
List list;
if(num ();
foreach (SaleRecord saleRecord in saleRecords)
{
list.Add(saleRecord);
if (--num == 0)
break;
}
return list;
}
public int GetItemStock(Item item)
{
if (machineItems.ContainsKey(item))
return machineItems[item];
return -1;
}
public int GetTotalMachineItems()
{
int totalItems = 0;
foreach (Item item in machineItems.Keys)
The program represents a vending machine with all the relevant actions and properties. The vending machine can be refilled with items, a client can purchase a specific item, the machine screen notifies the client about the result of his actions etc'.
VendingMachine.cs
```
public sealed class VendingMachine : ObserverCommands
{
private static VendingMachine instance;
private readonly Dictionary machineItems;
private readonly Stack saleRecords;
private double machineBank;
private List machineObservers;
private VendingMachine()
{
this.machineItems = new Dictionary();
this.saleRecords = new Stack();
this.machineBank = 0;
this.machineObservers = new List();
}
public static VendingMachine GetInstance()
{
if (instance == null)
instance = new VendingMachine();
return instance;
}
public double MachineBank
{
get
{
return this.machineBank;
}
}
public Dictionary MachineItems
{
get
{
return this.machineItems;
}
}
public Stack SaleRecords
{
get
{
return this.saleRecords;
}
}
public List GetLastSaleRecords(int num)
{
List list;
if(num ();
foreach (SaleRecord saleRecord in saleRecords)
{
list.Add(saleRecord);
if (--num == 0)
break;
}
return list;
}
public int GetItemStock(Item item)
{
if (machineItems.ContainsKey(item))
return machineItems[item];
return -1;
}
public int GetTotalMachineItems()
{
int totalItems = 0;
foreach (Item item in machineItems.Keys)
Solution
This isn't a pattern you typically see in the .Net languages.
But why? Observables/Observers are a really useful pattern?!
You don't ever see anyone implementing Observers by hand because the language natively supports the pattern via Events. Way back before VB6 the Microsoft languages had this pattern built into not just the language, but into the philosophy of how these languages are meant to work at their core.
There's nothing actually wrong with your implementation, but using events would be a much more idiomatic way to code it.
public void RegisterObserver(Observer observer)
{
this.machineObservers.Add(observer);
}
public void UnregisterObserver(Observer observer)
{
this.machineObservers.Remove(observer);
}
public void NotifyAllObservers(VendingMachineLog log)
{
foreach (Observer observer in this.machineObservers)
observer.Update(log);
}But why? Observables/Observers are a really useful pattern?!
You don't ever see anyone implementing Observers by hand because the language natively supports the pattern via Events. Way back before VB6 the Microsoft languages had this pattern built into not just the language, but into the philosophy of how these languages are meant to work at their core.
There's nothing actually wrong with your implementation, but using events would be a much more idiomatic way to code it.
Code Snippets
public void RegisterObserver(Observer observer)
{
this.machineObservers.Add(observer);
}
public void UnregisterObserver(Observer observer)
{
this.machineObservers.Remove(observer);
}
public void NotifyAllObservers(VendingMachineLog log)
{
foreach (Observer observer in this.machineObservers)
observer.Update(log);
}Context
StackExchange Code Review Q#158792, answer score: 7
Revisions (0)
No revisions yet.