JamesGold
JamesGold

Reputation: 815

What to do when object members of a class need access to each other's data?

I have a simple Store class that contains an Inventory object and a CashRegister object. In order to sell an item, the CashRegister object needs to access the Inventory object's methods; namely, those which return a particular item's price, quantity held in storage, etc. What's the best way to give the CashRegister object access to the Inventory object's methods?

Thank you.

Upvotes: 0

Views: 204

Answers (4)

Luchian Grigore
Luchian Grigore

Reputation: 258618

the CashRegister object needs to access the Inventory object's methods

Why? The CashRegister can exist and operate perfectly without an Inventory. You can have a CashRegister in a theater or a cinema, not only in a Store. This part of the logic you're talking about should be part of the store.

class Store
{
   CashRegister register;
   Inventory I;
   void sellSomething(Item i)
   {
      //move logic inside the store
      //not in CashRegister
      int price = I.getPrice(i);
      register.addCash(price);
      I.removeItem(i);
   }
};

Upvotes: 3

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24133

Give objects responsibilities, don't just use them for clumping data.

The Inventory should know how to add and remove Items. It may be that a transaction needs to take place. This assignment is a metaphor for a database.

@Luchian has the right idea but I would take it further.

You want the Inventory (database) to be able to process Transactions, in this case one which is specialised to use a cash register.

The Item should be able to represent itself to the Inventory.

Look at the ideas of Tell Don't Ask, and Write No Getters.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409206

One solution is to store a reference to the Inventory object in the CashRegister object, and give the reference when constructing the Store object.

class Inventory { /* ... */ };

class CashRegister
{
public:
    CashRegister(Inventory &inventory)
        : inventory_(inventory)
    { }

    // ....

private:
    Inventory &inventory_;
};

class Store
{
public:
    Store()
        : cashregister_(inventory_)
    { }

    // ....

private:
    Inventory inventory_;
    CashRegister cashregister_;
};

Upvotes: 3

Steve Jessop
Steve Jessop

Reputation: 279285

class Inventory {
  Currency price;
  uint64_t quantity;
  public:
    Currency get_price() { return price; }
    uint64_t get_quantity() { return quantity; }
};

Then your cash register can call the functions on an instance of Inventory.

But really, you should put a function on Inventory to reserve or deliver a certain number of objects if available -- a cash register does not need to know how many items are in stock, it only needs to know whether the current demand can be satisfied. For that matter, I'm not sure that an Inventory needs to know the price of an object -- if there's 10% off on Wednesday afternoons, then what business is that of the warehouse?

Upvotes: 1

Related Questions