Dart: Generics, Generic Interfaces, Generic Methods

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

This article sorts out the generic related knowledge points in Dart

Before learning generics in Dart, first understand the two main problems that generics solve as follows

  • Redundancy problems caused by the same method logic but different data logic
  • The strategy adopted when it is impossible to determine whether the data type will change in the early stage of development

1. Generic method

The following shows how to solve the code redundancy problem of returning different int and String types through the generic method

String getData1(String value){
    
    
      return value;
  }

  int getData2(int value){
    
    
      return value;
  }

Not specifying the type can solve the above problem, but this will give up type checking

getData(value){
    
    
      return value;
  }

To solve the problem of code redundancy caused by different return types, and to perform type checking at the same time, generics can be used

T getData<T>(T value){
    
    
      return value;
  }

void main(){
    
    

    print(getData(21));

    print(getData('xxx'));

    getData<String>('你好');

    print(getData<int>(12));

}

2. Generic class

Usage of collection List generic class

Case: convert the following class into a generic class, and require that the int type data can be added to the List, and the String type data can also be added. But the type added for each call should be unified

Demo code is as follows

class PrintClass<T>{
    
    
      List list=new List<T>();
      void add(T value){
    
    
          this.list.add(value);
      }
      void printInfo(){
    
              
          for(var i=0;i<this.list.length;i++){
    
    
            print(this.list[i]);
          }
      }
 }

main() {
    
      
    PrintClass p=new PrintClass();
    p.add(11);
    p.add(5);
    p.printInfo();

  PrintClass p=new PrintClass<String>();

  p.add('你好');
  p.add('哈哈');
  p.printInfo();

  PrintClass p=new PrintClass<int>();

  p.add(12);
  p.add(23);
  p.printInfo();

  List list=new List();
  list.add(12);
  list.add("你好");
  print(list);

  List list=new List<String>();

  // list.add(12);  //错误的写法

  list.add('你好');
  list.add('你好1');

  print(list);

  List list=new List<int>();

  // list.add("你好");  //错误写法
  list.add(12); 

  print(list);
}

3. Generic interface

Realize the function of data caching: there are file caching and memory caching. Memory cache and file cache are implemented according to interface constraints.

  • Define a generic interface constraint to implement its subclass must have getByKey(key) and setByKey(key,value)

  • The value type when requesting setByKey is consistent with the type specified when instantiating the subclass

  abstract class ObjectCache {
    
    
    getByKey(String key);
    void setByKey(String key, Object value);
  }

  abstract class StringCache {
    
    
    getByKey(String key);
    void setByKey(String key, String value);
  }

  abstract class Cache<T> {
    
    
    getByKey(String key);
    void setByKey(String key, T value);
  }

class FlieCache<T> implements Cache<T>{
    
    
  
  getByKey(String key) {
    
        
    return null;
  }

  
  void setByKey(String key, T value) {
    
    
   print("我是文件缓存 把key=${
      
      key}  value=${
      
      value}的数据写入到了文件中");
  }
}

class MemoryCache<T> implements Cache<T>{
    
    
  
  getByKey(String key) {
    
       
    return null;
  }

  
  void setByKey(String key, T value) {
    
    
       print("我是内存缓存 把key=${
      
      key}  value=${
      
      value} -写入到了内存中");
  }
}
void main(){
    
    

    MemoryCache m=new MemoryCache<String>();
     m.setByKey('index', '首页数据');

     MemoryCache m=new MemoryCache<Map>();
     m.setByKey('index', {
    
    "name":"张三","age":20});
}

Guess you like

Origin blog.csdn.net/qjyws/article/details/128816899