JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Fašade Pattern
  • The facade pattern is a software engineering design pattern commonly used with Object-oriented programming. (The name is by analogy to
    an architectural facade.)
  • A facade is an object that provides a simplified interface to a larger body of code, such as a class library.
  • A  facade can: make a software library easier to use and
    understand, since the facade has convenient methods for common tasks;
  • Make code that uses the library more readable, for the
    same reason;~
Purpose
  • Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higherlevel
    interface that makes the subsystem easier to use.

Applications

  • You want to provide a simple interface to a complex subsystem. Subsystems often get more complex as they evolve.
  • Most patterns, when applied, result in more and smaller classes. This makes the subsystem more reusable and easier to customize, but it also becomes harder to use for clients that don't need to customize it.
  •  A facade can provide a simple default view of the subsystem that is good enough for most clients.
  •  Only clients needing more customizability will need to look beyond the facade.
  • There are many dependencies between clients and the implementation classes of an abstraction.
  • Introduce a facade to decouple the subsystem from clients and other subsystems, thereby promoting subsystem independence and portability.
Examples of Facade Pattern

  • The Facade defines a unified, higher level interface to a subsystem that makes it
    easier to use.
  •  Consumers encounter a Facade when ordering from a catalog.
  •  The consumer calls one number and speaks with a customer service representative.
  • The customer service representative acts as a Facade, providing an interface to
    the order fulfillment department, the billing department, and the shipping department.
Example:-

DifficultProduct.java

package com.difficult;

public class DifficultProduct {
  char nameChars[] new char[7];

  public DifficultProduct() {
  }

  public void setFirstNameCharacter(char c) {
    nameChars[0= c;
  }

  public void setSecondNameCharacter(char c) {
    nameChars[1= c;
  }

  public void setThirdNameCharacter(char c) {
    nameChars[2= c;
  }

  public void setFourthNameCharacter(char c) {
    nameChars[3= c;
  }

  public void setFifthNameCharacter(char c) {
    nameChars[4= c;
  }

  public void setSixthNameCharacter(char c) {
    nameChars[5= c;
  }

  public void setSeventhNameCharacter(char c) {
    nameChars[6= c;
  }

  public String getName() {
    return new String(nameChars);
  }

HotelFacade.java

package com.facade;
import java.util.List;

import com.model.HotelSearchService;
import com.model.Room;
import com.model.RoomType;


public class HotelFacade {
  HotelSearchService service;
  
  public HotelFacade() {
    super();
    service=new HotelSearchService();
    for(int i=0;i<20;i++)
      if(i%2==0)
        service.addRoom(new Room(i+1,RoomType.SINGLE,true));
      else
        service.addRoom(new Room(i+1,RoomType.DOUBLE,true));
    // TODO Auto-generated constructor stub
  }

  public String reserve(RoomType rt){
    List<Room>r=service.search(rt);
    if(r.size()>0)
    {
      service.reserve(r.get(0).getRno());
      return "success";
    }
    else
      return "failure";
    
  }
  public void displayAvailableRooms(){
    System.out.println(service.search(RoomType.SINGLE));
    System.out.println(service.search(RoomType.DOUBLE));
    
  }
}

SimpleProductFacade.java

package com.facade;

import com.difficult.DifficultProduct;

public class SimpleProductFacade {
  DifficultProduct difficultProduct;

  public SimpleProductFacade() {
    difficultProduct = new DifficultProduct();
  }

  public void setName(String n) {
    char chars[] = n.toCharArray();
    if (chars.length > 0) {
      difficultProduct.setFirstNameCharacter(chars[0]);
    }
    if (chars.length > 1) {
      difficultProduct.setSecondNameCharacter(chars[1]);
    }
    if (chars.length > 2) {
      difficultProduct.setThirdNameCharacter(chars[2]);
    }
    if (chars.length > 3) {
      difficultProduct.setFourthNameCharacter(chars[3]);
    }
    if (chars.length > 4) {
      difficultProduct.setFifthNameCharacter(chars[4]);
    }
    if (chars.length > 5) {
      difficultProduct.setSixthNameCharacter(chars[5]);
    }
    if (chars.length > 6) {
      difficultProduct.setSeventhNameCharacter(chars[6]);
    }
  }

  public String getName() {
    return difficultProduct.getName();
  }
}

HotelSearchService.java

package com.model;

import java.util.LinkedList;
import java.util.List;

public class HotelSearchService {
  private List<Room> rooms; 
  public HotelSearchService() {
  // TODO Auto-generated constructor stub
    rooms=new LinkedList<Room>();
  }
  public void addRoom(Room room){
    rooms.add(room);
  }
  public List<Room> search(RoomType rt){
    List<Room> searchList=new LinkedList<Room>();
    for(Room r:rooms)
      if(r.isVacant()&& r.getType().equals(rt))
        searchList.add(r);
    return searchList;
  }
  public void reserve(int rno){
    for(Room r:rooms){
      if(r.getRno()==rno)
      {
        r.setVacant(false);
        return;
      }
    }
  }
}

Room.java

package com.model;

public class Room {
  private int rno;
  private RoomType type;
  private boolean vacant;
  
  public int getRno() {
    return rno;
  }
  public void setRno(int rno) {
    this.rno = rno;
  }
  public RoomType getType() {
    return type;
  }
  public void setType(RoomType type) {
    this.type = type;
  }
  public boolean isVacant() {
    return vacant;
  }
  public void setVacant(boolean vacant) {
    this.vacant = vacant;
  }
  public Room(int rno, RoomType type, boolean vacant) {
    super();
    this.rno = rno;
    this.type = type;
    this.vacant = vacant;
  }
  public Room() {
    super();
    // TODO Auto-generated constructor stub
  }
  @Override
  public String toString() {
    // TODO Auto-generated method stub
    return rno+" "+type+" "+vacant;
  }
  
  
}

RoomType.java

package com.model;

public enum RoomType {
  SINGLE, DOUBLE;
}

Client.java

package com.test;

import com.facade.HotelFacade;
import com.model.RoomType;

public class Client {
  public static void main(String arg[]){
    HotelFacade facade=new HotelFacade();
    facade.displayAvailableRooms();
    
    System.out.println(facade.reserve(RoomType.DOUBLE));
    System.out.println(facade.reserve(RoomType.SINGLE));
    
    facade.displayAvailableRooms();
    
  }
}

Output:-



TestFacade.java

package com.test;

import com.facade.SimpleProductFacade;

public class TestFacade {
  public static void main(String args[]) {
    TestFacade t = new TestFacade();
  }

  public TestFacade() {
    SimpleProductFacade simpleProductFacade =
new SimpleProductFacade();
    simpleProductFacade.setName("printer");
    System.out
        .println("This product is a " +
simpleProductFacade.getName
());
  }
}

Output:-


Doanload Zip code

ClientService.java

package convertor;

import interfaces.General;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

public class ClientService
{
  static HashMap<String, String>
userInfo,database,user;

  static General g;
  static String uname,upass;
  /*static{
    database.put("mrityunjay","kumar");
    user.put("mrityunjay", "general");
    database.put("vinay","belagavi");
    user.put("vinay","private");
    database.put("anindita","basak");
    user.put("anindita","special");
  }*/
  public ClientService() {
    userInfo=new HashMap<String, String>();
    database=new HashMap<String, String>();
    user=new HashMap<String, String>();
    database.put("mrityunjay","kumar");
    user.put("mrityunjay""general");
    database.put("vinay","belagavi");
    user.put("vinay","private");
    database.put("anindita","basak");
    user.put("anindita","special");
    //userInfo.put(uname,upass);
  }
    public ClientService(String un,String up)
    {
      //uname=un;
      //upass=up;
      this();
      //userInfo.put(un,up);
      System.out.println("Processing");
      
      decide(un,up);
      System.out.println("Processing over");
    }
  private void decide(String un,String up)
  {
    if(database.containsKey(un)&& database.
containsValue
(up))
    {
      if(user.containsKey(un))
      {
        new Service(user.get(un));
      }
      else
      {
        System.out.println("No User Available");
      }
    }
    else
    {
      System.out.println("No User Available
with this username and password"
);
    }
  }
    /*Set s1=userInfo2.entrySet();
    Iterator itr1=s1.iterator();
    Set s2=database.entrySet();
    Iterator itr2=s1.iterator();
    Set s_u=user.entrySet();
    Iterator itr_u=s_u.iterator();
    while(itr1.hasNext())
    {
      Object obj1=itr1.next();
      System.out.println("Processing level 3");
         while(itr2.hasNext())
         {
           Object obj2=itr2.next();
           System.out.println("Processing level 4");
           if(obj1.equals(obj2))
           {
             while(itr_u.hasNext())
             {
                Object object=itr_u.next();
                System.out.println("Processing level
5"+user.get(object));

                if(obj2.equals(object))
                {
                  System.out.println((String)user.
get(object));

                  new Service((String)user.get(object));
                }
             }
           }
         }
    }
    //database.containsKey(userInfo2.)
  }*/
}

Service.java

package convertor;

import impl.GeneralInfo;
import impl.PrivateInfo;
import impl.SpecialInfo;

public class Service 
{
  
    public Service(String s
    {
    if(s.equalsIgnoreCase("general"))
    {
      try {
        GeneralInfo.class.newInstance().
accessGeneral
();
      catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    else if(s.equalsIgnoreCase("private"))
    {
      try {
        PrivateInfo.class.newInstance().
accessPrivate
();
      catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    else if(s.equalsIgnoreCase("special"))
    {
      try {
        SpecialInfo.class.newInstance().
accessSpecial
();
      catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    else
    {
      System.out.println("Records not found");
    }
  }
}

General.java

package interfaces;
public interface General {
    public void accessGeneral();
}

Private.java

package interfaces;

public interface Private extends General {

    public void accessPrivate();

}

Special.java

package interfaces;

public interface Special extends General {

    public void accessSpecial();

}

GeneralInfo.java

package impl;

import interfaces.General;

public class GeneralInfo implements General {

  @Override
  public void accessGeneral() 
  {
       System.out.println("You are a general user");  

  }

}

PrivateInfo.java

package impl;

import interfaces.Private;

public class PrivateInfo extends
SpecialInfo 
implements Private {

    public void accessPrivate() 
    {
      System.out.println("You are a private user");
    }
       
}

SpecialInfo.java

package impl;

import interfaces.Special;

public class SpecialInfo extends
 
GeneralInfo implements Special{

    public void accessSpecial() {

      System.out.println("You are a special user");
    }

}

TestClient.java

package client;

import convertor.ClientService;

public class TestClient {

  /** 
   @param args
   */
  public static void main(String[] args
  {
    
      //ClientService cs=new ClientService("mrityunjay","kumars");  
     // ClientService cs=new ClientService("vinay","belagavi");
      ClientService cs=new ClientService("anindita","basak");

  }

}

Output:-

Download Zip code

Example:

FacadeCuppaMaker.java

public class FacadeCuppaMaker {  
   boolean teaBagIsSteeped; 
    
   public FacadeCuppaMaker() {
       System.out.println(
         "FacadeCuppaMaker ready to make you a cuppa!");
   }
   
   public FacadeTeaCup makeACuppa() {
       FacadeTeaCup cup = new FacadeTeaCup();
       FacadeTeaBag teaBag = new FacadeTeaBag();
       FacadeWater water = new FacadeWater();
       cup.addFacadeTeaBag(teaBag);
       water.boilFacadeWater();
       cup.addFacadeWater(water);
       cup.steepTeaBag();
       return cup;
   }
}

FacadeTeaBag.java

public class FacadeTeaBag {  
   public FacadeTeaBag() {
       System.out.println("behold the lovely tea bag");
   }
}

FacadeTeaCup.java

public class FacadeTeaCup {  
   boolean teaBagIsSteeped; 
   FacadeWater facadeWater;
   FacadeTeaBag facadeTeaBag;
    
   public FacadeTeaCup() {
       setTeaBagIsSteeped(false);
       System.out.println("behold the beautiful new tea cup");
   }    
   
   public void setTeaBagIsSteeped(boolean isTeaBagSteeped) {
       teaBagIsSteeped = isTeaBagSteeped;
   }
   public boolean getTeaBagIsSteeped() {
       return teaBagIsSteeped;
   }
    
   public void addFacadeTeaBag(FacadeTeaBag facadeTeaBagIn) {
       facadeTeaBag = facadeTeaBagIn;
       System.out.println("the tea bag is in the tea cup");
   }
   
   public void addFacadeWater(FacadeWater facadeWaterIn) {
       facadeWater = facadeWaterIn;
       System.out.println("the water is in the tea cup");  
   }   
   
   public void steepTeaBag() {
       if ( (facadeTeaBag != null&& 
             ( (facadeWater != null&& 
               (facadeWater.getWaterIsBoiling()) )
          ) {
          System.out.println("the tea is steeping in the cup");
          setTeaBagIsSteeped(true);
       else {
          System.out.println("the tea is not steeping in the cup");
          setTeaBagIsSteeped(false);
       }           
   }
   
   public String toString() {
       if (this.getTeaBagIsSteeped()) {
           return ("A nice cuppa tea!");
       else {
           String tempString = new String("A cup with ");
           if (facadeWater != null) {
                if (facadeWater.getWaterIsBoiling()) {
                    tempString = (tempString + "boiling water ");
                else {
                    tempString = (tempString + "cold water ");
                }
           else {
                tempString = (tempString + "no water ");
           }
 
           if (facadeTeaBag != null) {
                tempString = (tempString + "and a tea bag");
           else {
                tempString = (tempString + "and no tea bag");
           
           return tempString;
       }
                        
   }
}

FacadeWater.java

public class FacadeWater {  
   boolean waterIsBoiling; 
    
   public FacadeWater() {
       setWaterIsBoiling(false);
       System.out.println("behold the wonderous water");       
   }
   
   public void boilFacadeWater() {
       setWaterIsBoiling(true);
       System.out.println("water is boiling");
   }
   
   public void setWaterIsBoiling(boolean isWaterBoiling) {
       waterIsBoiling = isWaterBoiling;
   }
   public boolean getWaterIsBoiling() {
       return waterIsBoiling;
   }
}

TestFacade.java

class TestFacade {
   public static void main(String[] args) {
       FacadeCuppaMaker cuppaMaker = new FacadeCuppaMaker();
       FacadeTeaCup teaCup = cuppaMaker.makeACuppa();
       System.out.println(teaCup);
   }
}

Output:-


Download Zip Code