Teaser Image

qnoid

Markos Charatzas - London, UK




/**
     * Feel free to reuse!
     */
    public enum ShippingMethod
    {
        US_CANADA(0),
        INTERNATIONAL(10);

        /**
         * Dispatches the order to the specified address.
         * 
         * @param order the order which might contain several kits
         * @param adress the address for delivery
         * @return a unique identifier for the order used for tracking
         */
        public String dispatch(SurvivalKitOrder order, Address adress){
            
            //There goes another one
        }
    }

    /**
     * The almighty controller.
     * Dispatching orders since 1 month ago
     */
    public class ForrstController
    {
        /**
         * This method will decide on the shipping method, create 
         * a new SurvivalKitOrder and an Address and initiate the dispatch!
         */
        public void purchase()
        {
            ShippingMethod method = ShippingMethod.INTERNATIONAL;
            
            SurvivalKit survivalKit = new SurvivalKit(
                                                    new TShirt(Size.LARGE, Material.COTTON),
                                                    new Mug(),
                                                    new Wristband(),
                                                    Collections.nCopies(2, new Sticker(Material.VINYL)), 
                                                    29);

            Collection<SurvivalKit> survivalKits = new ArrayList<SurvivalKit>();
            survivalKits.add( survivalKit );
            
            SurvivalKitOrder order = new SurvivalKitOrder("F0RR7T", survivalKits);
            Address adress = new Address();
            
            //Woohoo we have started shipping!
            method.dispatch(order, adress);
        }
    }

When thinking about the ShippingMethod class we talked about the importance of designing immutable classes thus allowing us to create constant objects and reuse them freely. Also while trying to identify the relationships to the SurvivalKitOrder class in order to dispatch it we hold back from defining a rather complicated one.

Now the ShippingMethod is your point of reference. It's what you can reuse again and again to dispatch (Hint) an order to an address. The SurvivalKitOrder and the Address is what varies each time. So whenever a new purchase is made, you choose the ShippingMethod and create a new SurvivalKitOrder and an Address.

Designing for reusability through constants is "the key to success". If you look back at the design of your classes you will come to realize that not only you have defined the minimum required relationships between them but in doing so you have effectively avoided any concurrency issues in the process.

The reason for that is that any shared objects (the shipping method) are constants and anything that varies (order, address) is new to each thread.

The question that remains is where do you create a new order and an address and how does it all come together? That's where the controller steps in. The controller itself could be mapped to a URL that handles purchases and gets all the address details from the form.

This effectively also closes the circle for the #purchase method which has now found its place.

Next in the series is "Honour Your Builders" where we'll see how you can go on creating survival kits the fun way. Don't miss it.

Disclaimer: This is example code to make you think of class relationships and reusability. The ShippingMethod#dispatch will most likely require some other class relationship to do the dispatch, so in the real world you'll might have to take that into consideration as well.