1024programmer Java Using dynamic proxy classes to implement memory functions in JAVA (1)_MySQL

Using dynamic proxy classes to implement memory functions in JAVA (1)_MySQL

Memory is a design pattern derived from procedural languages ​​such as Lisp, Python, and Perl. It can memorize the previous calculation results. A function that implements the memory function, with an explicit cache, so the calculated results can be obtained directly from the cache without having to perform calculations every time.
Memory can significantly improve computationally heavy codes Efficiency. And it is a reusable solution.
This article explains how to use this pattern in Java, and provides a memory class that can provide the above functions:
Foo foo = (Foo) Memoizer .memoize(new FooImpl());
Here, Foo is an interface, and the methods it contains need to be memorized. FooImpl is an implementation of Foo. foo is a reference to Foo. The methods are basically the same as FooImpl, the difference is that The value returned by Foo will be cached. The advantage of a single memory class is that adding memory functionality to any class is very simple: define an interface containing the methods that need to be remembered, and then call memoize to implement an instance.

In order to understand how the memory class is implemented, we will explain it in several steps. First, I explain why caching can be implemented in the classes that require it. Then, I test how to add a cache wrapper for a specific class. Finally, I explain how to make a cache wrapper universal for any class.

Adding caching for computationally intensive programs
As a computationally intensive program For example, let’s consider the example of PiBinaryDigitsCalculator – calculating binary data pi. The only public method calculateBinaryDigit takes one parameter: an integer n, which represents the number of digits to be precise. For example, 1000000 will return one million digits after the decimal point, Return through byte value – each bit is 0 or 1. (For the algorithm, please refer to:

public >
This is a simple refactored version of the previous implementation of PiBinaryDigitsCalculator. CachingBinaryDigitsCalculator wraps the BinaryDigitsCalculator handle and adds a cache for the calculateBinaryDigit method call. This method improves the readability and maintainability of the code. Users cannot directly use the BinaryDigitsCalculator interface to implement the algorithm, so if it is necessary to turn off the cache block, it will be easy to implement.
Also, a suitable test program It’s easy to write. For example, we write a fake BinaryDigitsCalculator implementation. Each time calculateBinaryDigit is called, it is given the same parameters and returns different values. In this way, we can test whether the cache is working, because if the same value is returned every time value, it proves that the cache is working normally. This kind of test was impossible in the previous simple implementation.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/719566

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

The latest and most comprehensive programming knowledge, all in 1024programmer.com

© 2023 1024programmer - Encyclopedia of Programming Field
Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us