Recap - Generic Storage
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 69 of 110.
Challenge
EasyLet's build a generic storage system that combines everything you've learned about collections and generics! You'll create a Storage<T> class that can store items of any type, retrieve them, and be iterated over using a for-in loop.
You'll organize your code into two files:
storage.dart: Create your generic storage system with two classes:- A
StorageIterator<T>class that implementsIterator<T>to enable iteration over stored items - A
Storage<T>class that extendsIterable<T>with the following capabilities:- A private list to hold items internally
- An
add(T item)method to store a new item - A
getAt(int index)method that returns the item at the given index - A
countgetter that returns the number of stored items - An
iteratorgetter that returns aStorageIteratorfor the stored items
- A
main.dart: Import your storage file and demonstrate the generic storage with different types:- Create a
Storage<String>and add three city names:'Paris','Tokyo','Sydney' - Print the count of items
- Print the item at index 1
- Use a
for-inloop to print each city on its own line - Print an empty line
- Create a
Storage<int>and add four numbers:100,200,300,400 - Use the
wheremethod to filter values greater than 150, then print the resulting list
- Create a
Your storage system should work seamlessly with any type while maintaining full type safety. Because it extends Iterable, it automatically gains access to powerful methods like where, map, and fold!
Expected output:
Count: 3
Item at 1: Tokyo
Paris
Tokyo
Sydney
[200, 300, 400]Try it yourself
import 'storage.dart';
void main() {
// TODO: Create a Storage<String> for cities
// Add three cities: 'Paris', 'Tokyo', 'Sydney'
// TODO: Print the count of items (format: "Count: X")
// TODO: Print the item at index 1 (format: "Item at 1: X")
// TODO: Use a for-in loop to print each city on its own line
// TODO: Print an empty line
print('');
// TODO: Create a Storage<int> for numbers
// Add four numbers: 100, 200, 300, 400
// TODO: Use where method to filter values greater than 150
// Then print the resulting list using toList()
}
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesLibraries & ImportsIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsInstance VariablesConstructor BasicsRecap - Simple Calculator4Null Safety
Intro to Null SafetyNullable vs Non-NullableThe ? and ! OperatorsLate Keyword & Null SafetyNull-Aware OperatorsNull Safety in ClassesRecap - User Profile System7Abstract Classes & Interfaces
Abstract ClassesAbstract MethodsInterfaces in DartImplicit InterfacesImplementing vs ExtendingMultiple InterfacesRecap - Shape Calculator10Collections & Generics
List, Set, Map OverviewType-Safe CollectionsGeneric ClassesGeneric MethodsGeneric ConstraintsIterable & IteratorRecap - Generic Storage13Advanced OOP Concepts
Composition vs InheritanceExtension MethodsCallable ClassesSealed Classes (Dart 3)Records (Dart 3)Patterns & Matching (3.0)Enums with Methods16Project: Library Management
Project OverviewBook and User Classes2Constructors in Dart
Default ConstructorNamed ConstructorsInitializer ListsConstant ConstructorsFactory ConstructorsRedirecting ConstructorsRecap - Shape Builder5Encapsulation
Public vs Private MembersThe _ Prefix ConventionLibrary-Level PrivacyGetters & Setters DepthInformation HidingRecap - Student Records8Mixins
Introduction to MixinsCreating MixinsUsing Multiple Mixinson Keyword in MixinsMixin vs InheritanceMixin vs InterfaceRecap - Animal System11Special Methods
toString() OverridehashCode & == OverrideComparable Interfacecall() MethodnoSuchMethod OverrideRecap - Custom Collection14Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern3Class Properties
Instance vs Static MembersFinal & Const FieldsLate VariablesStatic Methods & FieldsGetters and SettersRecap - Bank Account Manager6Inheritance
Basic InheritanceThe super KeywordMethod OverridingThe @override AnnotationThe final Class KeywordConstructors & InheritanceRecap - Employee Hierarchy9Polymorphism
Polymorphism BasicsPolymorphism via InterfacesRuntime Type CheckingThe is & as OperatorsCovariant KeywordRecap - Payment Processor