oop - How to decouple two classes elegantly in the following example? -


suppose take case of building vending machine using oop principles.

let suppose have abstraction called vendingmachine.

class vendingmachine {     list<slot> slots; //or perhaps 2-d matrix of slots } 

the vendingmachine class has list of slots , each slot may have capacity (to model 5 items 1 behind other).

now how associate value ($) slot. each slot have same item each slot should associated same value (or better abstraction representing item, item class).

but in terms of responsibility, vendingmachine class should able eject item, or throw exception when trying eject item empty slot. think not responsibility of vendingmachine class know value of particular slot.

how design elegantly? there design pattern comes mind.

my solution create class moneymanager.

class moneymanager {     moneymanager(vendingmachine vm);     pair<slot, item> mapping; } class item {     int itemcode;     bigdecimal value; } 

even if think modelling wrong, more interested in knowing how decouple 2 classes that.

for example if design car parking lot, class vehicles should have information on how space takes (number of spots). parkinglot has information how spots has.

but don't want car know in parkinglot parked , in spot. don't want parkinglot maintain state of cars parked , where. should there intermediate class parkingmanager maintains state clean design?

i think need kind of logic wharehouse administration.

supposing slots , items of same size, probabbly break out this.

public class item {     public string name { get; set; }      public string category { get; set; } }  public class slot {     public int capacity { get; set; }      public list<item> items { get; set; } }  public class warehouse {     public list<slot> slots { get; set; } } 

this going manage basic distribution of items in warehouse. warehouse class going manage list of slots , slot class going manage list of items contains.

probably, if need, you add sizes , locations , add logic of kind of items fit different slots.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -