Dart Collections with OOP


Dart Collections with Object-Oriented Programming (OOP)

In Dart, collections like List, Set, and Map are commonly used data structures for storing and manipulating groups of related items. Combining Dart collections with OOP concepts allows you to create organized, modular, and reusable code structures.

Using collections with OOP often involves:

  1. Creating classes to represent data models.
  2. Storing instances of these classes in collections (e.g., lists of objects).
  3. Manipulating the collections to perform actions like filtering, searching, and sorting on objects.

Example: Using Collections with OOP in Dart

Let’s create a small program that demonstrates using a collection of Student objects stored in a list. The program will:

  1. Define a Student class with properties like name and grade.
  2. Store instances of Student in a list.
  3. Implement methods to perform operations on the collection, such as calculating the average grade and listing all students with grades above a certain threshold.

Code Example:

// Define the Student class class Student { String name; double grade; // Constructor Student(this.name, this.grade); // Method to display student information void displayInfo() { print("Student: $name, Grade: $grade"); } } // Class to manage a collection of Student objects class StudentCollection { List<Student> students = []; // Method to add a student to the collection void addStudent(Student student) { students.add(student); } // Method to calculate the average grade of students double calculateAverageGrade() { double totalGrade = students.fold(0, (sum, student) => sum + student.grade); return totalGrade / students.length; } // Method to display students with grades above a certain threshold void displayStudentsAboveGrade(double threshold) { for (var student in students) { if (student.grade > threshold) { student.displayInfo(); } } } } void main() { // Creating a StudentCollection instance var studentCollection = StudentCollection(); // Adding Student objects to the collection studentCollection.addStudent(Student("Alice", 85.0)); studentCollection.addStudent(Student("Bob", 92.5)); studentCollection.addStudent(Student("Charlie", 78.0)); studentCollection.addStudent(Student("Diana", 88.5)); // Display average grade of students print("Average Grade: ${studentCollection.calculateAverageGrade()}"); // Display students with grades above 80 print("\nStudents with grades above 80:"); studentCollection.displayStudentsAboveGrade(80); }

Explanation:

  1. Student Class:

    • This class has two properties, name and grade, and a displayInfo method to display the student’s details.
  2. StudentCollection Class:

    • Contains a List<Student> named students to store multiple Student objects.
    • Provides an addStudent method to add new Student objects to the list.
    • The calculateAverageGrade method calculates the average grade of all students by iterating through the list and summing up the grades.
    • The displayStudentsAboveGrade method accepts a grade threshold and displays details of students with grades above that threshold.
  3. Usage in main():

    • A StudentCollection instance is created.
    • Multiple Student objects are added to the collection.
    • The average grade of students is calculated and displayed.
    • Finally, students with grades above 80 are displayed.

Output:

Average Grade: 86.0 Students with grades above 80: Student: Alice, Grade: 85.0 Student: Bob, Grade: 92.5 Student: Diana, Grade: 88.5

Benefits of Using Collections with OOP in Dart

  1. Encapsulation: The StudentCollection class encapsulates the List<Student> and provides methods to manipulate it, which keeps the data safe and the code more organized.
  2. Reusability: The Student class can be reused in different contexts, and the StudentCollection class can be extended or modified without changing the underlying Student data model.
  3. Modularity: Separating the Student model from the operations on the collection makes the code modular, allowing easy maintenance and extension.
  4. Enhanced Readability: Using classes with methods for specific tasks makes the code more readable and easier to understand.

Summary

Using collections with OOP in Dart enables you to store and manage groups of objects in a structured way. This approach promotes encapsulation, reusability, and modularity. In the example, we demonstrated how a collection of Student objects can be managed through a StudentCollection class, performing operations like calculating averages and filtering based on criteria. This pattern is highly useful in real-world applications where data needs to be managed and manipulated efficiently.