MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Defining Factory Patterns and its usage in Java

In this article we will have a clear understanding of factory patterns and its usage in Java.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Introduction

Before starting describing factory pattern, we should have a clear understanding of design patterns and its usage. Design pattern can be described as a tested design mechanism which can be used to solve common day to day programming issues. These patterns are widely tested in different applications and proved to be efficient enough to use. The advantages of using design patterns can be as follows

  • The developer does not need to invent the solution for common programming issues.
  • The developer only need to identify which pattern should be suitable for specific problem.
  • As the pattern is tested, developer does not need to test it again.
  • Development time will be reduced significantly.

There are many design patterns in core java and j2ee world. Creational pattern is one of the most important and widely used design patterns in application programming. The name itself describes the purpose of this particular pattern. The name ‘Creational’ suggests that it is used to create objects in java applications. So most of the time this pattern is used when we need to create objects that will be used in application.

Factory pattern is one of the most important design patterns in core java. It is a part of creational design pattern. The name ‘factory’ also suggested that it is a factory of objects. So the factory pattern is used to create objects for application requirement. The creational patterns abstract the object instantiation and creation process by hiding how the objects are created when a requirement arises. It also makes the system independent of the object creation process. So the developer only needs to send request to the factory for new objects and the factory will take care of it. After creating the required object, factory sends it back to the requesting application. Now the application will get the ready made object to use. So the flexibility of the application increase many fold and development time reduces significantly.

An Abstract factory pattern is one level of abstraction higher than a factory method pattern, which means it returns the factory classes. Again the name ‘Abstract’ suggests that it provides some type of abstraction over the creation of objects. Abstract factory patter returns factory class not the objects and that is the difference between factory and abstract factory pattern. So when we have lot of application modules and need to use factory pattern, we use abstract factory. It returns factory classes and the again from factory class we get the required object.

See also: http://mrbool.com/abstract-factory-course-c-4-0-lesson-16/18683

The following diagram shows the simple hierarchy of factory pattern

Factory pattern

Figure 1: Factory pattern

The above diagram describes a simple hierarchy of factory pattern .Factory pattern returns one of the several product subclasses based on some input passed to the parent class. You should use a factory pattern if you have a super class and a number of subclasses, and you have a requirement to get any sub class at any point of time. The advantage is, you as a developer only needs to send some requirement (here input parameter values) to the factory (here the parent class) and the factory will return the required object to the calling class.

Let’s look at a sample code:

Listing 1: Class showing the implementation of factory design pattern

public interface ShapeConst {
// Defining shape constants
public static final int TRIANGLE_SHAPE =1;
public static final int OVAL_SHAPE =2;
}
public class ShapeFactoryProd {
public abstract Shape getProdShape(int shapeId);
}
public class OriginalShapeFactory extends
ShapeFactoryProd throws ShapeException {
public Shape getProdShape(int shapeTypeId){
Shape shape = null;
if(shapeTypeId == ShapeConst.TRIANGLE_SHAPE) {
//Draw a triangular shape
shape = new Triangle();
}
else if(shapeTypeId == ShapeConst. OVAL_SHAPE) {
//Draw an oval shape
shape = new Oval();
}
// Return a shape value to the calling class
return shape;
}
}

Now let’s look at the calling code, which uses the factory:

ShapeFactoryProd factory = new OriginalShapeFactory();
//Returns a Shape but whether it is a Triangle or a
//Oval is not known to the caller.
Shape s = factory.getShape(1);
s.draw(); // Triangle is drawn
//Again returns a Shape but whether it is a Triangle or a
//oval is not known to the caller.
s = factory.getShape(2);
s.draw(); //Oval is drawn

The important point to note is, factory pattern returns an instance of several object instances, but the calling method is unaware of the actual implementation class. The calling method invokes the method on the parent interface and the internal structure of the factory framework returns the correct object. The factory design pattern reduces the coupling or the bonding between the calling method and called objects. This decoupling is a very powerful and common feature in many frameworks as it introduces flexibility in the application architecture. In future, to conserve memory and increase performance you can cache the objects or reuse objects in your factory with no changes to the calling methods. You can also load objects in your factory based on parameter values read from an external properties file or xml file. Another benefit of factory pattern is that the developer does not need to call constructors directly; instead they can use more meaningful method.

Another important aspect to consider is the use of singleton design pattern into your factory pattern code. It does not make sense to create a new factory object for each invocation from the calling class. The singleton design pattern will create only a single instance of your factory.

So the advantages of factory pattern are as follows:

  • Factory design pattern implements loose coupling between calling and target class. So the code is application is flexible.
  • Factory pattern enables the subclasses to provide further extended version of an object. So in future if some other type of object is required then the same factory can be used.
  • Debugging is easier while factory pattern is implemented because the developer knows the execution path.
  • Factory pattern implements consistency in code because the application has to follow the factory to get new objects instead of calling the constructor directly.
  • Factory pattern hides the complexity of object creation. So the developer can easily create objects without knowing the internal mechanism.

Conclusion

So that is all about factory design pattern in java. It is one of the most widely used design patterns in various Java libraries and frameworks. So it is always recommended to used factory pattern whenever you have a requirement of many sub classes and there is a parent class. But remember, if there is no requirement of factory pattern then please do not use it. Otherwise it will unnecessary create complexity in your application code. The success of design pattern implementation only depends upon its appropriate use in correct situations.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
Know how to keep MrBool Online
SUPPORT US
SUPPORT US
With your help, we can keep providing free content and helping you to be a better professional
support us
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login