Teaser Image

qnoid

Markos Charatzas - London, UK




/**
     * Survival Kit #1
     * Rock some awesome Forrst schwag and show your support for the community.
     */
    public class SurvivalKit
    {     
        private final TShirt tShirt;
        private final Mug mug;
        private final Wristband wristband;
        private final Collection<Sticker> stickers;
        private final int cost;
        
        public SurvivalKit(TShirt tShirt, Mug mug, Wristband wristband, Collection<Sticker> stickers, int cost)
        {
            this.tShirt = tShirt;
            this.mug = mug;
            this.wristband = wristband;
            this.stickers = stickers;
            this.cost = cost;
        }

        /**
         * @return the tShirt
         */
        public TShirt tShirt() {
        return tShirt;
        }

        /**
         * @return the mug
         */
        public Mug mug() {
        return mug;
        }

        /**
         * @return the wristband
         */
        public Wristband wristband() {
        return wristband;
        }

        /**
         * @return the stickers
         */
        public Collection<Sticker> stickers() {
        return stickers;
        }
        
        /**
         * @return the size
         */
        public Size size(){
        return this.tShirt.size();
        }

        /**
         * @return the cost
         */
        public int cost(){
        return this.cost;
        }
    }

If you are totally unfamiliar with the notion of objects you should read the preamble first

To start building an OO program you need objects. The most basic way to identify objects in a text is looking at the nouns.

Take a look at the survival kit. Some of the nouns identified are tshirt (cotton), mug (coffee, tea, beverage, site), wristband, sticker (vinyl) and the survival kit itself. Also, shipping (fee - implied, cost - disguised as an image)

Now since objects are used to do things with or on them the second step is to identify their methods. Look at the verbs for that. wear, enjoy, browse, purchase. Some might be disguised like make friends jealous or even implied dispatch once a purchase is made.

Now all you have to do is make the association between which objects have which methods and create your classes. Some will be straight forward, others not so much.

You might be tempted to define a purchase method in the survival kit but let me ask you this. What is a purchase method supposed to do? Most likely will calculate the cost of the order and dispatch the item to a given address based on a shipping method.

An important thing to have in mind when crafting your OO code is to design classes with single responsibility.

You might think, hell that will only create so many classes! Well, if a class has a reason to exist you should do well to create one. There isn't such thing as too many classes when there is a reason. Even if you don't create a class, it will still manifest itself in your code. The ability to correctly identify the right number of classes only comes with experience.

If you are instead troubled that you will end up creating a vast API that will be cumbersome to use and difficult to understand there are other ways to hide those classes or design patterns to make using your API easier.

Although a basic methodology, you will be surprised how effective it is even for those technical specs or system requirements that you read. Especially when you are clueless about the problem domain.

Till the next post, leave your comments below on which class you would define a purchase method and what are some of the objects/classes you have identified in the survival kit.

Part of the Don't lose your train of thoughts series.