Prepare Interview

Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address
Check our LIVE MOCK INTERVIEWS

Java Design Patterns Interview Questions and Answers

Intermediate / 1 to 5 years experienced level questions & answers

Ques 1. What is Prototype Design Pattern in java design patterns?

Prototype pattern refers to creating duplicate object while keeping performance in mind. This type of design pattern comes under creational pattern as this pattern provides one of the best way to create an object.

This pattern involves implementing a prototype interface which tells to create a clone of the current object. This pattern is used when creation of object directly is costly. For example, a object is to be created after a costly database operation. We can cache the object, returns its clone on next request and update the database as as and when needed thus reducing database calls.

We're going to create an abstract class Mobile and concrete classes extending the Mobile class. A class MobileCache is defined as a next step which stores shape objects in a Hashtable and returns their clone when requested.

PrototypPatternDemo, our demo class will use MobileCache class to get a Mobile object.

Step 1
Create an abstract class implementing Clonable interface.
Mobile.java
public abstract class Mobile implements Cloneable {
   
   private String id;
   protected String type;
   
   abstract void make();
   
   public String getType(){
      return type;
   }
   
   public String getId() {
      return id;
   }
   
   public void setId(String id) {
      this.id = id;
   }
   
   public Object clone() {
      Object clone = null;
      try {
         clone = super.clone();
      } catch (CloneNotSupportedException e) {
         e.printStackTrace();
      }
      return clone;
   }
}

Step 2
Create concrete classes extending the above class.
Sony.java
public class Sony extends Mobile {

   public Sony(){
     type = "Sony";
   }

   @Override
   public void make() {
      System.out.println("Inside Sony::make() method.");
   }
}

Blackberry.java
public class Blackberry extends Mobile {

   public Blackberry(){
     type = "Blackberry";
   }

   @Override
   public void make() {
      System.out.println("Inside Blackberry::make() method.");
   }
}

Samsung.java
public class Samsung extends Mobile {

   public Samsung(){
     type = "Samsung";
   }

   @Override
   public void make() {
      System.out.println("Inside Samsung::make() method.");
   }
}

Step 3
Create a class to get concreate classes from database and store them in a Hashtable.
MobileCache.java
import java.util.Hashtable;
public class MobileCache {
   private static Hashtable<String, Mobile> shapeMap 
      = new Hashtable<String, Mobile>();

   public static Mobile getMobile(String shapeId) {
      Mobile cachedMobile = shapeMap.get(shapeId);
      return (Mobile) cachedMobile.clone();
   }

   // for each shape run database query and create shape
   // shapeMap.put(shapeKey, shape);
   // for example, we are adding three shapes
   public static void loadCache() {
      Samsung circle = new Samsung();
      circle.setId("1");
      shapeMap.put(circle.getId(),circle);

      Blackberry square = new Blackberry();
      square.setId("2");
      shapeMap.put(square.getId(),square);

      Sony rectangle = new Sony();
      rectangle.setId("3");
      shapeMap.put(rectangle.getId(),rectangle);
   }
}

Step 4
PrototypePatternDemo uses MobileCache class to get clones of shapes stored in a Hashtable.
PrototypePatternDemo.java
public class PrototypePatternDemo {
   public static void main(String[] args) {
      MobileCache.loadCache();

      Mobile clonedMobile = (Mobile) MobileCache.getMobile("1");
      System.out.println("Mobile : " + clonedMobile.getType());

      Mobile clonedMobile2 = (Mobile) MobileCache.getMobile("2");
      System.out.println("Mobile : " + clonedMobile2.getType());

      Mobile clonedMobile3 = (Mobile) MobileCache.getMobile("3");
      System.out.println("Mobile : " + clonedMobile3.getType());
   }
}

Step 5
Verify the output.
Mobile : Samsung
Mobile : Blackberry
Mobile : Sony

Is it helpful? Add Comment View Comments
 

Ques 2. What is Decorator Design Pattern in java design patterns?

Decorator pattern allows to add new functionality an existing object without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.

This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.

We are demonstrating use of Decorator pattern via following example in which we'll decorate a shape with some color without alter shape class.

We're going to create a Mobile interface and concrete classes implementing the Mobile interface. We then create a abstract decorator class MobileDecorator implementing the Mobile interface and having Mobile object as its instance variable.

RedMobileDecorator is concrete class implementing MobileDecorator.
DecoratorPatternDemo, our demo class will use RedMobileDecorator to decorate Mobile objects.

Step 1
Create an interface.
Mobile.java
public interface Mobile {
   void make();
}

Step 2
Create concrete classes implementing the same interface.
Sony.java
public class Sony implements Mobile {

   @Override
   public void make() {
      System.out.println("Mobile: Sony");
   }
}

Blackberry.java
public class Blackberry implements Mobile {

   @Override
   public void make() {
      System.out.println("Mobile: Blackberry");
   }
}

Step 3
Create abstract decorator class implementing the Mobile interface.
MobileDecorator.java
public abstract class MobileDecorator implements Mobile {
   protected Mobile decoratedMobile;

   public MobileDecorator(Mobile decoratedMobile){
      this.decoratedMobile = decoratedMobile;
   }

   public void make(){
      decoratedMobile.make();
   }
}

Step 4
Create concrete decorator class extending the MobileDecorator class.
RedMobileDecorator.java
public class RedMobileDecorator extends MobileDecorator {

   public RedMobileDecorator(Mobile decoratedMobile) {
      super(decoratedMobile);
   }

   @Override
   public void make() {
      decoratedMobile.make();       
      setRedBorder(decoratedMobile);
   }

   private void setRedBorder(Mobile decoratedMobile){
      System.out.println("Border Color: Red");
   }
}

Step 5
Use the RedMobileDecorator to decorate Mobile objects.
DecoratorPatternDemo.java
public class DecoratorPatternDemo {
   public static void main(String[] args) {

      Mobile circle = new Blackberry();

      Mobile redBlackberry = new RedMobileDecorator(new Blackberry());

      Mobile redSony = new RedMobileDecorator(new Sony());
      System.out.println("Blackberry with normal border");
      circle.make();

      System.out.println("nBlackberry of red border");
      redBlackberry.make();

      System.out.println("nSony of red border");
      redSony.make();
   }
}

Step 6
Verify the output.

Blackberry with normal border
Mobile: Blackberry

Blackberry of red border
Mobile: Blackberry
Border Color: Red

Sony of red border
Mobile: Sony
Border Color: Red

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

Related interview subjects

JUnit interview questions and answers - Total 24 questions
Spring Framework interview questions and answers - Total 53 questions
Java Design Patterns interview questions and answers - Total 15 questions
Java 17 interview questions and answers - Total 20 questions
Core Java interview questions and answers - Total 306 questions
Tomcat interview questions and answers - Total 16 questions
Apache Wicket interview questions and answers - Total 26 questions
Java Applet interview questions and answers - Total 29 questions
JAXB interview questions and answers - Total 18 questions
JMS interview questions and answers - Total 64 questions
Log4j interview questions and answers - Total 35 questions
Struts interview questions and answers - Total 84 questions
RMI interview questions and answers - Total 31 questions
Apache Camel interview questions and answers - Total 20 questions
JDBC interview questions and answers - Total 27 questions
Java 11 interview questions and answers - Total 24 questions
JPA interview questions and answers - Total 41 questions
EJB interview questions and answers - Total 80 questions
GWT interview questions and answers - Total 27 questions
Kotlin interview questions and answers - Total 30 questions
Glassfish interview questions and answers - Total 8 questions
Google Gson interview questions and answers - Total 8 questions
Java 21 interview questions and answers - Total 21 questions
JSP interview questions and answers - Total 49 questions
J2EE interview questions and answers - Total 25 questions
Apache Tapestry interview questions and answers - Total 9 questions
Java Swing interview questions and answers - Total 27 questions
Java Mail interview questions and answers - Total 27 questions
Hibernate interview questions and answers - Total 52 questions
JSF interview questions and answers - Total 24 questions
Java 8 interview questions and answers - Total 30 questions
Java 15 interview questions and answers - Total 16 questions
JBoss interview questions and answers - Total 14 questions
Web Services interview questions and answers - Total 10 questions
RichFaces interview questions and answers - Total 26 questions
Servlets interview questions and answers - Total 34 questions
Java Beans interview questions and answers - Total 57 questions
Spring Boot interview questions and answers - Total 50 questions
©2023 WithoutBook