Sunday, 9 February 2014

Interpreter Design pattern - Implementation [Date]



Click here to watch in Youtube : https://www.youtube.com/watch?v=sBx4bw1T4Ek

Click the below Image to Enlarge
Interpreter Design pattern - Implementation [Date]















Interpreter Design pattern - Implementation [Date] - Class Diagram


Context.java

import java.util.Date;

public class Context
{
public String expression;
public Date   date;

public String getExpression()
{
return expression;
}

public void setExpression( String expression )
{
this.expression = expression;
}

public Date getDate()
{
return date;
}

public void setDate( Date date )
{
this.date = date;
}

}


AbstractExpression.java

public abstract class AbstractExpression
{
public abstract void evaluate( Context context );
}

DayExpression.java

import java.util.Date;

public class DayExpression extends AbstractExpression
{

@Override
public void evaluate( Context context )
{
String expression = context.getExpression();
Date date = context.getDate();
Integer day = new Integer(date.getDate());
String tempExpression = expression.replaceAll("DD", day.toString());
context.setExpression(tempExpression);
}
}

MonthExpression.java

import java.util.Date;


public class MonthExpression extends AbstractExpression
{

@Override
public void evaluate( Context context )
{
String expression = context.getExpression();
Date date = context.getDate();
Integer month = new Integer(date.getMonth()+1);
String tempExpression = expression.replaceAll("MM", month.toString());
context.setExpression(tempExpression);
}

}

YearExpression.java

import java.util.Date;

public class YearExpression extends AbstractExpression
{

@Override
public void evaluate( Context context )
{
String expression = context.getExpression();
Date date = context.getDate();
Integer year = new Integer(date.getYear() + 1900);
String tempExpression = expression.replaceAll("YYYY", year.toString());
context.setExpression(tempExpression);
}

}


Client.java

import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class Client
{

public static void main( String[] args )
{

System.out.println("Please select the Expression  : 'MM-DD-YYYY' or 'YYYY-MM-DD'");
Scanner scanner = new Scanner(System.in);
String expression = scanner.next();

Context context = new Context();
context.setExpression(expression);
context.setDate(new Date());

ArrayList<AbstractExpression> expressionOrderList = getExpressionOrder(context);

System.out.println("Input : " + context.getExpression() + " : " + new Date());

for( AbstractExpression abstractExpression : expressionOrderList )
{
abstractExpression.evaluate(context);
System.out.println(abstractExpression.getClass().getName() + " Evaluated     : "
               + context.getExpression());

}

System.out.println("Output : " + context.getExpression());
}

private static ArrayList<AbstractExpression> getExpressionOrder( Context context )
{
ArrayList<AbstractExpression> expressionOrderList = new ArrayList<AbstractExpression>();
String[] strArray = context.getExpression().split("-");
for( String string : strArray )
{
if( string.equalsIgnoreCase("MM") )
{
expressionOrderList.add(new MonthExpression());
}
else if( string.equalsIgnoreCase("DD") )
{
expressionOrderList.add(new DayExpression());
}
else
{
expressionOrderList.add(new YearExpression());
}

}
return expressionOrderList;
}

}

Output

Please select the Expression  : 'MM-DD-YYYY' or 'YYYY-MM-DD'
MM-DD-YYYY
Input : MM-DD-YYYY : Sun Feb 09 22:23:45 IST 2014
MonthExpression Evaluated     : 2-DD-YYYY
DayExpression Evaluated     : 2-9-YYYY
YearExpression Evaluated     : 2-9-2014
Output : 2-9-2014

Please select the Expression  : 'MM-DD-YYYY' or 'YYYY-MM-DD'
YYYY-MM-DD
Input : YYYY-MM-DD : Sun Feb 09 22:23:55 IST 2014
YearExpression Evaluated     : 2014-MM-DD
MonthExpression Evaluated     : 2014-2-DD
DayExpression Evaluated     : 2014-2-9
Output : 2014-2-9

See also:

  • Interpreter Design pattern - Introduction
  • Interpreter Design pattern - Real time example [Google Translator]
  • Interpreter Design pattern - Real time example [JVM]
  • Interpreter Design pattern - Class Diagram
  • Interpreter Design pattern - Key points
  • All Design Patterns Links
  • No comments:

    Post a Comment