Tuesday, July 17, 2012

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class SortingFactoryImplementation {

   
    public static void main(String[] args) {
       
        ArrayList aList = new ArrayList();
        aList.add(new Person("Bill", 123456L, 34));
        aList.add(new Person("Moe", 423456L, 32));
        aList.add(new Person("Joe", 3456L, 49));
        SortingFactory s=null;

        //using factory to create concrete sort order based on name
        Collections.sort(aList,new SortingFactory("name").createSortOrder());

        System.out.println("Sorted by name");
        for(Person p:aList){
            System.out.println(p.getName()+" "+p.getAge()+" "+p.getContactNo());
        }
        Collections.sort(aList,new SortingFactory("age").createSortOrder());
        System.out.println("\nSorted by age");
        for(Person p:aList)
        {
            System.out.println(p.getAge()+" "+p.getContactNo()+" "+p.getName());
        }
        System.out.println("\nSorting by contact number");
        Collections.sort(aList,new SortingFactory("contact").createSortOrder());
        for(Person p:aList){
            System.out.println(+p.getContactNo()+" "+p.getAge()+" "+p.getName());
        }
       
       
    }
   
}




//this is the class whose attributes will be sorted using comparator Interface
class Person
{
    String name;
    Integer age;
    long contactNo;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Long getContactNo() {
        return contactNo;
    }
    public void setContactNo(Long contactNo) {
        this.contactNo = contactNo;
    }
    public Person(String name,Long contactNo,Integer age) {
        this.age=age;
        this.name=name;
        this.contactNo=contactNo;
       
    }
   
}
class NameSort extends  Sort
{
   
    public int compare(Person o1, Person o2) {
        return o1.getName().compareTo(o2.getName());
    }
}
class AgeSort extends Sort
{
    public int compare(Person o1, Person o2) {
        return o1.getAge().compareTo(o2.getAge());
    }
}
class ContactSort extends Sort
{
   
    public int compare(Person o1, Person o2) {
        return o1.getContactNo().compareTo(o2.getContactNo());
    }
   
}

//this class must be extended by all concrete product classes
abstract class  Sort implements Comparator
{
    public int compare(Person o1, Person o2) {return 0;}

   
   
}

//the factory
class SortingFactory {
   
    String type;
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public SortingFactory(String type) {
        this.type=type;
    }
    public Sort createSortOrder()
    {
        Sort s=null;
        if(getType().equals("name"))
            s=new NameSort();
        else if(getType().equals("age"))
             s=new AgeSort();
        else if(getType().equals("contact"))
            s=new ContactSort();
        return s;
    }

}


/****Output****/
Sorted by name
Bill 34 123456
Joe 49 3456
Moe 32 423456

Sorted by age
32 423456 Moe
34 123456 Bill
49 3456 Joe

Sorting by contact number
3456 49 Joe
123456 34 Bill
423456 32 Moe





No comments:

Post a Comment