Wednesday 30 September 2015

Java Memory Management

Memory Management Part-II


Question : - What a Garbage Collector will do?

Answer:- Let's talk about Garbage Collector jobs
                Things to Consider
  • Stop the application events :- Garbage Collector pauses the entire application and at that            point it collects garbage.
  • Memory Fragmentation :- Memory fragmentation is when most of your memory is allocated in a large number of non-contiguous blocks and leaving a good percentage of our total memory unallocated, but that is unusable for most typical scenarios. This results in out of memory exceptions,When garbage collector runs does it defrag the memory fragment            to once or leave it to the latest state. 
  • ThroughPut :– how quickly can it run and how quickly can it collect?
  • Multi Core :- Now we have multiple processors or multiple threads Pauses are the times when an application appears unresponsive because garbage collection is occurring.
  • Promptness is the time between when an object becomes dead and memory becomes available, which is very important for distributed systems, including RMI.

 Question:- What is the way of Direct memory access in java?

Answer:- Java HotSpot VM contains a “backdoor” that provides a number of low-level operations to control threads and memory directly. This backdoor class sun.misc.Unsafe which is widely used by JDK itself in packages like java.nio or java.util.concurrent. This class provides an easy way to look into HotSpot JVM internals and this class can also be used for profiling and development tools.

Division of  java memory pool


The heap memory is the runtime data area from which the Java VM allocates memory for all class instances and arrays. The heap may be of a fixed or variable size. JVM Heap memory is physically divided into two parts –Young Generation and Old Generation.

  • Eden Space: The pool from which memory is initially allocated for most objects. Most initial objects allocated in Eden space.
  •  Survivor Space: The pool containing objects that have survived the garbage collection of the Eden space.
  •  Tenured Generation: The pool containing objects that have existed for some time in the survivor space.
  •  Non-heap memory: Non-heap memory includes a method area shared among all threads and memory required for the internal processing or optimization for the Java VM. It stores per-class structures such as a runtime constant pool, field and method data, and the code for methods and constructors. The method area is logically part of the heap but, depending on the implementation, a Java VM may not garbage collect or compact it. Like the heap memory, the method area may be of a fixed or variable size.
  •  Permanent Generation: The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.
  • Code Cache: The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code.



For demonstartion how Garbage Collection works in Java

GarbageCollectionExample.java

package com.gaurav.memorymanagement;

import java.lang.reflect.Field;

import sun.misc.Unsafe;

public class GarbageCollectionExample {
private static Unsafe unsafe;
static {
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (Unsafe) field.get(null);
} catch (Exception e) {
e.printStackTrace();
}
}

public static long addressOf(Object o) throws Exception {
Object array[] = new Object[] { o };
long baseOffset = unsafe.arrayBaseOffset(Object[].class);
int addressSize = unsafe.addressSize();
long objectAddress;
switch (addressSize) {
case 4:
objectAddress = unsafe.getInt(array, baseOffset);
break;
case 8:
objectAddress = unsafe.getLong(array, baseOffset);
break;
default:
throw new Error("unsupported address size: " + addressSize);
}
return (objectAddress);
}

public static void main(String args[]){
try{
for(int i=0; i< 40000; i++){
Object mine = new GCObjects();
long address = addressOf(mine);
System.out.println(address);
}
}catch(Exception e){
e.printStackTrace();
}
}
}

class GCObjects {
long data;
long a;
long aa;
long aaa;
long aaaa;
long aaaaa;
long aaaaaa;
long aaaaaaa;
long aaaaaaaa;
long aaaaaaaaa;
long aaaaaaaaaa;
long aaaaaaaaaaa;
long aaaaaaaaaaaa;
long aaaaaaaaaaaaa;
long aaaaaaaaaaaaaa;
long aaaaaaaaaaaaaaa;
long aaaaaaaaaaaaaaaa;
long aaaaaaaaaaaaaaaaa;

}


Execution process - For demonstartion how Garbage Collection works in Java

            Java -cp . com.gaurav.memorymanagement.GarbageCollectionExample > GarbageCollectionExampleOutput.csv

This command will create a CSV file, which we can open with Microsoft Excel and represent those data using line chart. The representation will look like follows :




Thursday 27 August 2015

Java Memory Management

Memory Management - PART - 1


Memory Management in java is responsibility of garbage collector. Garbage Collection is not the only form of Memory Management in Java. Real-time Specification for Java (RTSJ) is also being used for Memory Management. These efforts were mainly dedicated to real-time and embedded programming in Java for which GC was not suitable - due to performance overhead.

Understanding JVM Memory Model is very important if we want to know the working process of Java Garbage Collection.

Types of Garbage Collector : -

  • Do nothing : - It might just decide never to run and never to do anything, no memory gets free but it do stills gurantee to not collecting live objects.

  • Reference Counting garbage collector : - COM programming environment is the best example of Reference Counting Garbage collector. COM application may call 2 functions. First is Add Ref and second is Release. Add Ref increments the count of the object and Release decrease the count. When count goes to zero then ref is no longer been used or we can say that A reference count is maintained for each object on the heap. When an object is first created and a reference to it is assigned to a variable, the object's reference count is set to one. When any other variable is assigned a reference to that object, the object's count is incremented. When a reference to an object goes out of scope or is assigned a new value, the object's count is decremented. Any object with a reference count of zero can be garbage collected. When an object is garbage collected, any objects that it refers to have their reference counts decremented. In this way the garbage collection of one object may lead to the subsequent garbage collection of other objects.

  • Mark and Sweep : - To determine which objects are no longer in use, the JVM intermittently runs mark-and-sweep algorithm. Garbage collector runs in 2 phases, In mark phase it is marking that memory is still alive or this algorithm traverses all object references, starting with the GC roots, and marks every object found as alive and in sweep phase all of the heap memory that is not occupied by marked objects is reclaimed. It is simply marked as free, essentially swept free of unused objects.

  • Copying - Copying garbage collectors move all live objects to a new area and the old area is known to be all free space. This is not following any separate Mark and Sweep phases Objects are copied(these objects are discovered by the traversal from the root nodes) to the new area on the fly and forwarding ponters are left in their old locations and these pointers allows the garbage collector to detect references to objects that have already been moved. The garbage collector can then assign the value of the forwarding pointer to the references so they point to the object's new location.

  • Generational - Copying collectors spend much of their time for copying the same long-lived objects again and again. In order to address this inefficiency Generational collectors work with grouping objects by age and garbage collecting younger objects more often than older objects. This approach works by dividing the heap into two or more sub-heaps, each of which serves one "generation" of objects. The youngest generation is garbage collected most often. As most objects are short-lived, only a small percentage of young objects are likely to survive their first collection. Once an object has survived a few garbage collections as a member of the youngest generation, the object is promoted to the next generation: it is moved to another sub-heap.

  • Incremental - Rather than attempting to find and discard all unreachable objects at each invocation an incremental garbage collector just attempts to find and discard a portion of the unreachable objects. Because only a portion of the heap is garbage collected at each invocation, each invocation should in theory run in less time. A garbage collector that can perform incremental collections, each of which is guaranteed to require less than a certain maximum amount of time, can help make a Java virtual machine suitable for real-time environments.  


Reference taken from other sources

Tuesday 4 August 2015

How to Compare two Arraylists which contain objects of the same class?

Compare two Arraylists which contain objects of the same class

I have two lists of different sizes which belong to the same class Student which is having attributes like:

  • Integer id;
  • String name;
  • Integer age;


We will compare these two lists and remove the elements which are common
in both of them. Below is the example available to compare two lists:-

Student.java

public class Student {

/**
  * @author Gaurav
  *
  */
private Integer id;
private String name;
private Integer age;


public Student(Integer id, String name, Integer age){
    this.id = id;
    this.name = name;
    this.age = age;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}


public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

@Override
public String toString() {
    return "Id : " + this.id +" Name : "+this.name+ " age : "+this.age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
Student student = (Student) obj;
return id == student.id
&& (name == student.name || (name != null && name
.equals(student.getName())))
&& (age == student.age || (age != null && age
.equals(student.getAge())));
}
}

CompareLists.java

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

/**
 * @author Gaurav
 *
 */
public class CompareLists {
public static void main( String[] args )
{
  List<Student> l1 = new ArrayList<Student>();
  l1.add(new Student(1,"Kumar",33));
  l1.add(new Student(2,"Hiten",35));
  l1.add(new Student(3,"Varun",46));
  l1.add(new Student(4,"Gaurav",35));
  l1.add(new Student(5,"Vishal",40));


  List<Student> l2 = new ArrayList<Student>();
  l2.add(new Student(4,"Gaurav",35));
  l2.add(new Student(5,"Vishal",40));
  
 
  //This is the first way to compare two list containing objects of same classes and removing the common item.
  
  List<Student> firstList = new ArrayList<Student>(l1);
  System.out.println("First list before removing common elements");
  System.out.println(firstList);
  firstList.removeAll(l2);
  System.out.println("\nFirst list after removing common elements");
  System.out.println(firstList);
  
 //This is the Second way to compare two list containing objects of same classes  and removing the common item.
  List<Student> secondList = new ArrayList<Student>();
  System.out.println("\nSecond list before removing common elements");
  System.out.println(l1);
  for (int i = 0; i < l1.size(); i++){

       if (!l2.contains(l1.get(i))){

           secondList.add(l1.get(i));
       }
   }
  System.out.println("\nSecond list after removing common elements");
  System.out.println(secondList);
  
  //This is the Third way to compare two list containing objects of same classes  and removing the common item.
  List<Student> thirdList = new ArrayList<Student>(l1);
  System.out.println("\nThird list before removing common elements");
  System.out.println(thirdList);
  for(Student apv : l1){
for(Student pv : l2){
if(apv.getId() == pv.getId() && apv.getName().equalsIgnoreCase(pv.getName())){
thirdList.remove(pv);
}
}
}
  System.out.println("\nThird list after removing common elements");
  System.out.println(thirdList);
   
  
  //This is the Fourth way to compare two list containing objects of same classes  and removing the common item.
 
 /**org.apache.commons.collections.CollectionUtils - with the help of this class below call is possible and for this explicitly, we need to add the jar named commons-collections-3.2.1.jar*/
  
  System.out.println("\nFourth list after removing common elements");
  @SuppressWarnings("unchecked")
  Collection<Student> fourthList= CollectionUtils.subtract(l1, l2);
  System.out.println(fourthList);
 
}
}

Result:-

First list before removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46, Id : 4 Name : Gaurav age : 35, Id : 5 Name : Vishal age : 40]

First list after removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46]

Second list before removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46, Id : 4 Name : Gaurav age : 35, Id : 5 Name : Vishal age : 40]

Second list after removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46]

Third list before removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46, Id : 4 Name : Gaurav age : 35, Id : 5 Name : Vishal age : 40]

Third list after removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46]

Fourth list after removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46]

Comparing and Sorting list objects based on their multiple attributes

Comparing and Sorting list objects 


There are multiple ways to sort objects available in list collection based on their multiple attributes which we can treat as keys. We will see the example below where we will sort list of objects. These objects are available as pojo(plain old java objects) forms.

Employee.java

package com.gaurav.compare.lists;
/**
 * @author Gaurav
 *
 */
public class Employee {
    private String empName;
    private String designation;
    private int age;
    private int salary;

    public Employee(String name, String jobTitle, int age, int salary) {
        this.empName = name;
        this.designation = jobTitle;
        this.age = age;
        this.salary = salary;
    }

    public String toString() {
        return String.format("%s\t%s\t%d\t%d", empName, designation, age, salary); 
    }

public String getEmpName() {
return empName;
}

public void setEmpName(String empName) {
this.empName = empName;
}

public String getDesignation() {
return designation;
}

public void setDesignation(String designation) {
this.designation = designation;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}
}


ComparatorChain.java

package com.gaurav.compare.lists;
/**
 * @author Gaurav
 *
 */
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class ComparatorChain implements Comparator<Employee>{
private List<Comparator<Employee>> lstComparators;
 
    @SafeVarargs
public ComparatorChain(Comparator<Employee>... comparators) {
        this.lstComparators = Arrays.asList(comparators);
    }

    @Override
    public int compare(Employee employee1, Employee employee2) {
        for (Comparator<Employee> comparator : lstComparators) {
            int finalResult = comparator.compare(employee1, employee2);
            if (finalResult != 0) {
                return finalResult;
            }
        }
        return 0;
    }
}

DesignationComparator.java

package com.gaurav.compare.lists;
/**
 * @author Gaurav
 *
 */
import java.util.Comparator;

public class DesignationComparator implements Comparator<Employee>{
 @Override
   public int compare(Employee employee1, Employee employee2) {
       return employee1.getDesignation().compareTo(employee2.getDesignation());
   }
}

AgeComparator.java

package com.gaurav.compare.lists;
/**
 * @author Gaurav
 *
 */
import java.util.Comparator;

public class AgeComparator implements Comparator<Employee>{
 @Override
   public int compare(Employee employee1, Employee employee2) {
       return employee1.getAge() - employee2.getAge();
   }
}

SalaryComparator.java

package com.gaurav.compare.lists;
/**
 * @author Gaurav
 *
 */
import java.util.Comparator;

public class SalaryComparator implements Comparator<Employee>{
@Override
    public int compare(Employee employee1, Employee employee2) {
        return employee1.getSalary() - employee2.getSalary();
    }
}

ListObjectsComparisonAndSortingExample.java

package com.gaurav.compare.lists;
/**
 * @author Gaurav
 *
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ListObjectsComparisonAndSortingExample {
public static void main(String[] args) {
 
       System.out.println("# COMPARING AND SORTING LIST OBJECTS BASED ON MULTIPLE ATTRIBUTES #\n");
 
       List<Employee> employeesList = new ArrayList<Employee>();
 
       employeesList.add(new Employee("Tusaar", "Developer", 44, 82000));
       employeesList.add(new Employee("Suresh", "Designer", 32, 77500));
       employeesList.add(new Employee("Bimlesh", "Designer", 44, 141900));
       employeesList.add(new Employee("Piyush", "Programmer", 26, 65700));
       employeesList.add(new Employee("Tiya", "Designer", 44, 131300));
       employeesList.add(new Employee("Culinan", "Programmer", 31, 55000));
       employeesList.add(new Employee("Amit", "Programmer", 27, 58000));
       employeesList.add(new Employee("Aaditya", "Designer", 31, 155000));
       employeesList.add(new Employee("Bimal", "Programmer", 24, 39000));
       employeesList.add(new Employee("Smith", "Developer", 29, 89000));
       employeesList.add(new Employee("Jimmy", "Developer", 36, 69000));
       employeesList.add(new Employee("Peter", "Developer", 38, 152000));
       employeesList.add(new Employee("Ali", "Programmer", 37, 85000));
       employeesList.add(new Employee("Dravid", "Developer", 33, 10000));
       employeesList.add(new Employee("Jatin", "Designer", 34, 83250));
 
       System.out.println("*** Displaying the employee list objects before sorting:\n");
 
       for (Employee employee : employeesList) {
           System.out.println(employee);
       }
 
       Collections.sort(employeesList, new ComparatorChain(
               new DesignationComparator(),
               new AgeComparator(),
               new SalaryComparator())
       );
 
       System.out.println("\n*** Displaying the employee list objects after sorting:\n");
 
       for (Employee employee : employeesList) {
           System.out.println(employee);
       }
 
   }
}


Result:-

# COMPARING AND SORTING LIST OBJECTS BASED ON MULTIPLE ATTRIBUTES #

*** Displaying the employee list objects before sorting:

Tusaar Developer 44 82000
Suresh Designer 32 77500
Bimlesh Designer 44 141900
Piyush Programmer 26 65700
Tiya Designer 44 131300
Culinan Programmer 31 55000
Amit Programmer 27 58000
Aaditya Designer 31 155000
Bimal Programmer 24 39000
Smith Developer 29 89000
Jimmy Developer 36 69000
Peter Developer 38 152000
Ali Programmer 37 85000
Dravid Developer 33 10000
Jatin Designer 34 83250

*** Displaying the employee list objects after sorting:

Aaditya Designer 31 155000
Suresh Designer 32 77500
Jatin Designer 34 83250
Tiya Designer 44 131300
Bimlesh Designer 44 141900
Smith Developer 29 89000
Dravid Developer 33 10000
Jimmy Developer 36 69000
Peter Developer 38 152000
Tusaar Developer 44 82000
Bimal Programmer 24 39000
Piyush Programmer 26 65700
Amit Programmer 27 58000
Culinan Programmer 31 55000
Ali Programmer 37 85000




Monday 3 August 2015

Visitor Design Pattern Implementation

Visitor Design Pattern


Question : - What is Visitor Design Pattern?
Answer : - This design pattern also falls under Behavioral design pattern. Using this design pattern, we can move the operational logic from the objects to another class. This pattern is providing a way of separating an algorithm from an object structure on which it operates. This visitor pattern is used to simplify operations on groupings of related objects.

As of GOF:-Allows for one or more operation to be applied to a set of objects at runtime, decoupling the operations from the object structure.

Benefits : -

Adding new operatios are easy because if the logic of operation changes, then we need to make change only in the visitor implementation rather than doing it in all the item classes.
Visitors can gather state as they visit each element in the object structure. Without a visitor, this state would have to be passed as an extra arguments to the operations that perform the traversal.

Disadvantage : -
During design period of application, we should know the return type of visit() methods otherwise
we will have to change the interface and all of its implementations.
If there are many implementations of visitor interface then extending that class is not a easy task.

For more information on Visitor design pattern please refer to the below link

In the below example, we will see the implementation of the Visitor design pattern

BodyPart.java

package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public interface BodyPart {
public void accept(BodyPartVisitor bodyPartVisitor);
}


Body.java

package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public class Body implements BodyPart{
private BodyPart[] bodyParts;
public Body(){
bodyParts = new BodyPart[] {new Eye(), new Heart(), new Kidney()};
}
@Override
  public void accept(BodyPartVisitor bodyPartVisitor) {
     for (int i = 0; i < bodyParts.length; i++) {
     bodyParts[i].accept(bodyPartVisitor);
     }
     bodyPartVisitor.visit(this);
  }

}

BodyPartVisitor.java

package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public interface BodyPartVisitor {
public void visit(Body body);
public void visit(Eye eye);
public void visit(Heart heart);
public void visit(Kidney kidney);
}

Eye.java

package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public class Eye implements BodyPart{

@Override
public void accept(BodyPartVisitor bodyPartVisitor) {
bodyPartVisitor.visit(this);
}

}


Heart.java


package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public class Heart implements BodyPart{

@Override
public void accept(BodyPartVisitor bodyPartVisitor) {
bodyPartVisitor.visit(this);
}

}

Kidney.java


package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public class Kidney implements BodyPart {

@Override
public void accept(BodyPartVisitor bodyPartVisitor) {
bodyPartVisitor.visit(this);
}

}

FullBodyScanner.java

package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public class FullBodyScanner implements BodyPartVisitor{

@Override
public void visit(Body body) {
System.out.println("Scanning Body");
}
@Override
public void visit(Eye eye) {
System.out.println("Scanning Eye");
}

@Override
public void visit(Heart heart) {
System.out.println("Scanning Heart");
}

@Override
public void visit(Kidney kidney) {
System.out.println("Scanning Heart");
}

}

VisitorDesignPatternDemo.java

package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public class VisitorDesignPatternDemo {
public static void main(String args[]) {
BodyPart body = new Body();
body.accept(new FullBodyScanner());
}
}

Result:-

Scanning Eye
Scanning Heart
Scanning Kidney
Scanning Body

JMS                                                            JSON                                                       JDBC

Wednesday 24 June 2015

Template Design Pattern Implementation

Template Design Pattern


Question : - What is Template Design Pattern?
Answer : - This design pattern also falls under Behavioral design pattern. The Template design pattern defines the common steps of an algorithm and leave the details to be implemented by the inherited classes. This design pattern is very popular in framework development. The Template Design Pattern helps in maximizing the code reusability.

As per GOF:- “Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithms structure.”

Question :- How Template pattern works?
Answer : -
  • Define an abstract base class with a qualified method which contains common implementation logic,
  • Declare the abstract methods for subclasses to override their particular behaviours
  • Declare a Template method in superclass that holds the common algorithm implementation steps.
  • Derived classes can override implemented methods according to their requirements.
The Template pattern is achieved through abstract classes. The Template method design pattern works on a certain principle which tells Don't call us, we'll call you. The template method in the parent class controls the overall process.

Question : - When to use Template design pattern?
Answer:-
  • When behaviour of an algorithm can vary in parent and child classes, we should leave subclasses to implement the behaviour through overriding.
  • If we want to avoid code duplication.

Example of Template design pattern in JDK :-
a) Template design pattern in Java :-
  • All non-abstract methods of java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.
  • All non-abstract methods of java.util.AbstractList, java.util.AbstractSet and java.util.AbstractMap.
  • javax.servlet.http.HttpServlet, all the doXXX() methods by default sends a HTTP 405 "Method Not Allowed" error to the response. You're free to implement none or any of them.
  • org.springframework.jdbc.core.JdbcTemplate class has all the common repeated code blocks related with JDBC workflow, such as update, query, execute,
  • Similarly, we have HibernateTemplate and others
b) Real time example of Template design pattern:-
  • We are creating web project using Eclipse, then it is providing a sample structure for a web project and few required files like web.xml.
  • Code generators
  • XML parsers.
  • In MS-Office, when we will try to create a spreedsheet using MS-excel or any presentation using MS-Powerpoint then we can see the template are available for specific workflow.
  • Validation in business components
We will see the example where we will use an order template for normal and bulk order.

OrderTemplate.java


package com.gaurav.designpattern.template;

/**
 * @author Gaurav
 * 
 */
public abstract class OrderTemplate {
public boolean isGift;

public abstract boolean getSelectedItem();

public abstract boolean getPayment(double itemCost, double grossTotal);

public final void getGiftPack() {
System.out.println("Selected item is packed as Gift.");
}

public abstract boolean delivery();

public final boolean orderProcess(double itemCost, double grossTotal) {
boolean orderProcessFlag = false;
orderProcessFlag = getSelectedItem();

if (orderProcessFlag) {
getPayment(itemCost, grossTotal);
}

double giftAmount = grossTotal - itemCost;

if (giftAmount >= 30.00)
isGift = true;
else
isGift = false;

boolean isDelivered = false;
if (orderProcessFlag && isGift) {
getGiftPack();
isDelivered = delivery();
} else {
isDelivered = delivery();
}
return isDelivered;
}

}


Order.java


package com.gaurav.designpattern.template;
/**
 * @author Gaurav
 * 
 */
public class Order extends OrderTemplate{

@Override
public boolean getSelectedItem() {
boolean indicator = false;
System.out.println("product are added to shopping cart");
System.out.println("Delivery address is provided by customer.");
indicator = true;
return indicator;
}

@Override
public boolean getPayment(double itemCost, double grossTotal) {
boolean iPaymentSuccessful = false;
System.out.println("Payment is provided by the customer");
iPaymentSuccessful = true;
return iPaymentSuccessful;
}

@Override
public boolean delivery() {
boolean isDelivered = false;
System.out.println("Item got delivered to the customer.");
isDelivered = true;
return isDelivered;
}

}

BulkOrder.java


package com.gaurav.designpattern.template;
/**
 * @author Gaurav
 * 
 */
public class BulkOrder extends OrderTemplate{
@Override
public boolean getSelectedItem() {
boolean indicator = false;
System.out.println("Multiple products are added to shopping cart");
System.out.println("Delivery address which is warehouse is provided by the retailer.");
indicator = true;
return indicator;
}

@Override
public boolean getPayment(double itemCost, double grossTotal) {
boolean iPaymentSuccessful = false;
System.out.println("This order is for cash on delivery, payment is not given by the retailer.");
iPaymentSuccessful = true;
return iPaymentSuccessful;
}

@Override
public boolean delivery() {
boolean isDelivered = false;
System.out.println("Item got delivered to the retailer.Payment is received.");
isDelivered = true;
return isDelivered;
}
}

TemplateDesignPatternDemo.java


package com.gaurav.designpattern.template;
/**
 * @author Gaurav
 * 
 */
public class TemplateDesignPatternDemo {
public static void main(String... args) {
double itemCost = 5000; 
double grossTotal = 5030;
System.out.println("#####     This is the example for normal Order using generic OrderTemplate.     #####");
OrderTemplate order = new Order();
order.orderProcess(itemCost, grossTotal);
System.out.println("\n#####     This is the example for bulk Order using generic OrderTemplate.     #####");
itemCost = 200000.00; 
grossTotal = 200000.00;
OrderTemplate bulkOrder = new BulkOrder();
bulkOrder.orderProcess(itemCost, grossTotal);
}
}

Result:-

#####     This is the example for normal Order using generic OrderTemplate.     #####
product are added to shopping cart
Delivery address is provided by customer.
Payment is provided by the customer
Selected item is packed as Gift.
Item got delivered to the customer.

#####     This is the example for bulk Order using generic OrderTemplate.     #####
Multiple products are added to shopping cart
Delivery address which is warehouse is provided by the retailer.
This order is for cash on delivery, payment is not given by the retailer.
Item got delivered to the retailer.Payment is received.