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:
- Creating classes to represent data models.
- Storing instances of these classes in collections (e.g., lists of objects).
- 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:
- Define a
Student
class with properties likename
andgrade
. - Store instances of
Student
in a list. - 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:
Explanation:
Student Class:
- This class has two properties,
name
andgrade
, and adisplayInfo
method to display the student’s details.
- This class has two properties,
StudentCollection Class:
- Contains a
List<Student>
namedstudents
to store multipleStudent
objects. - Provides an
addStudent
method to add newStudent
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.
- Contains a
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.
- A
Output:
Benefits of Using Collections with OOP in Dart
- Encapsulation: The
StudentCollection
class encapsulates theList<Student>
and provides methods to manipulate it, which keeps the data safe and the code more organized. - Reusability: The
Student
class can be reused in different contexts, and theStudentCollection
class can be extended or modified without changing the underlyingStudent
data model. - Modularity: Separating the
Student
model from the operations on the collection makes the code modular, allowing easy maintenance and extension. - 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.