Archive

Archive for July, 2011

Interface Vs. Abstract class

Abstract classes
Abstract class is class which contain one or more abstract methods, which is implemented by sub classes. An abstract class can contain no abstract methods but also containe mehtod with body. Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses.Abstract class can contain private as well as protected members.
When to use Abstract Class-If we have a base class where all the classes will perform the same function, then we can define that in our Abstract class. If you plan on updating this base class throughout the life of your program, it is best to allow that base class to be an abstract class. Because you can make a change to it and all of the inheriting classes will now have this new functionality.

Interface
Interface is extremely useful when you don’t want a big hierarchical type framework. As interfaces are implicitly abstract, they cannot be directly instantiated except when instantiated by a class which implements the said interface. The class must implement all of the methods described in the interface, or be an abstract class. An Interface can only have public members. A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class.Maintainability–if you want to add a new feature (method) in its contract, then you must implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass.Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class.

When To use interface – Interfaces are useful when you do not want classes to inherit from unrelated classes just to get the required functionality.It is used where there of chances adding new method in future. Interfaces are more used to set standards. interface gave merely a specification,nothing implemented for any standalone project which can be changed at will its more design flexible and it can be utilized to model multiple inheritance.

Advertisements

Factory Method pattern in java

The factory method pattern is an object-oriented design pattern to implement the concept of factories. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The creation of an object often requires complex processes not appropriate to include within a composing object. The object’s creation may lead to a significant duplication of code, may require information not accessible to the composing object, may not provide a sufficient level of abstraction, or may otherwise not be part of the composing object’s concerns. The factory method design pattern handles these problems by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.

Factory pattern comes into creational design pattern category, the main objective of the creational pattern is to instantiate an object and in Factory Pattern an interface is responsible for creating the object but the sub classes decides which class to instantiate. It is like the interface instantiate the appropriate sub-class depending upon the data passed. Here in this article we will understand how we can create an Factory Pattern in Java

The essence of the Factory method Pattern is to “Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses
Use the factory pattern when:

  1.   The creation of the object precludes reuse without significantly duplicating code.
  2.  The creation of the object requires access to information or resources not appropriate to contain within the composing object.
  3.  The lifetime management of created objects needs to be centralised to ensure consistent behavior.

PrintSomething.java

package javainsider.pattern.factory;

public abstract class PrintSomething {

public abstract void printTech();
}

Now we will have the concrete implementations of the printSomething class, JavaTech and J2eeTech, each providing a much simplified implementation for the printTech() method.

JavaTech.java

package javainsider.pattern.factory;

public class JavaTech extends printSomething {
@Override
public void printTech()
{
System.out.println(“this is java technology”);
}

We will having one more class for j2eeTech for j2ee technology.

J2eeTech.java

package javainsider.pattern.factory;

public class J2eeTech extends PrintSomething {
public void printTech()
{
System.out.println(“this is j2ee technology”);
}
}

Now let us come to the core implementation, the Factory class itself. The PrintFactory class has one static method called showPrint() which the clients can invoke to get the printSomething object. Note the return type of the method, it is neither JavaTech nor J2eeTech, but the super type of the both, i.e, printSomething. Whether the return type of method is JavaTech or J2eeTech is decided based on the input operating system.

PrintFactory.java

package javainsider.pattern.factory;

public class PrintFactory {

public static PrintSomething showPrint(String os){

if (os.equals(“Java”))
{

return new JavaTech();

}
else if (os.equals(“J2ee”))
{

return new J2eeTech();

}

return null

}

}

In this we can makes use of the above PrintFactory class. The client is un-aware of the fact there is multiple implementations of the printSomething class. It accesses the printTech() operation through a single unified type printSomething.

FactoryClient.java

package javainsider.pattern.factory;

public class FactoryClient {

public static void main(String[] args) {

PrintSomething psJava =PrintFactory.showPrint(“Java”);

psJava.printTech();

PrintSomething psJ2ee = PrintFactory.showPrint(“j2ee”);

psJ2ee.printTech();

}

Welcome To Java Technical Blog

Welcome to Java Technical Blog.

This blog is completely for people those who came from java j2ee background :). All about Java Technology, Java related Technology and frameworks.

I will cover the following topic’s as follows:

  1. Java & J2EE Technology Discussion.
  2. Java with RFID Technology.
  3. Java with Near Field Communication(NFC).
  4. Java J2EE frameworks like Spring, Hibernate, Struts, JSF, JBoss Seam, JBoss Richfaces and many more.

Thank you visiting to my Java Technical blog.

With Best Regards,

RUDRA NARAYAN GARNAIK

www.garnaik.com : My Entertainment Blog.

http://java.garnaik.com – My Java FAQ blog.

%d bloggers like this: