26

Creational Design Patterns: Prototype Pattern [Snippet]

 5 years ago
source link: https://www.tuicool.com/articles/hit/ueyqYzU
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Creational Design Patterns: Prototype Pattern [Snippet]

DZone's Guide to

Creational Design Patterns: Prototype Pattern [Snippet]

Want to learn more about using creational design patterns? Check out this tutorial on how to use the prototype design pattern in Java to learn more!

Free Resource

Join the DZone community and get the full member experience.

Verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

The prototype pattern is used to create a copy of an object. This pattern can be really useful, especially when creating an object from scratch. In comparison with the builder , factory , and abstract factory patterns, it does not create an object from scratch — it clones it.

However, with the  singleton pattern, it creates multiple copies of an instance whilst the singleton has to ensure that only one will exist.

Imagine the scenario of an object that, which in order to be created, requires a very resource-intensive method. It can be a database query with many joins or even the result of a federated search.

We want those data to be processed by various algorithms using one thread per algorithm. Every thread should have its own copy of the original instance, since using the same object will result in inconsistent results.

Here, we have an interface for representing the result of a search:

package com.gkatzioura.design.creational.prototype;

public interface SearchResult {

    SearchResult clone();

    int totalEntries();

    String getPage(int page);
}

And, we have the implementation of the SearchResult  and  the   FederatedSearchResult .

package com.gkatzioura.design.creational.prototype;

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

public class FederatedSearchResult implements SearchResult {

    private List<String> pages = new ArrayList<String>();

    public FederatedSearchResult(List<String> pages) {
        this.pages = pages;
    }

    @Override
    public SearchResult clone() {

        final List<String> resultCopies = new ArrayList<String>();
        pages.forEach(p->resultCopies.add(p));
        FederatedSearchResult federatedSearchResult = new FederatedSearchResult(resultCopies);
        return federatedSearchResult;
    }

    @Override
    public int totalEntries() {
        return pages.size();
    }

    @Override
    public String getPage(int page) {
        return pages.get(page);
    }
}

We can use the clone method and create as many copies as we need of this object.

You can find the source code on GitHub .

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

DOWNLOAD

Topics:

java , design-pattern , creational design pattern , snippet , code , singleton pattern , prototype pattern

Published at DZone with permission of Emmanouil Gkatziouras , DZone MVB . See the original article here.

Opinions expressed by DZone contributors are their own.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK