----

Prototype Design Pattern Example

Prototype Design Pattern: Prototype design pattern is a creational design pattern and it is a template for creating an object from an already existing object. 

In this design pattern you use an already existing object as a prototype for creating a new object. If creating an object is time consuming, and if your application needs multiple such objects which are exactly the same or slightly different then you can use prototype pattern.

Prototype pattern can be used in transactional system, where in before performing a transaction, a clone of the prototype object is created, transaction is performed on the cloned object and if the transaction is successful  the prototype is replaced with cloned object by making the cloned object as the prototype. In case if transaction fails then the cloned object can be completely discarded.

The prototype object holds certain amount of information and if you need a similar object you can then clone the prototype object to create a similar object. If your application requires similar object but differ slightly then you can clone the prototype object and modify it slightly to fit the need of the application.

Let us consider an application which maintains user profile information in a database. When ever a user profile information is required the application reads the information from the database over the network and an object let us say UserProfile instance is created and used for display purpose. To modify any information let us say address, the database is updated and all the information are re-read and a new user profile object instance is created. The new object which now holds updated profile information is now used for display purpose.

The UserProfile class holds all the information which is retrieved from the database.

A simple UserProfile.java can have following implementation.

public class UserProfile {
private int id;
private String name;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "User id: " + id + ", name: " + this.name + ", address: "+ address;
}
}

The traditional client application retrieves the user profile information from database, display the user information and updates the personal details like address or any other detail.

public class UserProfileApplication {

          public static void main(String args[]) {
                    UserProfile  userProfile = getUserProfile(12345);
                    display(userProfile);

                    updateAddress(12345, "YYY Mumbai, India");
                    userProfile = getUserProfile(12345);
                    display(userProfile);
          }

 // This method will read the user profile information from the database
private static UserProfile getUserProfile(int userId) {
                 // check if userId exists in the database and if exists
                 // read all the user profile information and set to this object as shown
                    UserProfile userProfile = new UserProfile();
                    userProfile.setId(userId);
                    userProfile.setName("Guest");
                    userProfile.setAddress("Delhi, India");
                    return userProfile;
           }

           /* You can display user profile information in a well designed UI using UserProfile bean, for
             Simplicity the information is printed to the console */
           private static void display(UserProfile userProfile) {
                    System.out.println("User details :" + userProfile);
           }

           private static boolean updateAddress(int userId, String address) {
                  // Update the address in the database for the input userId
                  // execute update transaction and return success or failure here
return true;
           }
}

Problem: In this example, the application first reads user profile information from the database and displays it in the console. The address is then updated and the information is read again from the database and displayed again. Here if you see UserProfile object is created multiple times which is not desirable because every time user profile is requested, new UserProfile object is created which is essentially time consuming operation.

Solution: Initially read user profile information only once into UserProfile object. This object can be used for display purpose...


No comments :

Post a Comment