Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Dependency injection is useful for creating <code>service</code> style objects. These have the following characteristics:-</p> <ul> <li>multiple implementations are likely,</li> <li>heavy on behaviour, </li> <li>internal state is limited to their dependencies and they are generally not mutable</li> <li>would map to an actor in the real world (e.g. cashier), rather than a thing</li> </ul> <p>Based on this, <code>Payment</code> is a service object. I would rename it <code>PaymentService</code> to distinguish it from the ledger entry you may store about a payment (which would be a value object).</p> <p>You example does not show much about what the <code>Order</code> class, does, but I assume it would hold information like some line items, delivery address and total amount. This is a <code>value</code> object. It represents a thing in the business domain.</p> <p>Value objects are heavy on state, and lighter on behaviour. Multiple implementations are possible, but you are less likely to want to replace one implementation with another.</p> <p>Value objects are not created by your dependency injection framework. They are created by your business logic code. In your example, you are creating all the objects using Guice. I expect in reality you would need to create the <code>Order</code> at run-time based on user input.</p> <p>Service objects can depend on value objects, but never the other way around. I think you should be looking to implement:</p> <p><code>checkoutService.payfor( order, method );</code></p> <p>and not <code>order.finishOrder( method )</code></p> <p>Within the <code>CheckoutService</code> class, you could choose an approriate <code>PaymentService</code> and pass the <code>order</code> to it. The <code>CheckoutService</code> would take as constructor arguments <code>PaymentCardPaymentService</code> (equivalent to your PaymentCardImpl) and <code>CashPaymentService</code> (equivalent to your PaymentCashImpl).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload