What is difference between decorator and adapter, template and strategy, factory and abstract factory?

Decorator and Adapter.

Decorator Adaptar
Used to deal with new behavior or responsibilities without affecting existing code Mainly involved in converting interfaces as per the requirement of client
Provides a wrapper over objects to apply same method which yields different results based on whether the object invoked is granular or composite Allows use of new libraries without changing existing methods
Change of interface does not happen Change of interface happens in this case


Template and Strategy.

Template Strategy
Defines outline for algorithms and allows sub class to override some steps Uses object composition to provide variety of algorithm implementation
Efficient in terms of less redundant code Flexible in terms of allowing users to change algorithm at run time
Depends on super class for methods All the algorithms can be itself implemented


Factory and Abstract Factory.

Factory Abstract factory
Specifies creation method which will be  used by sub class for object creation Used to create group of related objects while hiding actual classes
Deals with single product Deals with number of products
Each concrete factory implements abstract factory in case more than one products needed to be supported Makes uses of factory pattern provided by each family of product

 

See the following JAVA / J2EE/ Core Java video on get started with Apache CXF.


Also visit our site from more such Java / J2EE/ Core Java interview question with answers videos.

Java J2ee interview questions and answers: – From the below java code which constructor will fire first?

You can see our Java J2ee interview question videos from http://www.questpond.com/java/javaj2eedesignpattern.html

Ok, the first thing, let’s try to understand the above java interview question.  Let’s say you have two classes one is parent class and the other is a child class which extends (inherits) from the parent class as shown in the below code. Now if you create the object of “Child” class which constructor will fire first.

 class Parent

{

public Parent()

{

System.out.println(“This is parent”);

}

}

class Child extends Parent

{

public Child()

{

System.out.println(“This is Child”);

}

}

The answer is first the Parent class constructor will get executed and then the child class constructor will fire.

One more  Java j2ee interview question which was recently asked  to one of my friend was :-  Does Stringbuffer always give better performance as compared to string.

See the following video on By using String or String Buffer performance increases:-

Java Training: -How will you explain Bootstrap, Extension and System Class loader?

There three types of class loaders:-

  • Bootstrap Class loader also called as primordial class loader.
  • Extension Class loader.
  • System Class loader.

Let’s now try to get the fundamentals of these class loaders.

Bootstrap Class loader

Bootstrap class loader loads those classes those which are essential for JVM to function properly. Bootstrap class loader is responsible for loading all core java classes for instance java.lang.*, java.io.* etc. Bootstrap class loader finds these necessary classes from “jdk/jre/lib/rt.jar”. Bootstrap class loader cannot be instantiated from JAVA code and is implemented natively inside JVM.

Extension Class loader

The extension class loader also termed as the standard extensions class loader is a child of the bootstrap class loader. Its primary responsibility is to load classes from the extension directories, normally located the “jre/lib/ext” directory. This provides the ability to simply drop in new extensions, such as various security extensions, without requiring modification to the user’s class path.

System Class loader

The system class loader also termed application class loader is the class loader responsible for loading code from the path specified by the CLASSPATH environment variable. It is also used to load an application’s entry point class that is the “static void main ()” method in a class.

See the following video on overview and working of Servlets in Java: –


Click to get Java Training

Regards,

Get more Java Training from author’s blog

Java Training: – Explain the concept of local interfaces?

One of the biggest issues of creating objects using home interface is performance. Below are the steps which follow when you call the EJB object:-

  • JAVA client calls the local stub.
  • Stub marshal the values in to some other form which the network understands and sends it to the skeleton.
  • Skeleton then de-marshals it back to a form which is suitable for JAVA.
  • Skeleton then calls the EJB object and methods.
  • EJB object then does object creation, connection pooling, transaction etc.
  • Once EJB object calls the bean and the bean completes its functionalities. All the above steps must again be repeated to reach back to the JAVA client.

So you can easily guess from the above step that its lot of work. But this has been improved in EJB 2.0 using Local objects. Local objects implement local interface rather than using remote interface. Just to have a comparison below are the steps how the local object works.

  • JAVA client calls the local object.
  • Local object does connection pooling, transactions and security.
  • It then passes calls the bean and when bean completes its work it returns the data to the Local object who then passes the same to the end client.

See the following video on Introduction to EJB in Java: –

Click to get Java Training

Regards,

Get more Java Training from author’s blog

Java Training: – Elaborate different types of resultset?

“ResultSet” is an object that contains the results of SQL query. That means it contains the rows that satisfy the conditions of the query.

There are three basic types of resultsets:-

Forward-only results set

These types of resultset are non-scrollable and moves only forward.

Scroll-insensitive set

Resultset is scrollable so the cursor can move forward, backward, to a particular row etc.

While the resultset is open it does not show any changes done to the underlying database.

Scroll-sensitive set

Resultset is scrollable so the cursor can move forward, backward, to a particular row etc.

Resultset is sensitive to changes when it is open or used. That means if any values are modified in the database it’s propagated to the resultset.

See the following video on Factory Pattern in Java: –


Click to get Java Training

Regards,

Get more Java Training from author’s blog

Java Training: – Show “ResultSet”, “RowSet”, “CachedRowset”, “JdbcRowset” and “WebRowSet” relation ship?

Below are the major points of difference:-

  • “ResultSet” is a connected architecture while “RowSet” is a disconnected architecture.
  • As “ResultSet” is a connected architecture we cannot serialize the object while “RowSet” can be serialized.
  • “RowSet” object is a Java bean while “ResultSet” is not. In the below diagram you can see “RowSet” interface also derives from “BaseRowSet” interface. “BaseRowSet” interface has all the ingredients to make it a Java bean.

Below diagram shows the complete relationship between all the interface and classes.

java

Figure: – Interface diagram for “Resultset” and “RowSet”

As “RowSet” is a disconnected from database it need to maintain Meta data for columns. “RowSet” can also provide scrollable resultsets or updatable resultsets even if the JDBC driver is not supporting the same. Java client can get a “RowSet” manipulate the same and finally send all the results to the database at one go. Sun has provided three implementation of “RowSet” as below:-

CachedRowSet: – Disconnected rowset always keeps the data in memory. It is scrollable and can also be serialized. It’s an ideal choice where we want to pass data between tiers or to do batch updates. “CachedRowSet” are disconnected so can be used for huge number of updates. Get the “CachedRowSet” object manipulate all your data and send the whole bunch of manipulated data in on go to the Database.

JDBCRowSet: – It’s opposite to “CachedRowSet”. It maintains a connection to the database while the client has reference to it. So it’s a connected architecture as compared to CachedRowSet.

WebRowSet :- “WebRowSet” is a extension of “CachedRowSet”. But the added feature is it can produce XML presentation of the data cached. If you are thinking of exposing your data through web services or to provide data to thin client which are written in different language other than JAVA. Best bet if you want to pass data in XML format over HTTP protocol.

See the following video on FlyWeight Pattern in Java: –


Click to get Java Training

Regards,

Get more Java Training from author’s blog

Java Training: – Differences between JNDI context, Initial context, session context and EJB context?

JNDI context

JNDI Context Provides a mechanism to lookup resources on the network

Initial context

Initial Context constructor provides the initial context.

Session context

Session Context has all the information a session bean would require from the container

Entity Context

Entity Context has all the information that an Entity bean would need from a container

EJB context

EJB Context contains the information that is common to both the session and entity bean

See the following video on EJB (Enterprise Java Beans): –


Click to get Java Training

Regards,

Get more Java Training from author’s blog

Java Training: – How will you explain stateful and stateless session bean?

Stateful Session bean:-

  • Stateful session bean maintain the state of the conversation between the client and itself
  • When the client invokes a method on the bean the instance variables of the bean may contain a state but only for the duration of the invocation
  • implements the javax.ejb.SessionBean interface and is deployed with the declarative attribute “stateful”

Stateless session bean:-

  • A stateless session bean is an enterprise bean that provides a stateless service to the client.
  • Conceptually, the business methods on a stateless session bean are similar to procedural applications or static methods; there is no instance state, so all the data needed to execute the method is provided by the method arguments
  • implements the javax.ejb.SessionBean interface and is deployed with the declarative attribute “stateless

See the following video on Factory Pattern in Hibernate: –


Click to get Java Training

Regards,

Get more Java Training from author’s blog

Java Training: – Elaborate ORM and different levels of ORM quality?

ORM: –

ORM stands for Object/Relational mapping which is mainly used to remove the difference between object oriented and relation model

Different levels of ORM quality: –

  • Pure relational entire application, including the user interface, is designed around the relational model and SQL-based relational operations
  • Light object mapping The entities are represented as classes that are mapped manually to the relational tables
  • Medium object mapping The application is designed around an object model. The SQL code is generated at build time. And the associations between objects are supported by the persistence mechanism, and queries are specified using an object-oriented expression language
  • Full object mapping supports sophisticated object modeling: composition, inheritance, polymorphism and persistence

See the following video on Batch Processing in Hibernate: –

Click to get Java Training

Regards,

Get more Java Training from author’s blog