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:

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

printSomething.java

package techartifact.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 techartifact.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 techartifact.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 techartifact.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 techartifact.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();

   }
}

Task Flow in Oracle ADF

Task Flow

ADF Task Flows is a new feature that will ship with ADF 11g. In simple terms, it is a tool to encapsulate page flow/navigation. A set of ADF Controller activities, control flow rules and managed beans that interact to allow a user to complete a task. It is much more than that in reality. A set of screens grouped into a Task Flow can be reused from wherever the application requires the same business function. A task flow can be a completely independent component with interaction through input and return parameters. A cool feature is when a task flow is generated as using page fragments (Note: a task flow can have either JSF Pages or JSF Fragments; not both). Such a task flow can be embedded into an existing screen as a region, so you have a main frame that stays static while a sub region contains an entire flowThe main component is the task flow definition file. This is an xml file on the lines of the faces-config.xml and adfc-config.xml. The various pages in a flow are represented by a view component and the navigation between the pages by a control-flow. he return from a task flow is defined as a task-flow-return element. Note that this does not need to know where the task flow was called from. Of course, that’s necessary for a task flow to be reusable. Every task flow to be used in the application is then defined in adfc-config.xml. A task definition file can be dragged into the adfc-config in design mode. Navigations to a task flow are defined as control flows in adfc-config. These control flows can be anonymous so that they can be used from any JSF page in the application.

Bounded Task Flow

Bounded task flow represents modular and reusable application flows with a defined entry (i.e., default activity) and zero to many defined exit points (i.e., task flow return activities). They can be called from other task flows, referenced from a browser URL or embedded as a region in a view. They support reuse, parameters, transaction management and reentry.

A specialized form of ADF Controller task flow that has a single entry point and zero or more exit points. It contains its own set of private, control flowregion. Bounded task flows should be used for every other situation. In most circumstances these bounded rules, activities, and managed beans. A bounded task flow allows reuse, parameters, transaction management, and reentry. When dropped on a page, it becomes a

task flows will be hosted within the core page on the unbounded task flow and, as such, will be made up of JSF fragments rather than whole pages. A called ADF bounded task flow can accept input parameters and can pass return values to the caller upon exit
In addition, bounded task flows have the following features:

Operate within their own private memory scope–page flow scope are loaded lazily at runtime A new instance of TaskFlowContext (can be accessed using EL ${controllerContext.currentViewPort.taskFlowContext}) will be created each time a bounded ADF flow is entered. This context:

Manages the lifespan of all DataControl instances within the bounded task flow holds the information about the task flow ID and whether or not it contains uncommitted data.Don’t support multiple transactions when sharing the data control frame with parent flow To call a bounded task flow directly from a URL, the default activity must be a view activity can be set to be critical (i.e., dictates the framework to create an implicit save point when entering a bounded task flow. Also helps to prevent users from closing the browser window or browser tab if uncommitted data is present).

If protected by ADF Security, authorization is checked first.You can create train-based activities in a bounded task flow and only one train is allowed in each.Bounded task flow should be used if it:

Should be reused in same or other applications

Should run as part of a page within a region container

Requires an isolated transaction context

Changes data and then either commits or rollbacks on exit

Has a requirement for a single entry point

Unbounded Task Flow

A Fusion web application always contains an ADF unbounded task flow, which contains the entry point or points to the application. Its XML configuration file (i.e., adfc-config.xml) is automatically created when building a new application using the Fusion Web Application (ADF) application template or when adding the ADF Page Flow Technology Scope to an existing or new web project. There will always exist a single instance of unbounded task flow at runtime, even if there is no activity added to it.

A set of activities, ADF control flow rules, and managed beans that interact to allow a user to complete a task. An unbounded task flow has a single point of entry. unbounded task flow should only be used to

define entry points to your application, for example the home page, pages that will be deep linked from emails and so on.

A unbounded task flow has the following features:

You cannot declaratively specify input parameters for it.It cannot contain a default activity (i.e., an activity designated as the first to run in the task flow). This is because an unbounded task flow does not have a single point of entry.It can be configured by one or many configuration files that are parsed and loaded the first time the user starts the application.View activities of an unbounded task flow can be configured bookmarkable managed beans that are in session or application scope should be configured in the unbounded task flow definition.You cannot create a train from activities in an unbounded task flow.You cannot use a task flow template as the basis for creating a new unbounded task flow

Java 5 UUID

It is very common to use unique identifiers in web application for identifying unique users or some other usage, till now I was always
writing an program using some random generator code for them. It is always hard to do that, as those unique identifiers should not
repeat them self in future, else it will lead in data integrity issue later on.

Recently i got to knw about UUID class out of box from Java UUID
UUID static class provide 3 static method to genretae UUID’s. UUID generate are from UUID are actually universally unique identifiers.

1) UUID.randomUUID()

randomUUID() will result some thing like following,

Result : 8284e82d-a2ff-4120-a684-6d7166991b05

2) UUID.fromString(“18-23-58-96-96”)

This method requires an input containing long number separated by ‘-‘. It mean first secton would most significant bit and section 4 would be least signt bits that would be used
for creating UUID.

Result : 00000018-0023-0058-0096-000000000096

import java.util.UUID;

public class UUIDExample {

  public static final void main(String... aArgs){
    //generate example random UUIDs
    System.out.println("Example UUID One: " + UUID.randomUUID());
    System.out.println("Example UUID Two: " + UUID.fromString("18-23-58-96-96"));
  }
}

Example run :
>java -cp . UUIDExample
Example UUID One: 067e6162-3b6f-4ae2-a171-2470b63dff00
Example UUID Two: 54947df8-0e9e-4471-a2f9-9af509fb5889