Single variable cache

Advantages: convenient and easy to understand

Disadvantages: Each Cache function requires an additional *Cache variable to be maintained

String _getNativeVersionCache;
StringgetNativeVersion() { _getNativeVersionCache ?? = Native.getVersion();return _getNativeVersionCache;
}
Copy the code

Cache helper function

Advantages: Frequently used, only one memorize function is exposed

Disadvantages: Memorize often requires multiple overloading (functions that process different parameters)

T Function() memorize<T>(T Function() fun) {
	T cache;
	return() { cache ?? = fun();return cache;
	};
}

String _getNativeVersion() {
	return Native.getVersion();
}

String getNativeVersion = memorize(_getNativeVersion);
Copy the code

Caching auxiliary objects

Advantages: convenient, easy to understand, only need to disclose one person

Disadvantages: Not often used

Map<String.dynamic> memorize = {};
String getNativeVersion() {
	memorize['getNativeVersion']???? = Native.getVersion();return memorize['getNativeVersion'];
}
Copy the code

conclusion

Single variable cache Cache helper function Caching auxiliary objects
convenient Square root x Square root
Easy to understand Square root x Square root
Easy to maintain x Square root Square root
The commonly used Square root Square root x