Showing posts with label DESIGN PATTERNS. Show all posts
Showing posts with label DESIGN PATTERNS. Show all posts

Thursday, October 31, 2013

FACADE DESIGN PATTERN IN JAVA

/*
FACADE DESIGN PATTERN IN JAVA
*/
class Client
{
public static void main(String args[]){

OrderFacade orderFacade = new OrderFacade();

orderFacade.placeOrder("OR123456");

System.out.println("Order processing completed");
}

}

 class OrderFacade {
private Payment pymt = new Payment();
private Inventory inventry = new Inventory();

public void placeOrder(String orderId) {

String step1 = inventry.checkInventory(orderId);

String step2 = pymt.deductPayment(orderId);

System.out.println("Following steps completed:" + step1+ " & " + step2);

}

}
 class Payment {
public String deductPayment(String orderID)
{
System.out.println("TOKEN "+orderID+" recieved\nProcessing payment...\n");
return "Payment deducted successfully";

}
}

class Inventory {
public String checkInventory(String OrderId)
{
System.out.println("TOKEN "+OrderId+" recieved\nProcessing Inventory...\n");
return "Inventory checked";
}
}






/*
OUTPUT:
TOKEN OR123456 recieved
Processing Inventory...

TOKEN OR123456 recieved
Processing payment...

Following steps completed:Inventory checked & Payment deducted successfully
Order processing completed
*/

OBSERVER DESIGN PATTERN IN JAVA

/*
OBSERVER DESIGN PATTERN IN JAVA
*/
import java.util.*;
class subject
{

person p1,p2;

subject()
{
p1=new person();
p2=new person();
}
void notify(boolean a)
{
if(a)
{
p2.state=p1.state="changed";

System.out.println(p1);
System.out.println(p2);
}
else
{
System.out.println(p1);
System.out.println(p2);
}

}
public static void main(String[] args)
{
boolean executed=false;
String choice;
subject s=new subject();
Scanner sc=new Scanner(System.in);
while(!executed)
{
System.out.println("SHOULD WE CHANGE STATE ?  Y/N");
choice=sc.nextLine();
if(choice.equals("Y") || choice.equals("y"))
{
s.notify(true);
executed=true;
}
else
s.notify(false);
}
}
}
class person
{//person is the observer;
String state;
person()
{
state="unchanged";
}
public String toString()
{
return state;
}

}

/*
OUTPUT
SHOULD WE CHANGE STATE ?  Y/N
n
unchanged
unchanged
SHOULD WE CHANGE STATE ?  Y/N
n
unchanged
unchanged
SHOULD WE CHANGE STATE ?  Y/N
n
unchanged
unchanged
SHOULD WE CHANGE STATE ?  Y/N
n
unchanged
unchanged
SHOULD WE CHANGE STATE ?  Y/N
y
changed
changed
*/

Wednesday, October 30, 2013

SINGLETON PATTERN IN JAVA

/*
TEJAS J GHALSASI
TEIT
SINGLETON PATTERN IN JAVA
*/
import java.util.*;
public class NoticeBoard {
   
    private static NoticeBoard a;
    String message;
    /**
     * Create private constructor
     */
    private NoticeBoard(){
       
    }
    /**
     * Create a static method to get instance.
     */
    public static NoticeBoard getInstance(){
        if(a == null){
            a = new NoticeBoard();
        }
        return a;
    }
   
    public void getMessage(String msg){
        message=msg;
        System.out.println("message updated \n");
    }
   
    public void postMessage(){
       
        System.out.println("Current message is: "+message);
    }
   
    public static void main(String a[]){
    Scanner sc=new Scanner(System.in);
    NoticeBoard nb = NoticeBoard.getInstance();
        NoticeBoard nb1 = NoticeBoard.getInstance();
        System.out.println("enter message");
        String msg=sc.nextLine();
       
nb.getMessage(msg);
System.out.println("message stored in nb");
nb.postMessage();

System.out.println("message stored in nb1");
nb1.postMessage();
     
      System.out.println("enter a new message ");
       msg=sc.nextLine();
     
nb.getMessage(msg);
System.out.println("message stored in nb");
nb.postMessage();

System.out.println("message stored in nb1");
nb1.postMessage();
    }
}
/*
THIS PROGRAM IS CODED BY TG
*/