Using Java dynamic proxy class to realize memory function

Source: Internet
Author: User
Tags final require wrapper

Memory is a design pattern derived from Lisp,python, and Perl and other procedural languages, which can be used to memorize the results of previous calculations. A function that implements the memory function, with explicit cache, so that the results that have been computed can be obtained directly from the cache, without having to compute each time.

Memory can significantly increase the efficiency of large computational code. And it's a reusable solution.

This article explains how to use this pattern in Java and provides a "memory class" that can provide the above functionality:

Foo foo = (foo) memoizer.memoize (new Fooimpl ());

Here, Foo is an interface, and it contains methods that require memory. Fooimpl is an implementation of Foo. Foo is a reference to Foo. The method is basically the same as the Fooimpl, except that the value returned by Foo is cached. The advantage of a single memory class is that it is simple to add memory to any class: Define an interface that contains methods that require memory. Then call Memoize to implement an instance.

To understand how memory classes are implemented, we'll explain in a few steps. First, let me explain why caching can be implemented in classes that need it. Then I'll test how to add a cache wrapper to a particular class. Finally, let me explain how you can make a cache wrapper universal to any class.

Adding caching for programs that have large amounts of computation

As an example of a large computational program, we consider pibinarydigitscalculator This example-compute the binary data pi. The only public method Calculatebinarydigit with an argument: integer n, Represents the number of digits that need to be precise. For example, 1000000 will return 1 million digits after the decimal point and return with a byte value of 0 or 1 per digit.

public class PiBinaryDigitsCalculator {
/**
* Returns the coefficient of 2^n in the binary
* expansion of pi.
* @param n the binary digit of pi to calculate.
* @throws ValidityCheckFailedException if the validity
* check fails, this means the implementation is buggy
* or n is too large for sufficient precision to be
* retained.
*/
public byte calculateBinaryDigit(final int n) {
return runBBPAlgorithm(n);
}
private byte runBBPAlgorithm(final int n) {
// Lengthy routine goes here ...
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.