Skip to content

Instantly share code, notes, and snippets.

@shan-shaji
Last active July 30, 2023 16:30
Show Gist options
  • Save shan-shaji/737eddbb855681ea95a07bfe6b665232 to your computer and use it in GitHub Desktop.
Save shan-shaji/737eddbb855681ea95a07bfe6b665232 to your computer and use it in GitHub Desktop.
Dart-SRP
# Dart-SRP
void main() {
ApiController apiRedisService = ApiController(cacheService: RedisService());
ApiController apiNodeCacheService = ApiController(cacheService: NodeCacheService());
apiRedisService.saveToCache('Hello');
apiNodeCacheService.saveToCache('Main');
}
class ApiController {
final CacheService cacheService;
ApiController({required this.cacheService});
void saveToCache(dynamic data) {
cacheService.saveToCache(data);
}
void readFromCache(String key) {
cacheService.readFromCache(key);
}
}
abstract class CacheService {
void saveToCache(dynamic data);
dynamic readFromCache(String key);
bool updateCache(String key, dynamic value);
bool deleteCache(String key);
}
class RedisService extends CacheService {
@override
void saveToCache(dynamic data) {
print('RedisService: saveToCache');
}
@override
dynamic readFromCache(String key) {
print('RedisService: readFromCache');
return {'key': 'hello'};
}
@override
bool updateCache(String key, dynamic value) {
print('RedisService: updateCache');
return true;
}
@override
bool deleteCache(String key) {
print('RedisService: deleteCache');
return false;
}
}
class NodeCacheService extends CacheService {
@override
void saveToCache(dynamic data) {
print('NodeCacheService: saveToCache');
}
@override
dynamic readFromCache(String key) {
print('NodeCacheService: readFromCache');
return {'key': 'hello'};
}
@override
bool updateCache(String key, dynamic value) {
print('NodeCacheService: updateCache');
return true;
}
@override
bool deleteCache(String key) {
print('NodeCacheService: deleteCache');
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment