Friday, 31 October 2014

Multiton Design Pattern


Multiton is a creational design pattern. You can assume it as an extension of singleton design pattern. Rather than having a single instance per application, multiton pattern instead ensures a single instance per key.

Let’s say there is a student database, where each student mapped to exactly one address. We can do the implementation using multiton design pattern.

Address.java
public class Address {
    private String street;
    private String city;
    private String district;
    private String state;
    private String country;
    private String PIN;

    public String getPIN() {
        return PIN;
    }

    public String getCity() {
        return city;
    }

    public String getCountry() {
        return country;
    }

    public String getDistrict() {
        return district;
    }

    public String getState() {
        return state;
    }

    public String getStreet() {
        return street;
    }

    public void setPIN(String PIN) {
        this.PIN = PIN;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

    public void setState(String state) {
        this.state = state;
    }

    public void setStreet(String street) {
        this.street = street;
    }
}


Student.java
public class Student {
    private String firstName;
    private String lastName;
    private int age;
    private int id;

    public int getAge() {
        return age;
    }

    public String getFirstName() {
        return firstName;
    }

    public int getId() {
        return id;
    }

    public String getLastName() {
        return lastName;
    }

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

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

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

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}


StudentDatabase.java
import java.util.Map;
import java.util.HashMap;

public class StudentDatabase {
    private static Map<Student, Address> studentDb = new HashMap<Student, Address> ();

    /* Get the Address associated with student */
    public static Address getStudentAddress(Student s){
        /* Check whether student exist in database or not */
        if(studentDb.containsKey(s)){
             Address addr;
             addr = studentDb.get(s);

             /* If address not exist for this student, create address instance and map it to student */
             if(addr == null){
                 addr = new Address();
                 studentDb.put(s, addr);
             }
             return addr;
        }
        return null;
    }

    public static Map<Student, Address> getStudentDb() {
        return studentDb;
    }

    public static void setStudentDb(Map<Student, Address> studentDb) {
        StudentDatabase.studentDb = studentDb;
    }
}


MultitonPattern.java
import java.util.HashMap;
import java.util.Map;

public class MultitonPattern {
    public static void main(String args[]){
        Map<Student, Address> studentDb = new HashMap<Student, Address> ();

        Student s1 = new Student();
        Address a1 = null;

        s1.setFirstName("Krishna");
        s1.setLastName("Gurram");
       
        studentDb.put(s1, a1);

        StudentDatabase.setStudentDb(studentDb);

        Address addr = StudentDatabase.getStudentAddress(s1);
        addr.setCity("Ongole");
        addr.setCountry("India");
        addr = StudentDatabase.getStudentAddress(s1);

        System.out.println(addr.getCity());
        System.out.println(addr.getCountry());

    }
}


Output
Ongole
India

No comments:

Post a Comment