๐ Lesson 7: Advanced OOP Features
Beyond Basic OOP: The Next Level
You've mastered classes and inheritance. Now unlock Dart's secret weapons: enhanced enums, extensions, and mixins!
๐จ
Enhanced Enums
Enums with superpowers - properties, methods, and constructors!
๐
Extensions
Add methods to ANY class - even ones you don't own!
๐งฌ
Mixins
Compose classes from reusable building blocks!
Today's Project: "Super Logger" System
We'll build a professional logging framework using all advanced OOP features:
- ๐ Enhanced enum for log levels
- ๐ Extensions for string formatting
- ๐งฌ Mixins for reusable behaviors
- ๐ญ Factory constructors for flexible creation
Step 1
Enhanced Enums: Enums on Steroids
๐ Enum Evolution
๐ด Old Enums (Pre-2.17)
enum LogLevel {
debug,
info,
warning,
error
}
// Limited - just named values!
โจ Enhanced Enums (Dart 2.17+)
enum LogLevel {
debug(0, '๐'),
info(1, 'โน๏ธ'),
warning(2, 'โ ๏ธ'),
error(3, 'โ');
final int severity;
final String icon;
const LogLevel(this.severity, this.icon);
bool get isError => severity >= 3;
}
๐๏ธ Interactive Enum Builder
๐ Generated Enhanced Enum:
enum HttpStatus {
ok(200, 'OK'),
notFound(404, 'Not Found'),
serverError(500, 'Internal Server Error');
final int code;
final String message;
const HttpStatus(this.code, this.message);
bool get isSuccess => code >= 200 && code < 300;
bool get isError => code >= 400;
static HttpStatus? fromCode(int code) {
return values.where((s) => s.code == code).firstOrNull;
}
}
๐งช Test Your Enum
Step 2
Extensions: Supercharging Existing Classes
๐ Extension Playground
Add your own methods to built-in types:
๐ String Extensions
๐งช Extension Tester
.capitalize
Hello world
.isValidEmail
false
.toSlug
hello-world
.wordCount
2
๐ Extension Use Cases
๐จ UI Helpers
extension ColorExtension on Color {
Color get darker =>
Color.fromARGB(alpha,
(red * 0.8).toInt(),
(green * 0.8).toInt(),
(blue * 0.8).toInt());
}
๐ Date Formatting
extension DateFormatting on DateTime {
String get timeAgo {
final diff = DateTime.now().difference(this);
if (diff.inDays > 365) return '${(diff.inDays/365).floor()}y ago';
if (diff.inDays > 30) return '${(diff.inDays/30).floor()}mo ago';
if (diff.inDays > 0) return '${diff.inDays}d ago';
return 'today';
}
}
๐ข Number Formatting
extension NumberFormatting on num {
String get toFileSize {
if (this < 1024) return '$this B';
if (this < 1048576) return '${(this/1024).toStringAsFixed(1)} KB';
return '${(this/1048576).toStringAsFixed(1)} MB';
}
}
Step 3
Mixins: Composable Behaviors
๐งฌ Mixin Composer
Build classes by combining mixins like LEGO blocks!
Available Mixins:
๐ Logger
Adds logging capability
โ
Validator
Adds validation methods
๐พ Cache
Adds caching behavior
๐ Notifier
Adds notification system
Your Class:
class UserService
extends BaseService
Click mixins to add them!
Available Methods:
- No mixins selected
๐ณ Mixin Hierarchy Visualizer
BaseService
UserService
Step 4
Factory Constructors: Flexible Object Creation
๐ญ Factory Pattern Simulator
Select a user type to create!
๐ Factory Constructor Pattern:
class User {
final String name;
final String role;
final List permissions;
User._(this.name, this.role, this.permissions);
factory User.admin(String name) => User._(name, 'Admin',
['read', 'write', 'delete', 'manage_users']);
factory User.regular(String name) => User._(name, 'User',
['read', 'write']);
factory User.guest() => User._('Guest', 'Guest', ['read']);
}
๐
Build: Super Logger System
๐ Enhanced LogLevel Enum
๐ String & DateTime Extensions
๐งฌ Logger Mixins
๐ฎ Main Application
๐ค Logger Output Preview
Click "Run Logger" to see the output!
๐ฎ Advanced OOP Challenge: Feature Master Quiz
๐ Score: 0/6
โก Streak: 0
โฑ Time: 0s
๐ง Mastery: 0%