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
  • Command Design pattern - Implementation [Menu]



    Click here to watch in Youtube : https://www.youtube.com/watch?v=04iqkBZe-JE

    Click the below Image to Enlarge
    Command Design pattern - Implementation [Menu]

    Command Design pattern - Implementation [Menu]
    Command Design pattern - Implementation [Menu] - Class Diagram

    WordDocument.java

    /*
     *  Receiver
     */

    public class WordDocument
    {
    public void open()
    {
    System.out.println("Document Opened");
    }

    public void save()
    {
    System.out.println("Document Saved");
    }

    public void close()
    {
    System.out.println("Document Closed");
    }

    }

    Command.java

    public interface Command
    {
    public void execute();
    }

    OpenCommand.java

    public class OpenCommand implements Command
    {
    private WordDocument wordDocument;

    public OpenCommand( WordDocument wordDocument )
    {
    this.wordDocument = wordDocument;
    }

    @Override
    public void execute()
    {
    wordDocument.open();
    }
    }

    SaveCommand.java

    public class SaveCommand implements Command
    {

    private WordDocument wordDocument;

    public SaveCommand( WordDocument wordDocument )
    {
    this.wordDocument = wordDocument;
    }

    @Override
    public void execute()
    {
    wordDocument.save();
    }
    }


    CloseCommand.java

    public class CloseCommand implements Command
    {
    private WordDocument wordDocument;

    public CloseCommand( WordDocument wordDocument )
    {
    this.wordDocument = wordDocument;
    }

    @Override
    public void execute()
    {
    wordDocument.close();
    }
    }


    MenuOptions.java

    /*
     *  Invoker
     */

    public class MenuOptions
    {
    private Command openCommand;
    private Command saveCommand;
    private Command closeCommand;

    public MenuOptions( Command open, Command save, Command close )
    {
    this.openCommand = open;
    this.saveCommand = save;
    this.closeCommand = close;
    }

    public void clickOpen()
    {
    openCommand.execute();
    }

    public void clickSave()
    {
    saveCommand.execute();
    }

    public void clickClose()
    {
    closeCommand.execute();
    }
    }

    Client.java

    public class Client
    {
    public static void main( String[] args )
    {
    WordDocument wordDocument = new WordDocument();
    Command openCommand = new OpenCommand(wordDocument);
    Command saveCommand = new SaveCommand(wordDocument);
    Command closeCommand = new CloseCommand(wordDocument);
    MenuOptions menu = new MenuOptions(openCommand, saveCommand, closeCommand);
    menu.clickOpen();
    menu.clickSave();
    menu.clickClose();
    }

    }

    Output

    Document Opened
    Document Saved
    Document Closed

    See also:

  • Command Design pattern - Introduction
  • Command Design pattern - Real time example [Hotel]
  • Command Design pattern - Real time example [Menu]
  • Command Design pattern - Class Diagram
  • Command Design pattern - Sequence Diagram
  • Command Design pattern - Object Creation and flow
  • Command Design pattern - Key points
  • All Design Patterns Links
  • Command Design pattern - Real time example [Menu]

    Interpreter Design pattern - Class Diagram

    Interpreter Design pattern - Real time example [JVM]

    Friday 7 February 2014

    Interpreter Design pattern - Real time example [Google Translator]

    Interpreter Design pattern - Introduction

    Interpreter Design pattern - Key points

    Wednesday 5 February 2014

    Command Design pattern - Object Creation and flow

    Command Design pattern - Sequence Diagram

    Command Design pattern - Class Diagram

    Saturday 1 February 2014

    State Design pattern - Implementation [LED TV]




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

    Click the below Image to Enlarge
    State Design pattern - Implementation [LED TV]

    State Design pattern - Implementation [LED TV]
    State Design pattern - Implementation [LED TV] - Class Diagram














































    StateOfLedTv.java

    public interface StateOfLedTv
    {

    public void doTurnOnOrOff();

    }


    LEDTVOffState.java

    public class LEDTVOffState implements StateOfLedTv
    {

    @Override
    public void doTurnOnOrOff()
    {
    System.out.println("TV is turned OFf");
    }

    }

    LEDTVOnState.java

    public class LEDTVOnState implements StateOfLedTv
    {

    @Override
    public void doTurnOnOrOff()
    {
    System.out.println("TV is turned ON");
    }

    }


    LEDTVContext.java

    public class LEDTVContext implements StateOfLedTv
    {

    private StateOfLedTv stateOfLedTv;

    public StateOfLedTv getStateOfLedTv()
    {
    return stateOfLedTv;
    }

    public void setStateOfLedTv(StateOfLedTv stateOfLedTv)
    {
    this.stateOfLedTv = stateOfLedTv;

    }

    @Override
    public void doTurnOnOrOff()
    {
    System.out.println("Current state : " + stateOfLedTv.getClass().getName());
    stateOfLedTv.doTurnOnOrOff();

    }

    }

    TVRemote.java

    public class TVRemote
    {

    public static void main( String[] args )
    {
    LEDTVContext context = new LEDTVContext();
    StateOfLedTv ledTvOnState = new LEDTVOnState();
    StateOfLedTv ledTvOffState = new LEDTVOffState();

    context.setStateOfLedTv(ledTvOnState);
    context.doTurnOnOrOff();

    context.setStateOfLedTv(ledTvOffState);
    context.doTurnOnOrOff();
    }

    }

    Output

    Current state : LEDTVOnState
    TV is turned ON
    Current state : LEDTVOffState
    TV is turned OFf

    See also:

  • State Design pattern - Introduction
  • State Design pattern - Real time example [ATM]
  • State Design pattern - Real time example [TV Remote]
  • State Design pattern - Real time example [Vending Machine]
  • State Design pattern - Real time example [Project]
  • State Design pattern - Class Diagram
  • State Design pattern - Implementation [ATM]
  • State Design pattern - Implementation [Vending Machine]
  • State Design pattern - Implementation [Project]
  • State Design pattern - Keypoints
  • All Design Patterns Links
  • State Design pattern - Implementation [Project]




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

    Click the below Image to Enlarge
    State Design pattern - Implementation [Project]


    State Design pattern - Implementation [Project] - Class Diagram












































    ProjectState.java

    public interface ProjectState
    {

    public void doRequirmentGathering();

    public void doDesignAndArchitecture();

    public void doDevelopement();

    public void doTesting();


    }

    RequirementGatheringState.java

    public class RequirementGatheringState implements ProjectState
    {

    @Override
    public void doRequirmentGathering()
    {
    System.out.println("'RequirmentGathering' has been started .............");

    System.out.println("'RequirmentGathering' has been finished by 7 days .............");

    }

    @Override
    public void doDesignAndArchitecture()
    {
    System.out.println("Cannot do 'DesignAndArchitecture' because Project is in 'RequirmentGathering' Phase...");

    }

    @Override
    public void doDevelopement()
    {
    System.out.println("Cannot do 'developement' because Project is in 'RequirmentGathering' Phase...");

    }

    @Override
    public void doTesting()
    {
    System.out.println("Cannot do 'Testing' because Project is in 'RequirmentGathering' Phase...");

    }

    }

    DesignAndArchitectureState.java

    public class DesignAndArchitectureState implements ProjectState
    {
    @Override
    public void doRequirmentGathering()
    {
    System.out.println("'RequirmentGathering'[Clarification of Requirement] you can do in ''DesignAndArchitecture'' Phase .......");
    }

    @Override
    public void doDesignAndArchitecture()
    {
    System.out.println("'DesignAndArchitecture' has been started .............");

    System.out.println("'DesignAndArchitecture' has been finished by 10 days .............");
    }

    @Override
    public void doDevelopement()
    {
    System.out.println("Cannot do 'developement' because Project is in 'DesignAndArchitecture' Phase...");

    }

    @Override
    public void doTesting()
    {
    System.out.println("Cannot do 'Testing' because Project is in 'DesignAndArchitecture' Phase...");

    }

    }

    DevelopementState.java

    public class DevelopementState implements ProjectState
    {
    @Override
    public void doRequirmentGathering()
    {
    System.out.println("Cannot do 'RequirmentGathering' because Project is in 'Developement' Phase...");
    }

    @Override
    public void doDesignAndArchitecture()
    {
    System.out.println("'DesignAndArchitecture'[Clarification of DesignAndArchitecture] you can do in ''Developement'' Phase .......");
    }

    @Override
    public void doDevelopement()
    {
    System.out.println("'Developement' has been started .............");

    System.out.println("'Developement' has been finished by 40 days .............");

    }

    @Override
    public void doTesting()
    {
    System.out.println("Cannot do 'Testing' because Project is in 'Developement' Phase...");

    }

    }

    TestingState.java

    public class TestingState implements ProjectState
    {
    @Override
    public void doRequirmentGathering()
    {
    System.out.println("Cannot do 'RequirmentGathering' because Project is in 'Testing' Phase...");
    }

    @Override
    public void doDesignAndArchitecture()
    {
    System.out.println("Cannot do 'DesignAndArchitecture' because Project is in 'Testing' Phase...");
    }

    @Override
    public void doDevelopement()
    {
    System.out.println("'Developement'[Bug fixes] you can do in ''Testing'' Phase .......");

    }

    @Override
    public void doTesting()
    {
    System.out.println("'Testing' has been started .............");

    System.out.println("'Testing' has been finished by 20 days .............");

    }

    }

    Project.java

    // This is like Context
    public class Project implements ProjectState
    {

    private ProjectState projectState;

    public Project()
    {
    projectState = new RequirementGatheringState();
    }

    public ProjectState getProjectState()
    {
    return projectState;
    }

    public void setProjectState( ProjectState projectState )
    {
    this.projectState = projectState;
    }

    @Override
    public void doRequirmentGathering()
    {
    projectState.doRequirmentGathering();
    /*
    * Once RequirmentGathering is finished project internal state
    * will be moved to DesignAndArchitectureState
    */
    if( projectState instanceof RequirementGatheringState )
    {
    setProjectState(new DesignAndArchitectureState());
    System.out.println("\nProject internal state moved to : " + projectState.getClass().getName() +"\n" );
    }

    }

    @Override
    public void doDesignAndArchitecture()
    {
    projectState.doDesignAndArchitecture();

    /*
    * Once DesignAndArchitecture is finished project internal state
    * will be moved to DevelopementState
    */
    if( projectState instanceof DesignAndArchitectureState )
    {
    setProjectState(new DevelopementState());
    System.out.println("\nProject internal state moved to : " + projectState.getClass().getName() +"\n" );
    }

    }

    @Override
    public void doDevelopement()
    {
    projectState.doDevelopement();

    /*
    * Once Developement is finished project internal state will be
    * moved to TestingState
    */

    if( projectState instanceof DevelopementState )
    {
    setProjectState(new TestingState());
    System.out.println("\nProject internal state moved to : " + projectState.getClass().getName() +"\n" );
    }

    }

    @Override
    public void doTesting()
    {
    projectState.doTesting();

    }

    }

    Client.java

    public class Client
    {

    public static void main( String[] args )
    {
    /*
    * Initially Project in 'RequirementGatheringState'
    */
    Project project = new Project();

    System.out.println("Project internal state is  : " + project.getProjectState().getClass().getName()+"\n");

    project.doDesignAndArchitecture();
    project.doDevelopement();
    project.doTesting();
    project.doRequirmentGathering();

    System.out.println();

    project.doDesignAndArchitecture();
    project.doTesting();
    project.doDevelopement();
    project.doRequirmentGathering();

    }

    }

    Output

    Project internal state is  : RequirementGatheringState

    Cannot do 'DesignAndArchitecture' because Project is in 'RequirmentGathering' Phase...
    Cannot do 'developement' because Project is in 'RequirmentGathering' Phase...
    Cannot do 'Testing' because Project is in 'RequirmentGathering' Phase...
    'RequirmentGathering' has been started .............
    'RequirmentGathering' has been finished by 7 days .............

    Project internal state moved to : DesignAndArchitectureState


    'DesignAndArchitecture' has been started .............
    'DesignAndArchitecture' has been finished by 10 days .............

    Project internal state moved to : DevelopementState

    Cannot do 'Testing' because Project is in 'Developement' Phase...
    'Developement' has been started .............
    'Developement' has been finished by 40 days .............

    Project internal state moved to : TestingState

    Cannot do 'RequirmentGathering' because Project is in 'Testing' Phase...

    See also:

  • State Design pattern - Introduction
  • State Design pattern - Real time example [ATM]
  • State Design pattern - Real time example [TV Remote]
  • State Design pattern - Real time example [Vending Machine]
  • State Design pattern - Real time example [Project]
  • State Design pattern - Class Diagram
  • State Design pattern - Implementation [ATM]
  • State Design pattern - Implementation [Vending Machine]
  • State Design pattern - Implementation [LED TV]
  • State Design pattern - Keypoints
  • All Design Patterns Links