jobs4timesLogo jobs4timesLogo

Session Facade Design Pattern

index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Index Page</title>
</head>
<body>

<a href="/javaT/findStock.jsp">Find Stock Information</a>

</body>
</html>
findStock.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Find Stock</title>
</head>
<body>
<form action="stock">
<table>
 <tr><td>Short Name : </td><td><input type="text" name="shortName"/></td></tr>
 <tr><td colspan="2"><input type="submit" value="Find"/></td></tr>
</table>
</form>
</body>
</html>


stockDetails.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<jsp:useBean id="stock" type="session.facade.vo.StockVO" scope="request"/>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Stock Information</title>
</head>
<body>
<p style="color:blue">
 Short Name : ${stock.shortName} <br>
 Price : ${stock.price} <br>
 Listed Date : ${stock.listDate}
</p>
</body>
</html>
package session.facade.bo;

import java.io.Serializable;

public class GetStock implements Serializable{
 
 private String shortName;

 //setters and getters
 
public String getShortName() {
	return shortName;
}

public void setShortName(String shortName) {
	this.shortName = shortName;
} 
 
}
package session.facade.bo;

import java.util.Date;

public class StockInfo {
	
  private int id;
  private float price;
  private Date listDate;
  
  //getters and setters
  
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public float getPrice() {
	return price;
}
public void setPrice(float price) {
	this.price = price;
}
public Date getListDate() {
	return listDate;
}
public void setListDate(Date listDate) {
	this.listDate = listDate;
}
    
}
package session.facade.dto;

import java.util.Date;

public class SearchStock {
	
  private String shortName;
  private Date time;
  
  //getters and setters
  
public String getShortName() {
	return shortName;
}
public void setShortName(String shortName) {
	this.shortName = shortName;
}
public Date getTime() {
	return time;
}
public void setTime(Date time) {
	this.time = time;
}
  
}
package session.facade.dto;

import java.util.Date;

public class Stock {
  
  private int id;
  private String shortName;
  private String companyName;
  private Date listedDate;
  private float price;
  
  //getters and setters
  
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getShortName() {
	return shortName;
}
public void setShortName(String shortName) {
	this.shortName = shortName;
}
public String getCompanyName() {
	return companyName;
}
public void setCompanyName(String companyName) {
	this.companyName = companyName;
}
public Date getListedDate() {
	return listedDate;
}
public void setListedDate(Date listedDate) {
	this.listedDate = listedDate;
}
public float getPrice() {
	return price;
}
public void setPrice(float price) {
	this.price = price;
} 
  
}
package session.facade.bo;

import java.util.Date;

public class Quote {
 private int id;
 private Date time;
 
 //setters and getters
 
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public Date getTime() {
	return time;
}
public void setTime(Date time) {
	this.time = time;
}
 
}
package session.facade.bo;

public class StockDetail {
	
  private int id;
  private String shortName;
  private String companyName;
  
  //getters and setters
  
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getShortName() {
	return shortName;
}
public void setShortName(String shortName) {
	this.shortName = shortName;
}
public String getCompanyName() {
	return companyName;
}
public void setCompanyName(String companyName) {
	this.companyName = companyName;
}  
    
}
package session.facade.delegate;

import java.util.Date;

import session.facade.StockFacade;
import session.facade.StockFacadeLocator;
import session.facade.dto.SearchStock;
import session.facade.dto.Stock;
import session.facade.vo.SearchVO;
import session.facade.vo.StockVO;

public class StockDelegate {
	
public StockVO search(SearchVO searchVO){
  StockVO stockVO=null;
  SearchStock searchStock=null;
  Stock stock=null;
  StockFacade stockFacade=null;
  StockFacadeLocator stockFacadeLocator=null;
  
   searchStock=new SearchStock();
   searchStock.setShortName(searchVO.getShortName());   
   searchStock.setTime(new Date());
   
   stockFacadeLocator=new StockFacadeLocator();
   stockFacade=stockFacadeLocator.getStockFacade();
   stock=stockFacade.findStock(searchStock);
   
   stockVO=new StockVO();
   stockVO.setShortName(stock.getShortName());
   stockVO.setPrice(String.valueOf(stock.getPrice()));
   stockVO.setListDate(stock.getListedDate().toString());
   
   return stockVO;
}

}
package session.facade;

import session.facade.bo.GetStock;
import session.facade.bo.Quote;
import session.facade.bo.StockDetail;
import session.facade.bo.StockInfo;
import session.facade.dto.SearchStock;
import session.facade.dto.Stock;
import session.facade.service.BSEStockTrade;
import session.facade.service.BSEStockTradeImpl;
import session.facade.service.FindStockDetail;
import session.facade.service.FindStockDetailImpl;

public class StockFacade {

public Stock findStock(SearchStock searchStock){
	
  FindStockDetail findStockDetail=null;
  BSEStockTrade bseStockTrade=null;
  Quote quote=null;
  StockInfo stockInfo=null;
  GetStock getStock=null;
  StockDetail stockDetail=null;
  Stock stock=null;
  
  getStock=new GetStock();
  getStock.setShortName(searchStock.getShortName());
  
  findStockDetail=new FindStockDetailImpl();
  stockDetail=findStockDetail.findStock(getStock);
  
  quote=new Quote();
  quote.setId(stockDetail.getId());
  quote.setTime(searchStock.getTime());
  
  bseStockTrade=new BSEStockTradeImpl();
  stockInfo=bseStockTrade.getStockPrice(quote);
  
  stock=new Stock();
  stock.setId(stockInfo.getId());
  stock.setShortName(stockDetail.getShortName());
  stock.setCompanyName(stockDetail.getCompanyName());
  stock.setListedDate(stockInfo.getListDate());
  stock.setPrice(stockInfo.getPrice());
  
  return stock;
}

}
package session.facade.service;

import session.facade.bo.Quote;
import session.facade.bo.StockInfo;

public interface BSEStockTrade {
   StockInfo getStockPrice(Quote quote);
}
package session.facade.service;

import java.util.Date;

import session.facade.bo.Quote;
import session.facade.bo.StockInfo;



public class BSEStockTradeImpl implements BSEStockTrade {

@Override
public StockInfo getStockPrice(Quote quote) {
 float price=0.0f;
 StockInfo stockInfo=null;
 
 if(quote!=null){
	if(quote.getId()==1001){
		price=323.34f;
	}else if (quote.getId()==1002) {
		price=323.23f;
	}
 }
 
stockInfo=new StockInfo();
stockInfo.setId(quote.getId());
stockInfo.setPrice(price);
stockInfo.setListDate(new Date());

return stockInfo;
}

}
package session.facade.service;

import session.facade.bo.GetStock;
import session.facade.bo.StockDetail;

public interface FindStockDetail {
  StockDetail findStock(GetStock getStock);
}
package session.facade.service;

import session.facade.bo.GetStock;
import session.facade.bo.StockDetail;

public class FindStockDetailImpl implements FindStockDetail {

@Override
public StockDetail findStock(GetStock getStock) {
  StockDetail stockDetail=null;
  stockDetail=new StockDetail();
  
  if(getStock.getShortName().equals("ICICIBAN")){
    stockDetail.setId(1001);
    stockDetail.setShortName(getStock.getShortName());
    stockDetail.setCompanyName("ICICI BANK Pvt Ltd");
  }else if(getStock.getShortName().equals("IBM")){
	stockDetail.setId(1002);
	stockDetail.setShortName(getStock.getShortName());
	stockDetail.setCompanyName("IBM INDIA Pvt Ltd");
  }
	
	return stockDetail;
}

}
package session.facade.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import session.facade.delegate.StockDelegate;
import session.facade.vo.SearchVO;
import session.facade.vo.StockVO;

public class FindStockServlet extends HttpServlet {

@Override
protected void service(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException{
	String shortName=null;
	SearchVO searchVO=null;
	StockVO stockVO=null;
	StockDelegate stockDelegate=null;
	
	shortName=request.getParameter("shortName");
	
	
	searchVO=new SearchVO();
	searchVO.setShortName(shortName);
	
	stockDelegate=new StockDelegate();
	stockVO=stockDelegate.search(searchVO);
	
	request.setAttribute("stock", stockVO);
	RequestDispatcher rd=request.getRequestDispatcher("stockDetails.jsp");
	rd.forward(request, response);
}

}
package session.facade;

public class StockFacadeLocator {

 public StockFacade getStockFacade(){
	 //look up logic
	 return new StockFacade();
 }
}
package session.facade.vo;

import java.io.Serializable;

public class SearchVO implements Serializable{
 private String shortName;
 
 //getters and setters

public String getShortName() {
	return shortName;
}

public void setShortName(String shortName) {
	this.shortName = shortName;
}
 
}
package session.facade.vo;

import java.io.Serializable;

public class StockVO implements Serializable{
 private String shortName;
 private String listDate;
 private String price;
 
 //setters and getters
 
public String getShortName() {
	return shortName;
}
public void setShortName(String shortName) {
	this.shortName = shortName;
}
public String getListDate() {
	return listDate;
}
public void setListDate(String listDate) {
	this.listDate = listDate;
}
public String getPrice() {
	return price;
}
public void setPrice(String price) {
	this.price = price;
}

}
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
xmlns="http://java.sun.com/xml/ns/javaee"   
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"   
id="WebApp_ID" version="3.0">  

<display-name>Session Facade</display-name>
<servlet>
 <servlet-name>facade</servlet-name>
 <servlet-class>session.facade.servlet.FindStockServlet</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>facade</servlet-name>
 <url-pattern>/stock</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
 
</web-app>


output :
http://localhost:8001/javaT/

(OR)

http://localhost:8001/javaT/stock?shortName=ICICIBAN
Short Name : ICICIBAN 
Price : 323.34 
Listed Date : Mon Mar 21 16:40:02 IST 2016

(OR)

http://localhost:8001/javaT/stock?shortName=IBM
Short Name : IBM 
Price : 323.23 
Listed Date : Mon Mar 21 16:41:33 IST 2016


BACK