Created
January 25, 2023 16:19
-
-
Save jasonmcaffee/2ab40e11b5c26f9db51d3bbe9ee5e13c to your computer and use it in GitHub Desktop.
Dart hashmap memoization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:collection'; | |
HashMap memoizedTextThemes = HashMap<String, String>(); | |
abstract class PacificDesignSystem { | |
String get colorScheme; | |
String get textTheme { | |
if(memoizedTextThemes[colorScheme] == null){ | |
memoizedTextThemes[colorScheme] = 'textTheme for $colorScheme'; | |
print('added memoized entry for $colorScheme'); | |
} | |
return memoizedTextThemes[colorScheme]; | |
} | |
} | |
class PacificLight extends PacificDesignSystem { | |
@override | |
String get colorScheme => "light"; | |
} | |
class PacificDark extends PacificDesignSystem { | |
@override | |
String get colorScheme => "dark"; | |
} | |
void main() { | |
final pLight1 = PacificLight(); | |
final pLight2 = PacificLight(); | |
final pDark1 = PacificDark(); | |
final pDark2 = PacificDark(); | |
print(pLight1.textTheme); | |
print(pLight1.textTheme); | |
print(pLight2.textTheme); | |
print(pLight2.textTheme); | |
print(pDark1.textTheme); | |
print(pDark1.textTheme); | |
print(pDark2.textTheme); | |
print(pDark2.textTheme); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment