Tuesday, June 1, 2010

Refractoring if-else to rules

This is one snippet to convert if else into rules mostly floating in non-concerned class .For example I have designed an online movie booking system .And now I am going to add cancellation functionality -


Class CancellationService {

public void cancelTicket(Ticket ticket ){
if(isCancellationAllowed()){
ticket.setStatus("Cancelled");

}
private boolean isCancellationAllowed(Ticket ticket){
if(new Date()return false;
}
if(ticket.bookingType()=="WINDOW_BOOKING"){
return false;
}

if(ticket.BookingType()=="PHONE_BOOKING"){
return false;
}


if(ticket.BookingType() = "SPECIAL_OFFER"){
return false;
}
return true;
}

As you might have already noticed slowly application need you to add more rules .This would mix cancellation class with rules .And if you do it this way ..rules are not easy to review for domain experts.


Another better way is to --

public interface cancellationValidator {
public boolean cancellationAllowed(Ticket);
}

public class windowTicketCancellationExc implemets cancellationValidator{
public boolean cancellationAllowed(Ticket ticket){
if(ticket.cancellationType() == "WINDOW_TICKET"){
return false;
}
return true;
}

public class TicketStatusCancellationExc implemets cancellationValidator {
public boolean cancellationAllowed(Ticket ticket){
if(!ticket.status .equals("BOOKED")){
return false;
}
return true;
}


now make a TicketCancellation RulesGenerator
//a static factory for now
public class TicketCancellationRuleGenerator {
public static List getCancellationValidators() {
List validators = new ArrayList();
validators.add (new windowTicketCancellationExc() );
validators.add(new TicketStatusCancellationExc());
}
}

//now write a master cancellationValidator --

public class MasterCancellationValid

Sunday, September 7, 2008

Dawn and Evening

dawn and evening
so similar
so different
.
.
.
blue redish and
all colors of infinity
both are illusions
both are symbols of change

.
.
.
dawn and evening
so different
dawn is new
evening is tired
dawn is start
evening is end
Dawn comes with light
Evening comes with a Dark
.
.
.
.
Dawn and evening
Stark emotions of Nature
One is hope of New Start
Another is tired hope of better effort
.
..
.
Both are emotions of nature
Both are emotionss of everyone!

Saturday, August 30, 2008

Disgrace

disgrace pushes me hard....

glory asks me to stop.....

A disgraceful Past
pushes me hard
..
A glorious Past
holds me tight
...
Disgrace pushesh me to glory

glory asks me to stop

Disgrace is forever
...
glory z so effervescent
..

Tuesday, August 19, 2008

Wave

Unseen wave of ocean
with a solo purpose to cover maximum
..
with just one chance
no regret in soul
..
one intention
to cover maximum
to cover maximum
...
roaring high to meet me
....
her divine sprite
unconscious of forgotten fate
...
a trivial fate
a trivial fate
....
but a divine spirit
a divine spirit