Teaser Image

qnoid

Markos Charatzas - London, UK




/**
     * It's a revelation
     */
    public final class Route
    {
        public static final class RouteBuilder
        {
            private final Destination from;
            private final Destination to;
            private Calendar date = Calendar.getInstance();
            
            public RouteBuilder(Destination from, Destination to)
            {
                this.from = from;
                this.to = to;
            }
            
            public RouteBuilder onDate(Calendar date)
            {
                this.date = date;
                
            return this;
            }

            public Route build() {
            return new Route(this.from, this.to, this.date);
            }
        }

        private final DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
        
        private final Destination from;
        private final Destination to;
        private final Calendar date;

        private Route(Destination from, Destination to, Calendar date)
        {
            
            this.from = from;
            this.to = to;
            this.date = date;
        }

        private void appendDate(StringBuilder builder, Date date, DateFormat dateFormat) {
            builder.append( dateFormat.format(date) );
        }

        private void appendCode(StringBuilder builder, String code) {
            builder.append(code);
        }

        public int via(TransportMethod method) {
        return method.route(this.from, this.to, this.date);
        }
        
        public String bookingCode()
        {
            StringBuilder builder = new StringBuilder();
            
            this.appendCode(builder, this.from.code());    
            this.appendCode(builder, this.to.code());    
            this.appendDate(builder, this.date.getTime(), this.dateFormat);
            
        return builder.toString();
        }
    }

    /**
     * And a well thought design
     */
    public class RouteTest
    {
        private static final DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");  
        private static final Calendar now = Calendar.getInstance(); 
        private static final String BOOKING_CODE = 
            String.format("%s%s%s", 
                    Destination.EDINBURGH.code(), 
                    Destination.LONDON.code(),
                    dateFormat.format(now.getTime()));
        
        @Test
        public void via() throws Exception
        {
            TransportMethod transportMethod = TransportMethods.BUSINESS_FLYING;                
            Destination from = Destination.EDINBURGH;
            Destination to = Destination.LONDON;

            Route route = new RouteBuilder(from, to).build();

            Assert.assertEquals(BOOKING_CODE, route.bookingCode());
            Assert.assertEquals(500, route.via(transportMethod));
        }
    }

This isn't about time, money or need. This is about software design

The code above isn't "better" because it's eloquent. Nor because it adheres to SOLID principles. It's because it uses the most basic concept in OO. The Object.

It didn't derive thinking about extensibility, reusability, maintenance or testing. It wasn't conceived after a long architectural design discussion. This is OO at its purest form.

Don't ignore it. Embrace it. It's liberating.

Source