UrbanPro
true

Learn Advanced Java coaching from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Design Pattern

Paras Chawla
29/05/2017 0 0

Prototype Design Pattern:

Ø Prototype pattern refers to creating duplicate object while keeping performance in mind.

Ø This pattern involves implementing a prototype interface which tells to create a clone of the current object.

Ø This pattern is used when creation of object directly is costly. For example, an object is to be created after a costly database operation.

Ø We can cache the object, returns its clone on next request and update the database as and when needed thus reducing database calls.

 Prototype DP involves cloning of object and thus object needs to:

 Ø Implement Cloneable interface

 Ø Override clone() of Object class

 public abstract class Payment implements Cloneable {

    

       protected double amount;

          

        public double getAmount() {

                return amount;

           }

 

           public void setAmount(double amount) {

                this.amount = amount;

           }

          

     public abstract void makePayment();

    

     public Object clone() {

                 Object clone = null;

                 try {

                    clone = super.clone();

                 } catch (CloneNotSupportedException e) {

                    e.printStackTrace();

                 }

                 return clone;

              }   

}

public class CreditCard extends Payment {

 

     public void makePayment() {

           System.out.println("Payment of "+amount+ " by Credit Card");

     }

 

}

 

public class CashPayment extends Payment {

 

     public void makePayment() {

           System.out.println("Payment of "+amount+" by Cash");

     }

    

}

 

public class PaymentCache {

 

     private Map<String, Payment> map = new HashMap();

 

     public Payment getPayment(String key) {

           Payment payment = map.get(key);

           return (Payment) payment.clone();

     }

 

     // Map of Objects which are frequently used...

     // Original objects are acting as template objects...

 

     public void loadCache() {

           CashPayment cash = new CashPayment();

           cash.setAmount(2000);

           map.put("cash", cash);

 

           CreditCard credit = new CreditCard();

           credit.setAmount(4000);

           map.put("credit", credit);

     }

}

 

public class Client {

 

     public static void main(String[] args) {

           PaymentCache cache= new PaymentCache();

           cache.loadCache();

          

           Payment payment=cache.getPayment("cash");

           payment.makePayment();

          

           Payment payment1=cache.getPayment("credit");

           payment1.makePayment();

          

     }

}

 

Output:

Payment of 2000.0 by Cash

Payment of 4000.0 by Credit Card

Application:

Session replication from one server to another server.

Generating the GUI having many numbers of similar controls.

Advantage of Prototype Pattern:

It reduces the need of subclassing.

It hides complexities of creating objects.

The clients can get new objects without knowing which type of object it will be.

It lets you add or remove objects at runtime.

Usage of Prototype Pattern:

When the classes are instantiated at runtime.

When the cost of creating an object is expensive or complicated.

When you want to keep the number of classes in an application minimum.

When the client application needs to be unaware of object creation and representation.

 

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

A Tutorial On Dynamic Programming
What is Dynamic Programming? Dynamic Programming, DP in short, is an intelligent way of solving a special type of complex problems which otherwise would hardly be solved in realistic time frame. What...

How Big Data Hadoop and its importance for an enterprise?
In IT phrasing, Big Data is characterized as a collection of data sets (Hadoop), which are so mind boggling and large that the data cannot be easily captured, stored, searched, shared, analyzed or visualized...

Solution to a trigonometric problem requested by one student
∫ sin 4x cos 2x dx as we don't have any ready made formulae for product of function in integration, let's split it into sum or difference. sin A cos B = 1/2 {sin (A + B ) + sin ( A - B )} A=...

Communication Rules
The rules for communication are as follows: 1. Ensure that the conversation or the delivery of the dialogue fits the purpose. 2. The information you share must be accurate, in brief and the content...

Solution to a trigonometric problem.
∫ sin‾¹ x dx Let's use integration by parts Let u = sin‾¹ x. ( du = 1/√1 - x² dx ∴ dx = du/√1 - x² ). dv = dx ∴ v = ∫ 1• dx...
X

Looking for Advanced Java coaching Classes?

The best tutors for Advanced Java coaching Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Advanced Java coaching with the Best Tutors

The best Tutors for Advanced Java coaching Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more