0

I have my main class PayUnit where I create my main object references, such as touchScreen, scale, and barcodeReader as these are has-a relationships to my PayUnit. This machine also has-a billDispenser but billDispenser is really 'is-a' relationship to currencyDispener. Do I still create a billDispenser and currencyDispenser object references in the main class, or do create a currencyDispenser reference in PayUnit and then create a billDispenser in the currencyDispenser class?

I hope that makes sense. What is the standard for OOP in this regard?

Thanks for the help.

KiloJKilo
  • 465
  • 1
  • 6
  • 16

1 Answers1

2

From your description, it appears that there's no need to have the references to both billDispenser and currencyDispenser in PayUnit (since they are not separate entities, and one of them is an implementation of the other). A common practice is to define a protocol (an interface or an abstract class in Java) that describes the API of a module (in your case, currencyDispenser), and then have a factory method somewhere provide an implementation that conforms to that interface (billDispenser in your case). I hope this answers your question.

TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76
  • I see, so only references to concrete classes, more or less? – KiloJKilo Jul 04 '12 at 17:33
  • 2
    Not sure if I understand the question, but it's preferable to use the references to the superclass whenever possible. Then you can replace the concrete class with another one, and it will just work. In your example, `PayUnit` can have a reference to `CurrencyDispenser`, and you can assign it an instance of `BillDispenser` or, say, `CoinDispenser`, all without any changes to `PayUnit`. – TotoroTotoro Jul 04 '12 at 17:39