Login
Forgot your password?
JavaMad logo
Popular Questions
Design Patterns Interview Questions
 
plz expalin my all design pattern questions its urgent plzzz

February 21 2013
 
what is adapter design pattern?

February 21 2013
 
what is builder design pattern?

February 21 2013
 
what is factory design pattern?

February 21 2013
 
plz expalin proxy design pattern.

February 21 2013
 
What is Visitor Design Pattern

December 13 2011
 
visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures. It is one way to easily follow the open/closed principle.

Ex :
package patterns.visited;

import patterns.visitors.Visitor;

public interface Shopping {
void accept(Visitor visitor);
}

package patterns.visited;

import patterns.visitors.Visitor;

public class IPhone implements Shopping {

Float price;
Float discount;
Boolean isAvailable;
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public Float getDiscount() {
return discount;
}
public void setDiscount(Float discount) {
this.discount = discount;
}
public Boolean getIsAvailable() {
return isAvailable;
}
public void setIsAvailable(Boolean isAvailable) {
this.isAvailable = isAvailable;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}

}



package patterns.visited;

import patterns.visitors.Visitor;

public class MacBook implements Shopping {

Float price;
Float discount;
Boolean isAvailable;
@Override
public void accept(Visitor visitor) {
visitor.visit(this);

}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public Float getDiscount() {
return discount;
}
public void setDiscount(Float discount) {
this.discount = discount;
}
public Boolean getIsAvailable() {
return isAvailable;
}
public void setIsAvailable(Boolean isAvailable) {
this.isAvailable = isAvailable;
}

}


package patterns.visited;

import patterns.visitors.Visitor;

public class MacPro implements Shopping {

Float price;
Float discount;
Boolean isAvailable;

public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public Float getDiscount() {
return discount;
}
public void setDiscount(Float discount) {
this.discount = discount;
}
public Boolean getIsAvailable() {
return isAvailable;
}
public void setIsAvailable(Boolean isAvailable) {
this.isAvailable = isAvailable;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}

}

package patterns.visitors;

import patterns.visited.IPhone;
import patterns.visited.MacBook;
import patterns.visited.MacPro;

public interface Visitor {
void visit(MacBook macBook);
void visit(MacPro macPro);
void visit(IPhone iPhone);
}

package patterns.visitors;

import patterns.visited.IPhone;
import patterns.visited.MacBook;
import patterns.visited.MacPro;

public class VisitorImpl implements Visitor {

@Override
public void visit(MacBook macBook) {
macBook.setDiscount(10f);
macBook.setIsAvailable(Boolean.TRUE);
macBook.setPrice(65000f);
System.out.println(macBook.getPrice());
System.out.println(macBook.getDiscount());
System.out.println(macBook.getIsAvailable());
}

@Override
public void visit(MacPro macPro) {
macPro.setDiscount(5f);
macPro.setIsAvailable(Boolean.TRUE);
macPro.setPrice(95000f);
System.out.println(macPro.getPrice());
System.out.println(macPro.getDiscount());
System.out.println(macPro.getIsAvailable());
}

@Override
public void visit(IPhone iPhone) {
iPhone.setDiscount(20f);
iPhone.setIsAvailable(Boolean.TRUE);
iPhone.setPrice(35000f);
System.out.println(iPhone.getPrice());
System.out.println(iPhone.getDiscount());
System.out.println(iPhone.getIsAvailable());
}

}


package patterns.demo;

import patterns.visited.IPhone;
import patterns.visited.MacBook;
import patterns.visited.Shopping;
import patterns.visitors.VisitorImpl;

public class VisitorDemo {
public static void main(String[] args) {
Shopping shopping=new IPhone();
shopping.accept(new VisitorImpl());
shopping=new MacBook();
shopping.accept(new VisitorImpl());
}
}


W

December 13 2011
 
what is factory, singleton design patterns?

May 18 2011
 
Singleton design pattern is used to maka a allow to create only one object per a class.


Fectory
-------

Factory Method is method that has a capability to create obj of a class

Ex: Calender c1=Calender.getInstance();

Here getInstance() is a factory Method

May 18 2011
 
what is the purpose of AbstractFactory and Factory
design patteren how to use in our application.
Please give example?

May 18 2011
 
Abstract Factory : Group mutiple related Factories & return one factory based on some condition.

Factory Method : Group related subclasses and return one among them based on some condition.

Singletons Pattern : create & return only one instance of class regardless of how many times you are instantiating it(calling new XYZ() or getInstance() method )

May 18 2011
 
how many desing patterns are there in java?

May 18 2011
 
There is no count for it. Every good design practice & technique will become a design pattern.

Pattern divided based on Tiers or based on their creation, structures and behaviors. Trying to Listing some of them :-

Pattern Based on Tiers :

1.Presentation Tier Pattern :
# Intercepting Filter
# Front Controller
# ViewHelper
# DispactherView/Compsoite View
# Service to Worker

2. Business Tier Pattern
# Business Delegate
# Service Locator
# Session Facade
# Value Object/TransferObject ---> ValueListHandler --->ValueObjectAssembler
# Compsite Entity

3. Data-tier or Integration Tier Pattern
# Data Access Object
# Service Activator & Invoker



Creational Pattern :
# Factory Pattern-->AbstractFactory Pattern,
# Sigleton P.
# Builder P.
# Protype P. etc.

Behavior Pattern :
# Chain Of Responsibility
# Command Pattern
# Interpretor P. And Iterator P.
# Stragety P. And Template P.
# Observer P, State P. And Visitor P. etc.


Structure Patterns :
# Adpator P, Bridge P. And Proxy P.
# Composite P, Facade P, Flyweight P And Decorator P.





May 18 2011
 
Write a comment...please check this site for your question--http://www.allapplabs.com/java_design_patterns/java_design_patterns.htm

May 18 2011
 
How can we implement observer pattern in JAVA?

April 15 2011
 
The Observer Design Pattern can be used whenever a subject has to be observed by one or more observers.

Let's assume we have a stock system which provides data for several types of client. We want to have a client implemented as a web based application but in near future we need to add clients for mobile devices, Palm or Pocket PC, or to have a system to notify the users with sms alerts. Now it's simple to see what we need from the observer pattern: we need to separate the subject(stocks server) from it's observers(client applications) in such a way that adding new observer will be transparent for the server.

April 15 2011
 
what is Factory Pattern?

April 05 2011
 
Factory Method is a creational pattern. This pattern helps to model an interface for creating an object which at creation time can let its subclasses decide which class to instantiate. We call this a Factory Pattern since it is responsible for "Manufacturing" an Object. It helps instantiate the appropriate Subclass by creating the right Object from a group of related classes. The Factory Pattern promotes loose coupling by eliminating the need to bind application-specific classes into the code.

Ex:
public class MailBuilderFactory {

public static IMailBuilder getMailBuilder(ReportType type) {
switch (type) {
case DAILY:
return DailyMailBuilder.getSingltonInstatne();
case WEEKLY:
return WeeklyMailBuilder.getSingltonInstatne();
default:
break;
}
return null;
}
}

public interface IMailBuilder {

public Message buildMessage();
}

public class DailyMailBuilder implements IMailBuilder{

private static DailyMailBuilder builder;

public static DailyMailBuilder getSingltonInstatne(){
if(builder==null)
builder=new DailyMailBuilder();
return builder;
}

@Override
public Message buildMessage() {
// TODO Auto-generated method stub
return null;
}
}

private static WeeklyMailBuilder builder;

public static WeeklyMailBuilder getSingltonInstatne(){
if(builder==null)
builder=new WeeklyMailBuilder();
return builder;
}

@Override
public Message buildMessage() {
// TODO Auto-generated method stub
return null;
}
}

April 05 2011
 
What is singleton pattern ??

April 05 2011
 
Look @ http://javainterview.co.in/interview/corejava-interview-question.html#h0-54-q-how-to-implement-singleton-pattern

April 05 2011
 
single pattern means if you want to create only one instance in our application so that developer can use singleton design pattern.
like as:-In the struts1.2 ActionServlet class create only one instance
only one instance.

April 05 2011
 
This is one of the most commonly used patterns. There are some instances in the application where we have to use just one instance of a particular class.To achive this Singleton pattern are used.
Lets take a simple example is say Logger, suppose we need to implement the logger and log it to some file according to date time. In this case, we cannot have more than one instances of Logger in the application otherwise the file in which we need to log will be created with every instance.

We use Singleton pattern for this and instantiate the logger when the first request hits or when the server is started.

-----------**** Code for a Singleton Class ****----------------
public class SingletonObjectExample {

private static SingletonObjectExample singletonObject;
// Note that the constructor is private
private SingletonObjectExample() {
// Optional Code
}
public static SingletonObjectExample getSingletonObject() {
if (singletonObject == null)
singletonObject = new SingletonObjectExample ();return singletonObject;
}
}


April 05 2011
 
Ads