๐Ÿš€ Lesson 7: Advanced OOP Features

โฑ 30-45 min ๐Ÿ“Š Advanced ๐Ÿš€ Power Features ๐Ÿ”ฅ Modern Dart 2026 ๐Ÿ”— Official Source
๐Ÿš€

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%

Loading question...